1
|
|
|
<?php |
2
|
|
|
namespace Yiisoft\Cache; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ApcCache 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
|
|
|
* Application configuration example: |
11
|
|
|
* |
12
|
|
|
* ```php |
13
|
|
|
* return [ |
14
|
|
|
* 'components' => [ |
15
|
|
|
* 'cache' => [ |
16
|
|
|
* '__class' => Yiisoft\Cache\Cache::class, |
17
|
|
|
* 'handler' => [ |
18
|
|
|
* '__class' => Yiisoft\Cache\ApcCache::class, |
19
|
|
|
* ], |
20
|
|
|
* ], |
21
|
|
|
* // ... |
22
|
|
|
* ], |
23
|
|
|
* // ... |
24
|
|
|
* ]; |
25
|
|
|
* ``` |
26
|
|
|
* |
27
|
|
|
* See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ApcCache supports. |
28
|
|
|
* |
29
|
|
|
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview). |
30
|
|
|
*/ |
31
|
|
|
final class ApcCache extends SimpleCache |
32
|
|
|
{ |
33
|
|
|
private const TTL_INFINITY = 0; |
34
|
|
|
|
35
|
|
|
public function hasValue(string $key): bool |
36
|
|
|
{ |
37
|
|
|
return (bool)\apcu_exists($key); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function getValue(string $key, $default = null) |
41
|
|
|
{ |
42
|
|
|
$value = \apcu_fetch($key, $success); |
43
|
|
|
return $success ? $value : $default; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function getValues(array $keys, $default = null): array |
47
|
|
|
{ |
48
|
|
|
// TODO: test that all keys are returned |
49
|
|
|
return \apcu_fetch($keys, $succses) ?: []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function setValue(string $key, $value, ?int $ttl): bool |
53
|
|
|
{ |
54
|
|
|
return (bool)\apcu_store($key, $value, $ttl ?? self::TTL_INFINITY); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function setValues(array $values, ?int $ttl): bool |
58
|
|
|
{ |
59
|
|
|
return \apcu_store($values, null, $ttl ?? self::TTL_INFINITY) === []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function deleteValue(string $key): bool |
63
|
|
|
{ |
64
|
|
|
return (bool)\apcu_delete($key); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function clear(): bool |
68
|
|
|
{ |
69
|
|
|
return \apcu_clear_cache(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|