1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Cache; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use DateTime; |
9
|
|
|
use Yiisoft\Cache\Dependency\Dependency; |
10
|
|
|
use Yiisoft\Cache\Exception\InvalidArgumentException; |
11
|
|
|
use Yiisoft\Cache\Exception\RemoveCacheException; |
12
|
|
|
use Yiisoft\Cache\Exception\SetCacheException; |
13
|
|
|
use Yiisoft\Cache\Metadata\CacheItem; |
14
|
|
|
use Yiisoft\Cache\Metadata\CacheItems; |
15
|
|
|
|
16
|
|
|
use function ctype_alnum; |
17
|
|
|
use function gettype; |
18
|
|
|
use function is_array; |
19
|
|
|
use function is_int; |
20
|
|
|
use function is_string; |
21
|
|
|
use function json_encode; |
22
|
|
|
use function json_last_error_msg; |
23
|
|
|
use function mb_strlen; |
24
|
|
|
use function md5; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Cache provides support for the data caching, including cache key composition and dependencies, and uses |
28
|
|
|
* "Probably early expiration" for cache stampede prevention. The actual data caching is performed via |
29
|
|
|
* {@see Cache::$handler}, which should be configured to be {@see \Psr\SimpleCache\CacheInterface} instance. |
30
|
|
|
* |
31
|
|
|
* @see \Yiisoft\Cache\CacheInterface |
32
|
|
|
*/ |
33
|
|
|
final class Cache implements CacheInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var \Psr\SimpleCache\CacheInterface The actual cache handler. |
37
|
|
|
*/ |
38
|
|
|
private \Psr\SimpleCache\CacheInterface $handler; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var CacheItems The items that store the metadata of each cache. |
42
|
|
|
*/ |
43
|
|
|
private CacheItems $items; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int|null The default TTL for a cache entry. null meaning infinity, negative or zero results in the |
47
|
|
|
* cache key deletion. This value is used by {@see getOrSet()}, if the duration is not explicitly given. |
48
|
|
|
*/ |
49
|
|
|
private ?int $defaultTtl; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \Psr\SimpleCache\CacheInterface $handler The actual cache handler. |
53
|
|
|
* @param DateInterval|int|null $defaultTtl The default TTL for a cache entry. |
54
|
|
|
* null meaning infinity, negative orzero results in the cache key deletion. |
55
|
|
|
* This value is used by {@see getOrSet()}, if the duration is not explicitly given. |
56
|
|
|
*/ |
57
|
57 |
|
public function __construct(\Psr\SimpleCache\CacheInterface $handler, $defaultTtl = null) |
58
|
|
|
{ |
59
|
57 |
|
$this->handler = $handler; |
60
|
57 |
|
$this->items = new CacheItems(); |
61
|
57 |
|
$this->defaultTtl = $this->normalizeTtl($defaultTtl); |
62
|
51 |
|
} |
63
|
|
|
|
64
|
48 |
|
public function getOrSet($key, callable $callable, $ttl = null, Dependency $dependency = null, float $beta = 1.0) |
65
|
|
|
{ |
66
|
48 |
|
$key = $this->normalizeKey($key); |
67
|
47 |
|
$value = $this->getValue($key, $beta); |
68
|
|
|
|
69
|
47 |
|
return $value ?? $this->setAndGet($key, $callable, $ttl, $dependency); |
70
|
|
|
} |
71
|
|
|
|
72
|
15 |
|
public function remove($key): void |
73
|
|
|
{ |
74
|
15 |
|
$key = $this->normalizeKey($key); |
75
|
|
|
|
76
|
14 |
|
if (!$this->handler->delete($key)) { |
77
|
2 |
|
throw new RemoveCacheException($key); |
78
|
|
|
} |
79
|
|
|
|
80
|
12 |
|
$this->items->remove($key); |
81
|
12 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the cache value. |
85
|
|
|
* |
86
|
|
|
* @param string $key The unique key of this item in the cache. |
87
|
|
|
* @param float $beta The value for calculating the range that is used for "Probably early expiration" algorithm. |
88
|
|
|
* |
89
|
|
|
* @return mixed|null The cache value or `null` if the cache is outdated or a dependency has been changed. |
90
|
|
|
*/ |
91
|
47 |
|
private function getValue(string $key, float $beta) |
92
|
|
|
{ |
93
|
47 |
|
if ($this->items->expired($key, $beta, $this->handler)) { |
94
|
2 |
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
47 |
|
$value = $this->handler->get($key); |
98
|
|
|
|
99
|
47 |
|
if (is_array($value) && isset($value[1]) && $value[1] instanceof CacheItem) { |
100
|
5 |
|
[$value, $item] = $value; |
101
|
|
|
|
102
|
5 |
|
if ($item->key() !== $key || $item->expired($beta, $this->handler)) { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
$this->items->set($item); |
107
|
|
|
} |
108
|
|
|
|
109
|
47 |
|
return $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sets the cache value and metadata, and returns the cache value. |
114
|
|
|
* |
115
|
|
|
* @param string $key The unique key of this item in the cache. |
116
|
|
|
* @param callable $callable The callable or closure that will be used to generate a value to be cached. |
117
|
|
|
* @param DateInterval|int|null $ttl The TTL of this value. If not set, default value is used. |
118
|
|
|
* @param Dependency|null $dependency The dependency of the cache value. |
119
|
|
|
* |
120
|
|
|
* @throws InvalidArgumentException Must be thrown if the `$key` or `$ttl` is not a legal value. |
121
|
|
|
* @throws SetCacheException Must be thrown if the data could not be set in the cache. |
122
|
|
|
* |
123
|
|
|
* @return mixed|null The cache value. |
124
|
|
|
*/ |
125
|
47 |
|
private function setAndGet(string $key, callable $callable, $ttl, ?Dependency $dependency) |
126
|
|
|
{ |
127
|
47 |
|
$ttl = $this->normalizeTtl($ttl); |
128
|
41 |
|
$ttl ??= $this->defaultTtl; |
129
|
41 |
|
$value = $callable($this->handler); |
130
|
|
|
|
131
|
41 |
|
if ($dependency !== null) { |
132
|
6 |
|
$dependency->evaluateDependency($this->handler); |
133
|
|
|
} |
134
|
|
|
|
135
|
41 |
|
$item = new CacheItem($key, $ttl, $dependency); |
136
|
|
|
|
137
|
41 |
|
if (!$this->handler->set($key, [$value, $item], $ttl)) { |
138
|
2 |
|
throw new SetCacheException($key, $value, $item); |
139
|
|
|
} |
140
|
|
|
|
141
|
39 |
|
$this->items->set($item); |
142
|
39 |
|
return $value; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Normalizes the cache key from a given key. |
147
|
|
|
* |
148
|
|
|
* If the given key is a string containing alphanumeric characters only and no more than 32 characters, |
149
|
|
|
* then the key will be returned back as it is, integers will be converted to strings. Otherwise, |
150
|
|
|
* a normalized key is generated by serializing the given key and applying MD5 hashing. |
151
|
|
|
* |
152
|
|
|
* @param mixed $key The key to be normalized. |
153
|
|
|
* |
154
|
|
|
* @throws InvalidArgumentException For invalid key. |
155
|
|
|
* |
156
|
|
|
* @return string The normalized cache key. |
157
|
|
|
*/ |
158
|
51 |
|
private function normalizeKey($key): string |
159
|
|
|
{ |
160
|
51 |
|
if (is_string($key) || is_int($key)) { |
161
|
33 |
|
$key = (string) $key; |
162
|
33 |
|
return ctype_alnum($key) && mb_strlen($key, '8bit') <= 32 ? $key : md5($key); |
163
|
|
|
} |
164
|
|
|
|
165
|
18 |
|
if (($key = json_encode($key)) === false) { |
166
|
2 |
|
throw new InvalidArgumentException('Invalid key. ' . json_last_error_msg()); |
167
|
|
|
} |
168
|
|
|
|
169
|
16 |
|
return md5($key); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Normalizes cache TTL handling `null` value and {@see DateInterval} objects. |
174
|
|
|
* |
175
|
|
|
* @param mixed $ttl raw TTL. |
176
|
|
|
* |
177
|
|
|
* @throws InvalidArgumentException For invalid TTL. |
178
|
|
|
* |
179
|
|
|
* @return int|null TTL value as UNIX timestamp or null meaning infinity. |
180
|
|
|
*/ |
181
|
57 |
|
private function normalizeTtl($ttl): ?int |
182
|
|
|
{ |
183
|
57 |
|
if ($ttl === null) { |
184
|
51 |
|
return null; |
185
|
|
|
} |
186
|
|
|
|
187
|
19 |
|
if ($ttl instanceof DateInterval) { |
188
|
2 |
|
return (new DateTime('@0'))->add($ttl)->getTimestamp(); |
189
|
|
|
} |
190
|
|
|
|
191
|
17 |
|
if (is_int($ttl)) { |
192
|
5 |
|
return $ttl; |
193
|
|
|
} |
194
|
|
|
|
195
|
12 |
|
throw new InvalidArgumentException(sprintf( |
196
|
12 |
|
'Invalid TTL "%s" specified. It must be a \DateInterval instance, an integer, or null.', |
197
|
12 |
|
gettype($ttl), |
198
|
|
|
)); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|