1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache\Core; |
16
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use Psr\Cache\CacheItemInterface; |
19
|
|
|
|
20
|
|
|
trait ExtendedCacheItemPoolTrait |
21
|
|
|
{ |
22
|
|
|
use StandardPsr6StructureTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Deletes all items in the pool. |
26
|
|
|
* @deprecated Use clear() instead |
27
|
|
|
* Will be removed in 5.1 |
28
|
|
|
* |
29
|
|
|
* @return bool |
30
|
|
|
* True if the pool was successfully cleared. False if there was an error. |
31
|
|
|
*/ |
32
|
|
|
public function clean() |
33
|
|
|
{ |
34
|
|
|
trigger_error('Cache clean() method is deprecated, use clear() method instead', E_USER_DEPRECATED); |
35
|
|
|
$this->clear(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $keys |
40
|
|
|
* An indexed array of keys of items to retrieve. |
41
|
|
|
* @param int $option json_encode() options |
42
|
|
|
* @param int $depth json_encode() depth |
43
|
|
|
* @return string |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
*/ |
46
|
|
|
public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512) |
47
|
|
|
{ |
48
|
|
|
$callback = function(CacheItemInterface $item){ |
49
|
|
|
return $item->get(); |
50
|
|
|
}; |
51
|
|
|
return json_encode(array_map($callback, array_values($this->getItems($keys))), $option, $depth); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $tagName |
56
|
|
|
* @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
57
|
|
|
* @throws InvalidArgumentException |
58
|
|
|
*/ |
59
|
|
|
public function getItemsByTag($tagName) |
60
|
|
|
{ |
61
|
|
|
if (is_string($tagName)) { |
62
|
|
|
$driverResponse = $this->getItem($this->getTagKey($tagName)); |
63
|
|
|
if ($driverResponse->isHit()) { |
64
|
|
|
$items = (array) $driverResponse->get(); |
65
|
|
|
|
66
|
|
|
return $this->getItems(array_unique(array_keys($items))); |
67
|
|
|
} else { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
} else { |
71
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $tagNames |
77
|
|
|
* @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
78
|
|
|
* @throws InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
|
public function getItemsByTags(array $tagNames) |
81
|
|
|
{ |
82
|
|
|
$items = []; |
83
|
|
|
foreach (array_unique($tagNames) as $tagName) { |
84
|
|
|
$items = array_merge($items, $this->getItemsByTag($tagName)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $items; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns A json string that represents an array of items by tags-based. |
92
|
|
|
* |
93
|
|
|
* @param array $tagNames |
94
|
|
|
* An indexed array of keys of items to retrieve. |
95
|
|
|
* @param int $option json_encode() options |
96
|
|
|
* @param int $depth json_encode() depth |
97
|
|
|
* |
98
|
|
|
* @throws InvalidArgumentException |
99
|
|
|
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
100
|
|
|
* MUST be thrown. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512) |
105
|
|
|
{ |
106
|
|
|
$callback = function(CacheItemInterface $item){ |
107
|
|
|
return $item->get(); |
108
|
|
|
}; |
109
|
|
|
|
110
|
|
|
return json_encode(array_map($callback, array_values($this->getItemsByTags($tagNames))), $option, $depth); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $tagName |
115
|
|
|
* @return bool|null |
116
|
|
|
* @throws InvalidArgumentException |
117
|
|
|
*/ |
118
|
|
|
public function deleteItemsByTag($tagName) |
119
|
|
|
{ |
120
|
|
|
if (is_string($tagName)) { |
121
|
|
|
$return = null; |
122
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
123
|
|
|
$result = $this->driverDelete($item); |
124
|
|
|
if ($return !== false) { |
125
|
|
|
$return = $result; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $return; |
130
|
|
|
} else { |
131
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param array $tagNames |
137
|
|
|
* @return bool|null |
138
|
|
|
* @throws InvalidArgumentException |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function deleteItemsByTags(array $tagNames) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$return = null; |
143
|
|
|
foreach ($tagNames as $tagName) { |
144
|
|
|
$result = $this->deleteItemsByTag($tagName); |
145
|
|
|
if ($return !== false) { |
146
|
|
|
$return = $result; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
public function incrementItemsByTag($tagName, $step = 1) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
if (is_string($tagName) && is_int($step)) { |
159
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
160
|
|
|
$item->increment($step); |
161
|
|
|
$this->saveDeferred($item); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->commit(); |
165
|
|
|
} else { |
166
|
|
|
throw new InvalidArgumentException('$tagName must be a string and $step an integer'); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
public function incrementItemsByTags(array $tagNames, $step = 1) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$return = null; |
176
|
|
|
foreach ($tagNames as $tagName) { |
177
|
|
|
$result = $this->incrementItemsByTag($tagName, $step); |
178
|
|
|
if ($return !== false) { |
179
|
|
|
$return = $result; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $return; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritdoc |
188
|
|
|
*/ |
189
|
|
View Code Duplication |
public function decrementItemsByTag($tagName, $step = 1) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
if (is_string($tagName) && is_int($step)) { |
192
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
193
|
|
|
$item->decrement($step); |
194
|
|
|
$this->saveDeferred($item); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this->commit(); |
198
|
|
|
} else { |
199
|
|
|
throw new InvalidArgumentException('$tagName must be a string and $step an integer'); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @inheritdoc |
205
|
|
|
*/ |
206
|
|
View Code Duplication |
public function decrementItemsByTags(array $tagNames, $step = 1) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
$return = null; |
209
|
|
|
foreach ($tagNames as $tagName) { |
210
|
|
|
$result = $this->decrementItemsByTag($tagName, $step); |
211
|
|
|
if ($return !== false) { |
212
|
|
|
$return = $result; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $return; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @inheritdoc |
221
|
|
|
*/ |
222
|
|
View Code Duplication |
public function appendItemsByTag($tagName, $data) |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
if (is_string($tagName)) { |
225
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
226
|
|
|
$item->append($data); |
227
|
|
|
$this->saveDeferred($item); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this->commit(); |
231
|
|
|
} else { |
232
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @inheritdoc |
238
|
|
|
*/ |
239
|
|
View Code Duplication |
public function appendItemsByTags(array $tagNames, $data) |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
$return = null; |
242
|
|
|
foreach ($tagNames as $tagName) { |
243
|
|
|
$result = $this->decrementItemsByTag($tagName, $data); |
244
|
|
|
if ($return !== false) { |
245
|
|
|
$return = $result; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $return; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @inheritdoc |
254
|
|
|
*/ |
255
|
|
View Code Duplication |
public function prependItemsByTag($tagName, $data) |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
if (is_string($tagName)) { |
258
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
259
|
|
|
$item->prepend($data); |
260
|
|
|
$this->saveDeferred($item); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $this->commit(); |
264
|
|
|
} else { |
265
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @inheritdoc |
271
|
|
|
*/ |
272
|
|
View Code Duplication |
public function prependItemsByTags(array $tagNames, $data) |
|
|
|
|
273
|
|
|
{ |
274
|
|
|
$return = null; |
275
|
|
|
foreach ($tagNames as $tagName) { |
276
|
|
|
$result = $this->decrementItemsByTag($tagName, $data); |
277
|
|
|
if ($return !== false) { |
278
|
|
|
$return = $result; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $return; |
283
|
|
|
} |
284
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.