|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sf4\Api\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemInterface; |
|
6
|
|
|
use Psr\Cache\InvalidArgumentException; |
|
7
|
|
|
use Symfony\Component\Cache\Adapter\RedisAdapter; |
|
8
|
|
|
use Symfony\Component\Cache\Adapter\TagAwareAdapter; |
|
9
|
|
|
use Symfony\Contracts\Cache\CallbackInterface; |
|
10
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
|
11
|
|
|
|
|
12
|
|
|
class TagAwareCacheAdapter implements CacheAdapterInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected $cache; |
|
15
|
|
|
|
|
16
|
|
|
public function init(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$redisConnection = RedisAdapter::createConnection( |
|
19
|
|
|
'redis://localhost' |
|
20
|
|
|
); |
|
21
|
|
|
$redisAdapter = new RedisAdapter( |
|
22
|
|
|
$redisConnection, |
|
23
|
|
|
'', |
|
24
|
|
|
0 |
|
25
|
|
|
); |
|
26
|
|
|
$this->cache = new TagAwareAdapter( |
|
27
|
|
|
$redisAdapter, |
|
28
|
|
|
$redisAdapter |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns a Cache Item representing the specified key. |
|
34
|
|
|
* |
|
35
|
|
|
* This method must always return a ItemInterface object, even in case of |
|
36
|
|
|
* a cache miss. It MUST NOT return null. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $key |
|
39
|
|
|
* The key for which to return the corresponding Cache Item. |
|
40
|
|
|
* |
|
41
|
|
|
* @throws InvalidArgumentException |
|
42
|
|
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
|
43
|
|
|
* MUST be thrown. |
|
44
|
|
|
* |
|
45
|
|
|
* @return ItemInterface |
|
46
|
|
|
* The corresponding Cache Item. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getItem($key): ?ItemInterface |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->getCache()->getItem($key); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return TagAwareAdapter |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getCache(): TagAwareAdapter |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->cache; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns a traversable set of cache items. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string[] $keys |
|
65
|
|
|
* An indexed array of keys of items to retrieve. |
|
66
|
|
|
* |
|
67
|
|
|
* @throws InvalidArgumentException |
|
68
|
|
|
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
|
69
|
|
|
* MUST be thrown. |
|
70
|
|
|
* |
|
71
|
|
|
* @return array|\Traversable |
|
72
|
|
|
* A traversable collection of Cache Items keyed by the cache keys of |
|
73
|
|
|
* each item. A Cache item will be returned for each key, even if that |
|
74
|
|
|
* key is not found. However, if no keys are specified then an empty |
|
75
|
|
|
* traversable MUST be returned instead. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getItems(array $keys = array()) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getCache()->getItems($keys); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Confirms if the cache contains specified cache item. |
|
84
|
|
|
* |
|
85
|
|
|
* Note: This method MAY avoid retrieving the cached value for performance reasons. |
|
86
|
|
|
* This could result in a race condition with ItemInterface::get(). To avoid |
|
87
|
|
|
* such situation use ItemInterface::isHit() instead. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $key |
|
90
|
|
|
* The key for which to check existence. |
|
91
|
|
|
* |
|
92
|
|
|
* @throws InvalidArgumentException |
|
93
|
|
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
|
94
|
|
|
* MUST be thrown. |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
* True if item exists in the cache, false otherwise. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function hasItem($key): bool |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->getCache()->hasItem($key); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Deletes all items in the pool. |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
* True if the pool was successfully cleared. False if there was an error. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function clear(): bool |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->getCache()->clear(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Removes the item from the pool. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $key |
|
119
|
|
|
* The key to delete. |
|
120
|
|
|
* |
|
121
|
|
|
* @throws InvalidArgumentException |
|
122
|
|
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
|
123
|
|
|
* MUST be thrown. |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
* True if the item was successfully removed. False if there was an error. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function deleteItem($key): bool |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->getCache()->deleteItem($key); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Removes multiple items from the pool. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string[] $keys |
|
137
|
|
|
* An array of keys that should be removed from the pool. |
|
138
|
|
|
* @throws InvalidArgumentException |
|
139
|
|
|
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
|
140
|
|
|
* MUST be thrown. |
|
141
|
|
|
* |
|
142
|
|
|
* @return bool |
|
143
|
|
|
* True if the items were successfully removed. False if there was an error. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function deleteItems(array $keys): bool |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->getCache()->deleteItems($keys); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Persists a cache item immediately. |
|
152
|
|
|
* |
|
153
|
|
|
* @param CacheItemInterface $item |
|
154
|
|
|
* The cache item to save. |
|
155
|
|
|
* |
|
156
|
|
|
* @return bool |
|
157
|
|
|
* True if the item was successfully persisted. False if there was an error. |
|
158
|
|
|
*/ |
|
159
|
|
|
public function save(CacheItemInterface $item): bool |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->getCache()->save($item); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Sets a cache item to be persisted later. |
|
166
|
|
|
* |
|
167
|
|
|
* @param CacheItemInterface $item |
|
168
|
|
|
* The cache item to save. |
|
169
|
|
|
* |
|
170
|
|
|
* @return bool |
|
171
|
|
|
* False if the item could not be queued or if a commit was attempted and failed. True otherwise. |
|
172
|
|
|
*/ |
|
173
|
|
|
public function saveDeferred(CacheItemInterface $item): bool |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->getCache()->saveDeferred($item); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Persists any deferred cache items. |
|
180
|
|
|
* |
|
181
|
|
|
* @return bool |
|
182
|
|
|
* True if all not-yet-saved items were successfully saved or there were none. False otherwise. |
|
183
|
|
|
*/ |
|
184
|
|
|
public function commit(): bool |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->getCache()->commit(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Fetches a value from the pool or computes it if not found. |
|
191
|
|
|
* |
|
192
|
|
|
* On cache misses, a callback is called that should return the missing value. |
|
193
|
|
|
* This callback is given a PSR-6 ItemInterface instance corresponding to the |
|
194
|
|
|
* requested key, that could be used e.g. for expiration control. It could also |
|
195
|
|
|
* be an ItemInterface instance when its additional features are needed. |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $key The key of the item to retrieve from the cache |
|
198
|
|
|
* @param callable|CallbackInterface $callback Should return the computed value for the given key/item |
|
199
|
|
|
* @param float|null $beta A float that, as it grows, controls the likeliness of triggering |
|
200
|
|
|
* early expiration. 0 disables it, INF forces immediate expiration. |
|
201
|
|
|
* The default (or providing null) is implementation dependent but should |
|
202
|
|
|
* typically be 1.0, which should provide optimal stampede protection. |
|
203
|
|
|
* See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration |
|
204
|
|
|
* @param array &$metadata The metadata of the cached item {@see ItemInterface::getMetadata()} |
|
205
|
|
|
* |
|
206
|
|
|
* @return mixed The value corresponding to the provided key |
|
207
|
|
|
* |
|
208
|
|
|
*/ |
|
209
|
|
|
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null) |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->getCache()->get($key, $callback, $beta, $metadata); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Removes an item from the pool. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $key The key to delete |
|
218
|
|
|
* |
|
219
|
|
|
* @return bool True if the item was successfully removed, false if there was any error |
|
220
|
|
|
*/ |
|
221
|
|
|
public function delete(string $key): bool |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->getCache()->delete($key); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Invalidates cached items using tags. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string[] $tags An array of tags to invalidate |
|
230
|
|
|
* |
|
231
|
|
|
* @return bool True on success |
|
232
|
|
|
* |
|
233
|
|
|
* @throws InvalidArgumentException When $tags is not valid |
|
234
|
|
|
*/ |
|
235
|
|
|
public function invalidateTags(array $tags): bool |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->getCache()->invalidateTags($tags); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|