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