1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RemotelyLiving\PHPCacheAdapter\SimpleCache; |
6
|
|
|
|
7
|
|
|
use Psr\SimpleCache; |
8
|
|
|
use RemotelyLiving\PHPCacheAdapter\Assertions; |
9
|
|
|
|
10
|
|
|
abstract class AbstractAdapter implements SimpleCache\CacheInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private ?int $defaultTTLSeconds = null; |
14
|
|
|
|
15
|
|
|
final public function setDefaultTTLSeconds(?int $seconds): self |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
$this->defaultTTLSeconds = $seconds; |
19
|
|
|
|
20
|
|
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
final public function getDefaultTTLSeconds(): ?int |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
return $this->defaultTTLSeconds; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
32
|
|
|
final public function get($key, $default = null) |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** @var \Generator $results */ |
36
|
|
|
$results = $this->getMultiple([$key], $default); |
37
|
|
|
return $results->current(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
*/ |
43
|
|
|
final public function getMultiple($keys, $default = null) |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
Assertions::assertIterable($keys); |
47
|
|
|
$normalizedKeys = $this->iterableToArray($keys); |
48
|
|
|
Assertions::assertValidKeys($normalizedKeys); |
49
|
|
|
|
50
|
|
|
return $this->multiGet($normalizedKeys, $default); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
final public function set($key, $value, $ttl = null): bool |
57
|
|
|
{ |
58
|
|
|
|
59
|
|
|
Assertions::assertValidKey($key); |
60
|
|
|
|
61
|
|
|
return $this->setMultiple([$key => $value], $ttl); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
|
|
final public function setMultiple($values, $ttl = null): bool |
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
Assertions::assertIterable($values); |
71
|
|
|
$normalizedValues = $this->iterableToArray($values); |
72
|
|
|
Assertions::assertValidKeys(array_keys($normalizedValues)); |
73
|
|
|
|
74
|
|
|
return $this->multiSave($normalizedValues, $this->normalizeTTLSeconds($ttl)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
final public function delete($key): bool |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
return $this->deleteMultiple([$key]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
final public function deleteMultiple($keys): bool |
90
|
|
|
{ |
91
|
|
|
|
92
|
|
|
Assertions::assertIterable($keys); |
93
|
|
|
$normalizedKeys = $this->iterableToArray($keys); |
94
|
|
|
Assertions::assertValidKeys($normalizedKeys); |
95
|
|
|
|
96
|
|
|
return $this->multiDelete($normalizedKeys); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
|
|
final public function has($key): bool |
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
Assertions::assertValidKey($key); |
106
|
|
|
return $this->exists($key); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritDoc |
111
|
|
|
*/ |
112
|
|
|
final public function clear(): bool |
113
|
|
|
{ |
114
|
|
|
|
115
|
|
|
return $this->flush(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
abstract protected function flush(): bool; |
119
|
|
|
|
120
|
|
|
abstract protected function exists(string $key): bool; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $keys |
124
|
|
|
* @param mixed $default |
125
|
|
|
* |
126
|
|
|
* @return \Generator |
127
|
|
|
*/ |
128
|
|
|
abstract protected function multiGet(array $keys, $default = null): \Generator; |
129
|
|
|
|
130
|
|
|
abstract protected function multiDelete(array $keys): bool; |
131
|
|
|
|
132
|
|
|
abstract protected function multiSave(array $values, ?int $ttl = null): bool; |
133
|
|
|
|
134
|
|
|
private function iterableToArray(iterable $iterable): array |
135
|
|
|
{ |
136
|
|
|
|
137
|
|
|
$array = []; |
138
|
|
|
foreach ($iterable as $index => $value) { |
139
|
|
|
$array[$index] = $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $array; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param mixed $ttl |
147
|
|
|
* |
148
|
|
|
* @throws \RemotelyLiving\PHPCacheAdapter\Exceptions\InvalidArgument |
149
|
|
|
*/ |
150
|
|
|
private function normalizeTTLSeconds($ttl): ?int |
151
|
|
|
{ |
152
|
|
|
|
153
|
|
|
Assertions::assertValidTTL($ttl); |
154
|
|
|
|
155
|
|
|
if (is_null($ttl)) { |
156
|
|
|
return $this->getDefaultTTLSeconds(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (is_int($ttl)) { |
160
|
|
|
return (int)$ttl; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return max(0, ($ttl->getTimestamp() - time())); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|