1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Cache; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use Traversable; |
9
|
|
|
use Yiisoft\Cache\Exception\InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
use function array_fill_keys; |
12
|
|
|
use function array_keys; |
13
|
|
|
use function array_map; |
14
|
|
|
use function is_string; |
15
|
|
|
use function iterator_to_array; |
16
|
|
|
use function strpbrk; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* NullCache does not cache anything reporting success for all methods calls. |
20
|
|
|
* |
21
|
|
|
* By replacing it with some other cache component, one can quickly switch from non-caching mode to caching mode. |
22
|
|
|
* |
23
|
|
|
* See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that NullCache supports. |
24
|
|
|
*/ |
25
|
|
|
final class NullCache implements \Psr\SimpleCache\CacheInterface |
26
|
|
|
{ |
27
|
3 |
|
public function get(string $key, mixed $default = null): mixed |
28
|
|
|
{ |
29
|
3 |
|
$this->validateKey($key); |
30
|
2 |
|
return $default; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool |
34
|
|
|
{ |
35
|
2 |
|
$this->validateKey($key); |
36
|
2 |
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
public function delete($key): bool |
40
|
|
|
{ |
41
|
2 |
|
$this->validateKey($key); |
42
|
1 |
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function clear(): bool |
46
|
|
|
{ |
47
|
1 |
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function getMultiple(iterable $keys, mixed $default = null): iterable |
51
|
|
|
{ |
52
|
1 |
|
$keys = $this->iterableToArray($keys); |
53
|
|
|
/** @psalm-suppress RedundantCondition */ |
54
|
1 |
|
$this->validateKeys($keys); |
55
|
1 |
|
return array_fill_keys($keys, $default); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool |
59
|
|
|
{ |
60
|
1 |
|
$values = $this->iterableToArray($values); |
61
|
1 |
|
$this->validateKeysOfValues($values); |
62
|
1 |
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function deleteMultiple(iterable $keys): bool |
66
|
|
|
{ |
67
|
1 |
|
$keys = $this->iterableToArray($keys); |
68
|
|
|
/** @psalm-suppress RedundantCondition */ |
69
|
1 |
|
$this->validateKeys($keys); |
70
|
1 |
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function has(string $key): bool |
74
|
|
|
{ |
75
|
2 |
|
$this->validateKey($key); |
76
|
1 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Converts iterable to array. |
81
|
|
|
* |
82
|
|
|
* @psalm-template T |
83
|
|
|
* @psalm-param iterable<T> $iterable |
84
|
|
|
* @psalm-return array<array-key,T> |
85
|
|
|
*/ |
86
|
3 |
|
private function iterableToArray(iterable $iterable): array |
87
|
|
|
{ |
88
|
3 |
|
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
11 |
|
private function validateKey(mixed $key): void |
92
|
|
|
{ |
93
|
11 |
|
if (!is_string($key) || $key === '' || strpbrk($key, '{}()/\@:')) { |
94
|
3 |
|
throw new InvalidArgumentException('Invalid key value.'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @psalm-assert string[] $keys |
100
|
|
|
*/ |
101
|
3 |
|
private function validateKeys(array $keys): void |
102
|
|
|
{ |
103
|
|
|
foreach ($keys as $key) { |
104
|
3 |
|
$this->validateKey($key); |
105
|
3 |
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function validateKeysOfValues(array $values): void |
109
|
1 |
|
{ |
110
|
|
|
$keys = array_map('\strval', array_keys($values)); |
111
|
1 |
|
$this->validateKeys($keys); |
112
|
1 |
|
} |
113
|
|
|
} |
114
|
|
|
|