|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Cache; |
|
6
|
|
|
|
|
7
|
|
|
use DateInterval; |
|
8
|
|
|
use Psr\SimpleCache\CacheInterface as PsrSimpleCacheInterface; |
|
9
|
|
|
use Yiisoft\Cache\Metadata\CacheItem; |
|
10
|
|
|
|
|
11
|
|
|
use function is_array; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Cache provides support for the data caching, including dependencies. |
|
15
|
|
|
* The actual data caching is performed via {@see DependencyAwareCache::handler()}. |
|
16
|
|
|
* |
|
17
|
|
|
* @internal |
|
18
|
|
|
*/ |
|
19
|
|
|
final class DependencyAwareCache implements PsrSimpleCacheInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param CacheInterface $cache The actual cache. |
|
23
|
|
|
* @param PsrSimpleCacheInterface $handler The actual cache handler. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct( |
|
26
|
|
|
private CacheInterface $cache, |
|
27
|
|
|
private PsrSimpleCacheInterface $handler |
|
28
|
|
|
) { |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function get(string $key, mixed $default = null): mixed |
|
32
|
|
|
{ |
|
33
|
|
|
$value = $this->handler->get($key, $default); |
|
34
|
|
|
return $this->checkAndGetValue($key, $value, $default); |
|
35
|
82 |
|
} |
|
36
|
|
|
|
|
37
|
82 |
|
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool |
|
38
|
82 |
|
{ |
|
39
|
|
|
return $this->handler->set($key, $value, $ttl); |
|
40
|
|
|
} |
|
41
|
9 |
|
|
|
42
|
|
|
public function delete($key): bool |
|
43
|
|
|
{ |
|
44
|
9 |
|
return $this->handler->delete($key); |
|
45
|
7 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function clear(): bool |
|
48
|
45 |
|
{ |
|
49
|
|
|
return $this->handler->clear(); |
|
50
|
45 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getMultiple(iterable $keys, mixed $default = null): iterable |
|
53
|
15 |
|
{ |
|
54
|
|
|
$values = []; |
|
55
|
15 |
|
|
|
56
|
|
|
foreach ($this->handler->getMultiple($keys, $default) as $key => $value) { |
|
57
|
|
|
$values[$key] = $this->checkAndGetValue($key, $value, $default); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return $values; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
23 |
|
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool |
|
64
|
|
|
{ |
|
65
|
23 |
|
return $this->handler->setMultiple($values, $ttl); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
23 |
|
public function deleteMultiple($keys): bool |
|
69
|
|
|
{ |
|
70
|
23 |
|
return $this->handler->deleteMultiple($keys); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
23 |
|
public function has(string $key): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->get($key) !== null; |
|
76
|
23 |
|
} |
|
77
|
|
|
|
|
78
|
23 |
|
/** |
|
79
|
|
|
* Gets the raw cache value. |
|
80
|
|
|
* |
|
81
|
9 |
|
* @param string $key The unique key of this item in the cache. |
|
82
|
|
|
* |
|
83
|
9 |
|
* @return mixed The raw cache value or `null if the cache is outdated. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getRaw(string $key) |
|
86
|
7 |
|
{ |
|
87
|
|
|
return $this->handler->get($key); |
|
88
|
7 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Checks if the cache dependency has expired and returns a value. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $key The unique key of this item in the cache. |
|
94
|
|
|
* @param mixed $value The value of this item in the cache. |
|
95
|
|
|
* @param mixed $default Default value to return if the dependency has been changed. |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed The cache value or `$default` if the dependency has been changed. |
|
98
|
43 |
|
*/ |
|
99
|
|
|
private function checkAndGetValue(string $key, mixed $value, mixed $default = null): mixed |
|
100
|
43 |
|
{ |
|
101
|
|
|
if (is_array($value) && isset($value[1]) && $value[1] instanceof CacheItem) { |
|
102
|
|
|
[$value, $item] = $value; |
|
103
|
|
|
$dependency = $item->dependency(); |
|
104
|
|
|
|
|
105
|
|
|
if ($item->key() !== $key || ($dependency !== null && $dependency->isChanged($this->cache))) { |
|
106
|
|
|
return $default; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $value; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|