1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Cache\Apcu; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ApcuCache provides APCu caching in terms of an application component. |
11
|
|
|
* |
12
|
|
|
* To use this application component, the [APCu PHP extension](http://www.php.net/apcu) must be loaded. |
13
|
|
|
* In order to enable APCu for CLI you should add "apc.enable_cli = 1" to your php.ini. |
14
|
|
|
* |
15
|
|
|
* See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ApcCache supports. |
16
|
|
|
*/ |
17
|
|
|
class ApcuCache implements CacheInterface |
18
|
|
|
{ |
19
|
|
|
private const TTL_INFINITY = 0; |
20
|
|
|
|
21
|
54 |
|
public function get($key, $default = null) |
22
|
|
|
{ |
23
|
54 |
|
$value = \apcu_fetch($key, $success); |
24
|
54 |
|
return $success ? $value : $default; |
25
|
|
|
} |
26
|
|
|
|
27
|
62 |
|
public function set($key, $value, $ttl = null): bool |
28
|
|
|
{ |
29
|
62 |
|
$ttl = $this->normalizeTtl($ttl); |
30
|
62 |
|
if ($ttl < 0) { |
31
|
1 |
|
return $this->delete($key); |
32
|
|
|
} |
33
|
61 |
|
return \apcu_store($key, $value, $ttl); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
11 |
|
public function delete($key): bool |
37
|
|
|
{ |
38
|
11 |
|
return \apcu_delete($key); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
71 |
|
public function clear(): bool |
42
|
|
|
{ |
43
|
71 |
|
return \apcu_clear_cache(); |
44
|
|
|
} |
45
|
|
|
|
46
|
7 |
|
public function getMultiple($keys, $default = null): iterable |
47
|
|
|
{ |
48
|
7 |
|
$values = \apcu_fetch($this->iterableToArray($keys), $success) ?: []; |
49
|
7 |
|
return array_merge(array_fill_keys($this->iterableToArray($keys), $default), $values); |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
public function setMultiple($values, $ttl = null): bool |
53
|
|
|
{ |
54
|
10 |
|
return \apcu_store($this->iterableToArray($values), null, $this->normalizeTtl($ttl)) === []; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function deleteMultiple($keys): bool |
58
|
|
|
{ |
59
|
1 |
|
return \apcu_delete($this->iterableToArray($keys)) === []; |
60
|
|
|
} |
61
|
|
|
|
62
|
11 |
|
public function has($key): bool |
63
|
|
|
{ |
64
|
11 |
|
return \apcu_exists($key); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @noinspection PhpDocMissingThrowsInspection DateTime won't throw exception because constant string is passed as time |
69
|
|
|
* |
70
|
|
|
* Normalizes cache TTL handling `null` value, strings and {@see DateInterval} objects. |
71
|
|
|
* @param int|string|DateInterval|null $ttl raw TTL. |
72
|
|
|
* @return int TTL value as UNIX timestamp |
73
|
|
|
*/ |
74
|
77 |
|
private function normalizeTtl($ttl): ?int |
75
|
|
|
{ |
76
|
77 |
|
$normalizedTtl = $ttl; |
77
|
77 |
|
if ($ttl instanceof DateInterval) { |
78
|
3 |
|
$normalizedTtl = (new DateTime('@0'))->add($ttl)->getTimestamp(); |
79
|
|
|
} |
80
|
|
|
|
81
|
77 |
|
if (is_string($normalizedTtl)) { |
82
|
2 |
|
$normalizedTtl = (int)$normalizedTtl; |
83
|
|
|
} |
84
|
|
|
|
85
|
77 |
|
return $normalizedTtl ?? static::TTL_INFINITY; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Converts iterable to array |
90
|
|
|
* @param iterable $iterable |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
10 |
|
private function iterableToArray(iterable $iterable): array |
94
|
|
|
{ |
95
|
10 |
|
return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|