1 | <?php |
||
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) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 4 | public function clear() |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 2 | public function getMultiple($keys, $default = null) |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 2 | public function setMultiple($values, $ttl = null) |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 2 | public function deleteMultiple($keys) |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 4 | public function has($key) |
|
137 | |||
138 | /** |
||
139 | * @param string $key |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 4 | private function buildKeyName($key) |
|
147 | |||
148 | /** |
||
149 | * @param string[] $keys |
||
150 | * |
||
151 | * @return string[] |
||
152 | */ |
||
153 | 2 | private function buildKeyNames(array $keys) |
|
160 | |||
161 | /** |
||
162 | * @param mixed $key |
||
163 | * |
||
164 | * @throws ApcuInvalidCacheKeyException |
||
165 | */ |
||
166 | 18 | private function assertKeyName($key) |
|
172 | |||
173 | /** |
||
174 | * @param string[] $keys |
||
175 | * |
||
176 | * @throws ApcuInvalidCacheKeyException |
||
177 | */ |
||
178 | private function assertKeyNames(array $keys) |
||
184 | } |
||
185 |