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