1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Paillechat\ApcuSimpleCache; |
4
|
|
|
|
5
|
|
|
use Paillechat\ApcuSimpleCache\Exception\ApcuInvalidCacheKeyException; |
6
|
|
|
use Paillechat\ApcuSimpleCache\Exception\ApcuInvalidCacheKeysException; |
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
|
9
|
|
|
class ApcuCache implements CacheInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $namespace; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
22 |
|
private $defaultLifetime; |
20
|
|
|
|
21
|
22 |
|
public function __construct($namespace = '', $defaultLifetime = 0) |
22
|
22 |
|
{ |
23
|
22 |
|
$this->namespace = $namespace; |
24
|
|
|
$this->defaultLifetime = $defaultLifetime; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
18 |
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
18 |
|
public function get($key, $default = null) |
31
|
8 |
|
{ |
32
|
|
|
$this->assertKeyName($key); |
33
|
8 |
|
$key = $this->buildKeyName($key); |
34
|
|
|
|
35
|
|
|
$value = apcu_fetch($key, $success); |
36
|
|
|
if ($success === false) { |
37
|
|
|
return $default; |
38
|
|
|
} |
39
|
8 |
|
|
40
|
|
|
return $value; |
41
|
8 |
|
} |
42
|
8 |
|
|
43
|
|
|
/** |
44
|
8 |
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
8 |
|
public function set($key, $value, $ttl = null) |
47
|
|
|
{ |
48
|
|
|
$this->assertKeyName($key); |
49
|
|
|
$key = $this->buildKeyName($key); |
50
|
|
|
|
51
|
|
|
$ttl = is_null($ttl) ? $this->defaultLifetime : $ttl; |
52
|
2 |
|
|
53
|
|
|
return apcu_store($key, $value, (int)$ttl); |
|
|
|
|
54
|
2 |
|
} |
55
|
2 |
|
|
56
|
|
|
/** |
57
|
2 |
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function delete($key) |
60
|
|
|
{ |
61
|
|
|
$this->assertKeyName($key); |
62
|
|
|
$key = $this->buildKeyName($key); |
63
|
4 |
|
|
64
|
|
|
return apcu_delete($key); |
|
|
|
|
65
|
4 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function clear() |
71
|
2 |
|
{ |
72
|
|
|
return apcu_clear_cache(); |
73
|
2 |
|
} |
74
|
2 |
|
|
75
|
|
|
/** |
76
|
2 |
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
2 |
|
public function getMultiple($keys, $default = null) |
79
|
2 |
|
{ |
80
|
2 |
|
$keys = $this->convertKeysToArray($keys); |
81
|
1 |
|
$this->assertKeyNames($keys); |
82
|
|
|
$keys = $this->buildKeyNames($keys); |
83
|
2 |
|
|
84
|
|
|
$result = apcu_fetch($keys); |
85
|
2 |
|
|
86
|
2 |
|
if (!is_null($default) && is_array($result) && count($keys) > count($result)) { |
87
|
|
|
$notFoundKeys = array_diff($keys, array_keys($result)); |
88
|
2 |
|
$result = array_merge($result, array_fill_keys($notFoundKeys, $default)); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
2 |
|
$mappedResult = []; |
92
|
|
|
|
93
|
|
|
foreach ($result as $key => $value) { |
94
|
|
|
$key = preg_replace("/^$this->namespace/", '', $key); |
95
|
|
|
|
96
|
|
|
$mappedResult[$key] = $value; |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
return $mappedResult; |
|
|
|
|
100
|
|
|
} |
101
|
2 |
|
|
102
|
|
|
/** |
103
|
2 |
|
* {@inheritdoc} |
104
|
2 |
|
*/ |
105
|
1 |
|
public function setMultiple($values, $ttl = null) |
106
|
|
|
{ |
107
|
2 |
|
$values = $this->convertKeysToArray($values); |
108
|
|
|
$this->assertKeyNames(array_keys($values)); |
109
|
2 |
|
|
110
|
|
|
$mappedByNamespaceValues = []; |
111
|
2 |
|
|
112
|
|
|
foreach ($values as $key => $value) { |
113
|
|
|
$mappedByNamespaceValues[$this->buildKeyName($key)] = $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$ttl = is_null($ttl) ? $this->defaultLifetime : $ttl; |
117
|
2 |
|
|
118
|
|
|
$result = apcu_store($mappedByNamespaceValues, (int)$ttl); |
119
|
2 |
|
|
120
|
2 |
|
return $result === true ? true : (is_array($result) && count($result) == 0 ? true : false); |
121
|
|
|
} |
122
|
2 |
|
|
123
|
|
|
/** |
124
|
2 |
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function deleteMultiple($keys) |
127
|
|
|
{ |
128
|
|
|
$keys = $this->convertKeysToArray($keys); |
129
|
|
|
$this->assertKeyNames($keys); |
130
|
4 |
|
$keys = $this->buildKeyNames($keys); |
131
|
|
|
|
132
|
4 |
|
$result = apcu_delete($keys); |
133
|
4 |
|
|
134
|
|
|
return count($result) === count($keys) ? false : true; |
135
|
4 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function has($key) |
141
|
|
|
{ |
142
|
|
|
$this->assertKeyName($key); |
143
|
10 |
|
$key = $this->buildKeyName($key); |
144
|
|
|
|
145
|
10 |
|
return (bool)apcu_exists($key); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $key |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
2 |
|
private function buildKeyName($key) |
154
|
|
|
{ |
155
|
|
|
return $this->namespace . $key; |
156
|
2 |
|
} |
157
|
2 |
|
|
158
|
|
|
/** |
159
|
|
|
* @param string[] $keys |
160
|
|
|
* |
161
|
|
|
* @return string[] |
162
|
|
|
* |
163
|
|
|
* @throws ApcuInvalidCacheKeysException |
164
|
|
|
*/ |
165
|
|
|
private function buildKeyNames($keys) |
166
|
20 |
|
{ |
167
|
|
|
return array_map(function ($key) { |
168
|
20 |
|
return $this->buildKeyName($key); |
169
|
10 |
|
}, $keys); |
170
|
|
|
} |
171
|
10 |
|
|
172
|
|
|
/** |
173
|
|
|
* @param mixed $key |
174
|
|
|
* |
175
|
|
|
* @throws ApcuInvalidCacheKeyException |
176
|
|
|
*/ |
177
|
|
|
private function assertKeyName($key) |
178
|
|
|
{ |
179
|
|
|
if (!is_scalar($key) || is_bool($key)) { |
180
|
2 |
|
throw new ApcuInvalidCacheKeyException(); |
181
|
2 |
|
} |
182
|
2 |
|
} |
183
|
2 |
|
|
184
|
|
|
/** |
185
|
|
|
* @param string[] $keys |
186
|
|
|
* |
187
|
|
|
* @throws ApcuInvalidCacheKeyException |
188
|
|
|
*/ |
189
|
|
|
private function assertKeyNames(array $keys) |
190
|
|
|
{ |
191
|
|
|
array_map(function ($value) { |
192
|
|
|
$this->assertKeyName($value); |
193
|
|
|
}, $keys); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string[]|object|\Traversable $keys |
198
|
|
|
* |
199
|
|
|
* @throws ApcuInvalidCacheKeysException |
200
|
|
|
*/ |
201
|
|
|
private function convertKeysToArray($keys) |
202
|
|
|
{ |
203
|
|
|
if (!$this->isIterableKeys($keys)) { |
204
|
|
|
throw new ApcuInvalidCacheKeysException(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return is_array($keys) ? $keys : iterator_to_array($keys); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string[]|object|\Traversable $keys |
212
|
|
|
* |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
private function isIterableKeys($keys) |
216
|
|
|
{ |
217
|
|
|
if (function_exists('is_iterable')) { |
218
|
|
|
return is_iterable($keys); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return is_array($keys) || (is_object($keys) && ($keys instanceof \Traversable)); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|