1 | <?php |
||
2 | |||
3 | namespace Vectorface\Cache; |
||
4 | |||
5 | use DateInterval; |
||
6 | use Vectorface\Cache\Common\PSR16Util; |
||
7 | use Vectorface\Cache\Common\MultipleTrait; |
||
8 | |||
9 | /** |
||
10 | * A cache implementation using an internal PHP associative array. |
||
11 | * |
||
12 | * This cache is very fast, but volatile: cache is only maintained while the PHP interpreter is running. |
||
13 | * Usually, this means one HTTP request. |
||
14 | * |
||
15 | * Capable of a huge number of requests/second |
||
16 | */ |
||
17 | class PHPCache implements Cache, AtomicCounter |
||
18 | { |
||
19 | use MultipleTrait, PSR16Util; |
||
20 | |||
21 | /** |
||
22 | * The "cache" which stores entries for the lifetime of the request. |
||
23 | * |
||
24 | * Each entry is an [expiry, value] pair, where expiry is a timestamp. |
||
25 | * |
||
26 | * @var array{int, mixed}[] |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
27 | */ |
||
28 | protected array $cache = []; |
||
29 | |||
30 | /** |
||
31 | * @inheritDoc |
||
32 | 22 | */ |
|
33 | public function get(string $key, mixed $default = null) : mixed |
||
34 | 22 | { |
|
35 | 20 | $key = $this->key($key); |
|
36 | 11 | if (isset($this->cache[$key])) { |
|
37 | 11 | list($expires, $value) = $this->cache[$key]; |
|
38 | 10 | if (!$expires || ($expires >= microtime(true))) { |
|
39 | return $value; |
||
40 | 1 | } |
|
41 | unset($this->cache[$key]); |
||
42 | 20 | } |
|
43 | return $default; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @inheritDoc |
||
48 | 22 | */ |
|
49 | public function set(string $key, mixed $value, DateInterval|int|null $ttl = null) : bool |
||
50 | { |
||
51 | 22 | /* Cache gets a microtime expiry date. */ |
|
52 | 22 | $ttl = $this->ttl($ttl); |
|
53 | 21 | $this->cache[$this->key($key)] = [ |
|
54 | 21 | $ttl ? ((int)$ttl + microtime(true)) : false, |
|
55 | $value |
||
56 | 21 | ]; |
|
57 | return true; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @inheritDoc |
||
62 | 6 | */ |
|
63 | public function delete(string $key) : bool |
||
64 | 6 | { |
|
65 | 6 | unset($this->cache[$this->key($key)]); |
|
66 | return true; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @inheritDoc |
||
71 | 3 | */ |
|
72 | public function clean() : bool |
||
73 | 3 | { |
|
74 | 2 | foreach ($this->cache as $key => $value) { |
|
75 | 2 | list($expires) = $value; |
|
76 | 1 | if ($expires && ($expires < microtime(true))) { |
|
77 | unset($this->cache[$key]); |
||
78 | } |
||
79 | 3 | } |
|
80 | return true; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @inheritDoc |
||
85 | 7 | */ |
|
86 | public function flush() : bool |
||
87 | 7 | { |
|
88 | 7 | $this->cache = []; |
|
89 | return true; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @inheritDoc |
||
94 | 3 | */ |
|
95 | public function clear() : bool |
||
96 | 3 | { |
|
97 | return $this->flush(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @inheritDoc |
||
102 | 2 | */ |
|
103 | public function has(string $key) : bool |
||
104 | 2 | { |
|
105 | return $this->get($this->key($key)) !== null; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @inheritDoc |
||
110 | 2 | */ |
|
111 | public function increment(string $key, int $step = 1, DateInterval|int|null $ttl = null) : int|false |
||
112 | 2 | { |
|
113 | 2 | $key = $this->key($key); |
|
114 | 2 | $exists = $this->has($key); |
|
115 | 2 | $newValue = $this->get($key, 0) + $this->step($step); |
|
116 | $this->set($key, $newValue, (!$exists ? $ttl : null)); |
||
117 | return $newValue; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | 2 | * @inheritDoc |
|
122 | */ |
||
123 | 2 | public function decrement(string $key, int $step = 1, DateInterval|int|null $ttl = null) : int|false |
|
124 | 2 | { |
|
125 | 2 | return $this->increment($key, -$step, $ttl); |
|
126 | 2 | } |
|
127 | } |
||
128 |