|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Cache; |
|
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
|
|
|
private const TTL_EXPIRED = -1; |
|
21
|
|
|
|
|
22
|
121 |
|
public function get($key, $default = null) |
|
23
|
|
|
{ |
|
24
|
121 |
|
$value = \apcu_fetch($key, $success); |
|
25
|
121 |
|
return $success ? $value : $default; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
137 |
|
public function set($key, $value, $ttl = null): bool |
|
29
|
|
|
{ |
|
30
|
137 |
|
$ttl = $this->normalizeTtl($ttl); |
|
31
|
137 |
|
if ($ttl < 0) { |
|
32
|
2 |
|
return $this->delete($key); |
|
33
|
|
|
} |
|
34
|
135 |
|
return \apcu_store($key, $value, $ttl); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
22 |
|
public function delete($key): bool |
|
38
|
|
|
{ |
|
39
|
22 |
|
return \apcu_delete($key); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
156 |
|
public function clear(): bool |
|
43
|
|
|
{ |
|
44
|
156 |
|
return \apcu_clear_cache(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
20 |
|
public function getMultiple($keys, $default = null): iterable |
|
48
|
|
|
{ |
|
49
|
20 |
|
$values = \apcu_fetch($this->iterableToArray($keys), $success) ?: []; |
|
50
|
20 |
|
return array_merge(array_fill_keys($this->iterableToArray($keys), $default), $values); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
26 |
|
public function setMultiple($values, $ttl = null): bool |
|
54
|
|
|
{ |
|
55
|
26 |
|
return \apcu_store($this->iterableToArray($values), null, $this->normalizeTtl($ttl)) === []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function deleteMultiple($keys): bool |
|
59
|
|
|
{ |
|
60
|
2 |
|
return \apcu_delete($this->iterableToArray($keys)) === []; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
29 |
|
public function has($key): bool |
|
64
|
|
|
{ |
|
65
|
29 |
|
return \apcu_exists($key); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @noinspection PhpDocMissingThrowsInspection DateTime won't throw exception because constant string is passed as time |
|
70
|
|
|
* |
|
71
|
|
|
* Normalizes cache TTL handling `null` value and {@see DateInterval} objects. |
|
72
|
|
|
* @param int|DateInterval|null $ttl raw TTL. |
|
73
|
|
|
* @return int|null TTL value as UNIX timestamp or null meaning infinity |
|
74
|
|
|
*/ |
|
75
|
165 |
|
private function normalizeTtl($ttl): ?int |
|
76
|
|
|
{ |
|
77
|
165 |
|
$normalizedTtl = $ttl; |
|
78
|
165 |
|
if ($ttl instanceof DateInterval) { |
|
79
|
5 |
|
$normalizedTtl = (new DateTime('@0'))->add($ttl)->getTimestamp(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
165 |
|
return $normalizedTtl ?? static::TTL_INFINITY; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Converts iterable to array |
|
87
|
|
|
* @param iterable $iterable |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
26 |
|
private function iterableToArray(iterable $iterable): array |
|
91
|
|
|
{ |
|
92
|
26 |
|
return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: