1
|
|
|
<?php |
2
|
|
|
namespace Yiisoft\Cache; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ApcuCache provides APCu caching in terms of an application component. |
6
|
|
|
* |
7
|
|
|
* To use this application component, the [APCu PHP extension](http://www.php.net/apcu) must be loaded. |
8
|
|
|
* In order to enable APCu for CLI you should add "apc.enable_cli = 1" to your php.ini. |
9
|
|
|
* |
10
|
|
|
* See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ApcCache supports. |
11
|
|
|
*/ |
12
|
|
|
final class ApcuCache extends SimpleCache |
13
|
|
|
{ |
14
|
|
|
private const TTL_INFINITY = 0; |
15
|
|
|
|
16
|
3 |
|
protected function hasValue(string $key): bool |
17
|
|
|
{ |
18
|
3 |
|
return \apcu_exists($key); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
15 |
|
protected function getValue(string $key, $default = null) |
22
|
|
|
{ |
23
|
15 |
|
$value = \apcu_fetch($key, $success); |
24
|
15 |
|
return $success ? $value : $default; |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
protected function getValues(iterable $keys, $default = null): iterable |
28
|
|
|
{ |
29
|
3 |
|
return \apcu_fetch($keys, $success) ?: []; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
13 |
|
protected function setValue(string $key, $value, ?int $ttl): bool |
33
|
|
|
{ |
34
|
13 |
|
return \apcu_store($key, $value, $ttl ?? self::TTL_INFINITY); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
protected function setValues(iterable $values, ?int $ttl): bool |
38
|
|
|
{ |
39
|
5 |
|
return \apcu_store($values, null, $ttl ?? self::TTL_INFINITY) === []; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
protected function deleteValue(string $key): bool |
43
|
|
|
{ |
44
|
1 |
|
return \apcu_delete($key); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
17 |
|
public function clear(): bool |
48
|
|
|
{ |
49
|
17 |
|
return \apcu_clear_cache(); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function deleteValues(iterable $keys): bool |
53
|
|
|
{ |
54
|
1 |
|
return \apcu_delete($keys) === []; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|