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
|
|
|
|
19
|
|
|
trait ExtendedCacheItemPoolTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param array $keys |
23
|
|
|
* An indexed array of keys of items to retrieve. |
24
|
|
|
* @param int $option json_encode() options |
25
|
|
|
* @param int $depth json_encode() depth |
26
|
|
|
* @return string |
27
|
|
|
* @throws \InvalidArgumentException |
28
|
|
|
*/ |
29
|
|
|
public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512) |
30
|
|
|
{ |
31
|
|
|
return json_encode(array_values($this->getItems($keys)), $option, $depth); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $tagName |
36
|
|
|
* @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
37
|
|
|
* @throws InvalidArgumentException |
38
|
|
|
*/ |
39
|
|
|
public function getItemsByTag($tagName) |
40
|
|
|
{ |
41
|
|
|
if (is_string($tagName)) { |
42
|
|
|
$driverResponse = $this->driverRead($this->getTagKey($tagName)); |
43
|
|
|
if ($driverResponse) { |
44
|
|
|
$items = (array) $this->driverUnwrapData($driverResponse); |
45
|
|
|
|
46
|
|
|
return $this->getItems(array_unique(array_keys($items))); |
|
|
|
|
47
|
|
|
} else { |
48
|
|
|
return []; |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $tagNames |
57
|
|
|
* @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
58
|
|
|
* @throws InvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
public function getItemsByTags(array $tagNames) |
61
|
|
|
{ |
62
|
|
|
$items = []; |
63
|
|
|
foreach (array_unique($tagNames) as $tagName) { |
64
|
|
|
$items = array_merge($items, $this->getItemsByTag($tagName)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $items; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns A json string that represents an array of items by tags-based. |
72
|
|
|
* |
73
|
|
|
* @param array $tagNames |
74
|
|
|
* An indexed array of keys of items to retrieve. |
75
|
|
|
* @param int $option json_encode() options |
76
|
|
|
* @param int $depth json_encode() depth |
77
|
|
|
* |
78
|
|
|
* @throws InvalidArgumentException |
79
|
|
|
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
80
|
|
|
* MUST be thrown. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512) |
85
|
|
|
{ |
86
|
|
|
return json_encode(array_values($this->getItemsByTags($tagNames)), $option, $depth); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $tagName |
91
|
|
|
* @return bool|null |
92
|
|
|
* @throws InvalidArgumentException |
93
|
|
|
*/ |
94
|
|
|
public function deleteItemsByTag($tagName) |
95
|
|
|
{ |
96
|
|
|
if (is_string($tagName)) { |
97
|
|
|
$return = null; |
98
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
99
|
|
|
$result = $this->driverDelete($item); |
100
|
|
|
if ($return !== false) { |
101
|
|
|
$return = $result; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $return; |
106
|
|
|
} else { |
107
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $tagNames |
113
|
|
|
* @return bool|null |
114
|
|
|
* @throws InvalidArgumentException |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function deleteItemsByTags(array $tagNames) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$return = null; |
119
|
|
|
foreach ($tagNames as $tagName) { |
120
|
|
|
$result = $this->deleteItemsByTag($tagName); |
121
|
|
|
if ($return !== false) { |
122
|
|
|
$return = $result; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function incrementItemsByTag($tagName, $step = 1) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
if (is_string($tagName) && is_int($step)) { |
135
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
136
|
|
|
$item->increment($step); |
137
|
|
|
$this->saveDeferred($item); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->commit(); |
141
|
|
|
} else { |
142
|
|
|
throw new InvalidArgumentException('$tagName must be a string and $step an integer'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritdoc |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function incrementItemsByTags(array $tagNames, $step = 1) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$return = null; |
152
|
|
|
foreach ($tagNames as $tagName) { |
153
|
|
|
$result = $this->incrementItemsByTag($tagName, $step); |
154
|
|
|
if ($return !== false) { |
155
|
|
|
$return = $result; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function decrementItemsByTag($tagName, $step = 1) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
if (is_string($tagName) && is_int($step)) { |
168
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
169
|
|
|
$item->decrement($step); |
170
|
|
|
$this->saveDeferred($item); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->commit(); |
174
|
|
|
} else { |
175
|
|
|
throw new InvalidArgumentException('$tagName must be a string and $step an integer'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritdoc |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function decrementItemsByTags(array $tagNames, $step = 1) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$return = null; |
185
|
|
|
foreach ($tagNames as $tagName) { |
186
|
|
|
$result = $this->decrementItemsByTag($tagName, $step); |
187
|
|
|
if ($return !== false) { |
188
|
|
|
$return = $result; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $return; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @inheritdoc |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function appendItemsByTag($tagName, $data) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
if (is_string($tagName)) { |
201
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
202
|
|
|
$item->append($data); |
203
|
|
|
$this->saveDeferred($item); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this->commit(); |
207
|
|
|
} else { |
208
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritdoc |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function appendItemsByTags(array $tagNames, $data) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$return = null; |
218
|
|
|
foreach ($tagNames as $tagName) { |
219
|
|
|
$result = $this->decrementItemsByTag($tagName, $data); |
220
|
|
|
if ($return !== false) { |
221
|
|
|
$return = $result; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $return; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritdoc |
230
|
|
|
*/ |
231
|
|
View Code Duplication |
public function prependItemsByTag($tagName, $data) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
if (is_string($tagName)) { |
234
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
235
|
|
|
$item->prepend($data); |
236
|
|
|
$this->saveDeferred($item); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this->commit(); |
240
|
|
|
} else { |
241
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @inheritdoc |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
public function prependItemsByTags(array $tagNames, $data) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$return = null; |
251
|
|
|
foreach ($tagNames as $tagName) { |
252
|
|
|
$result = $this->decrementItemsByTag($tagName, $data); |
253
|
|
|
if ($return !== false) { |
254
|
|
|
$return = $result; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $return; |
259
|
|
|
} |
260
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.