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