|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Allows to manage cache |
|
4
|
|
|
* cache structure (module_code/cache_code.cache) |
|
5
|
|
|
* |
|
6
|
|
|
* @package Blue |
|
7
|
|
|
* @subpackage Cache |
|
8
|
|
|
* @author chajr <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace BlueCache; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
13
|
|
|
use Psr\Cache\CacheItemInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Cache implements CacheItemPoolInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use Common; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $deferred = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $cacheCommitExceptions = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $name |
|
31
|
|
|
* @return null|CacheItemInterface |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function getItem($name) |
|
34
|
|
|
{ |
|
35
|
2 |
|
if ($this->hasItem($name)) { |
|
36
|
1 |
|
return $this->storage->restore($name); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
return null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $names |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function getItems(array $names = []) |
|
47
|
|
|
{ |
|
48
|
4 |
|
$list = []; |
|
49
|
|
|
|
|
50
|
4 |
|
foreach ($names as $name) { |
|
51
|
1 |
|
$list[$name] = $this->getItem($name); |
|
52
|
4 |
|
} |
|
53
|
|
|
|
|
54
|
4 |
|
return $list; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
8 |
|
public function hasItem($name) |
|
62
|
|
|
{ |
|
63
|
8 |
|
return $this->storage->exists($name); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return bool |
|
68
|
|
|
* @throws CacheException |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function clear() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->storage->clear(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
* @return bool |
|
78
|
|
|
* @throws CacheException |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function deleteItem($name) |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->storage->clear($name); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param array $names |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function deleteItems(array $names) |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->storage->clearMany($names); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param CacheItemInterface $item |
|
96
|
|
|
* @return bool |
|
97
|
|
|
* @throws CacheException |
|
98
|
|
|
*/ |
|
99
|
8 |
|
public function save(CacheItemInterface $item) |
|
100
|
|
|
{ |
|
101
|
8 |
|
return $this->storage->store($item); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Keep cache items to store it later |
|
106
|
|
|
* |
|
107
|
|
|
* @param CacheItemInterface $item |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
3 |
|
public function saveDeferred(CacheItemInterface $item) |
|
111
|
|
|
{ |
|
112
|
3 |
|
$this->deferred[] = $item; |
|
113
|
3 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Store all cache items added by saveDeferred |
|
118
|
|
|
* |
|
119
|
|
|
* @return bool |
|
120
|
|
|
* @throws CacheException |
|
121
|
|
|
*/ |
|
122
|
12 |
|
public function commit() |
|
123
|
|
|
{ |
|
124
|
12 |
|
$this->cacheCommitExceptions = []; |
|
125
|
12 |
|
$flag = true; |
|
126
|
|
|
|
|
127
|
12 |
|
foreach ($this->deferred as $item) { |
|
128
|
3 |
|
if (!$this->commitSave($item)) { |
|
129
|
1 |
|
$flag = false; |
|
130
|
1 |
|
} |
|
131
|
12 |
|
} |
|
132
|
|
|
|
|
133
|
12 |
|
if (!empty($this->cacheCommitExceptions)) { |
|
134
|
1 |
|
throw new CacheException( |
|
135
|
1 |
|
'Error on saving cache items: ' . implode('; ', $this->cacheCommitExceptions) |
|
136
|
1 |
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
11 |
|
$this->deferred = []; |
|
140
|
|
|
|
|
141
|
11 |
|
return $flag; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param CacheItemInterface $item |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
3 |
|
protected function commitSave(CacheItemInterface $item) |
|
149
|
|
|
{ |
|
150
|
|
|
try { |
|
151
|
3 |
|
return $this->save($item); |
|
152
|
1 |
|
} catch (CacheException $exception) { |
|
153
|
1 |
|
$this->cacheCommitExceptions[] = $exception->getMessage(); |
|
154
|
|
|
|
|
155
|
1 |
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @throws CacheException |
|
161
|
|
|
*/ |
|
162
|
12 |
|
public function __destruct() |
|
163
|
|
|
{ |
|
164
|
12 |
|
$this->commit(); |
|
165
|
11 |
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|