1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* For the full copyright and license information, please view the LICENSE |
4
|
|
|
* file that was distributed with this source code. |
5
|
|
|
* |
6
|
|
|
* @author Nikita Vershinin <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
namespace Endeveit\Cache\Drivers; |
10
|
|
|
|
11
|
|
|
use Endeveit\Cache\Abstracts\Common; |
12
|
|
|
use Endeveit\Cache\Exception; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Driver that stores data in Memcached and uses \Memcache. |
16
|
|
|
* This driver was inspired by the TrekkSoft AG Zend_Cache memcached backend. |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/bigwhoop/taggable-zend-memcached-backend |
19
|
|
|
*/ |
20
|
|
|
class Memcache extends Common |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Separator which concatenates tags. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $tagSeparator = '|'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
* |
32
|
|
|
* Additional options: |
33
|
|
|
* "client" => the instance of \Memcache object |
34
|
|
|
* "compress" => boolean value which indicates to enable zlib compression or not |
35
|
|
|
* |
36
|
|
|
* @codeCoverageIgnore |
37
|
|
|
* @param array $options |
38
|
|
|
* @throws \Endeveit\Cache\Exception |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $options = array()) |
41
|
|
|
{ |
42
|
|
|
if (!array_key_exists('client', $options)) { |
43
|
|
|
throw new Exception('You must provide option "client" with \Memcache object'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (array_key_exists('compress', $options) && $options['compress']) { |
47
|
|
|
$options['compress'] = MEMCACHE_COMPRESSED; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
parent::__construct($options); |
51
|
|
|
|
52
|
|
|
$this->validateIdentifier($this->getOption('prefix_id')); |
53
|
|
|
$this->validateIdentifier($this->getOption('prefix_tag')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
* |
59
|
|
|
* @param string $id |
60
|
|
|
* @param integer $value |
61
|
|
|
* @return integer |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function increment($id, $value = 1) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->validateIdentifier($id); |
66
|
|
|
|
67
|
|
|
$id = $this->getPrefixedIdentifier($id); |
68
|
|
|
$raw = $this->doLoadRaw($id); |
69
|
|
|
|
70
|
|
|
if (false !== $raw) { |
71
|
|
|
if ($raw < 0) { |
72
|
|
|
$result = $raw + $value; |
73
|
|
|
$this->doSaveScalar($result, $id); |
74
|
|
|
} else { |
75
|
|
|
$result = $this->getOption('client')->increment($id, $value); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
$result = $value; |
79
|
|
|
|
80
|
|
|
$this->doSaveScalar($value, $id); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
* |
89
|
|
|
* @param string $id |
90
|
|
|
* @param integer $value |
91
|
|
|
* @return integer |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function decrement($id, $value = 1) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->validateIdentifier($id); |
96
|
|
|
|
97
|
|
|
$id = $this->getPrefixedIdentifier($id); |
98
|
|
|
$raw = $this->doLoadRaw($id); |
99
|
|
|
|
100
|
|
|
if (false !== $raw) { |
101
|
|
|
if ($raw < 0) { |
102
|
|
|
$result = $raw - $value; |
103
|
|
|
|
104
|
|
|
$this->doSaveScalar($raw - $value, $id); |
105
|
|
|
} else { |
106
|
|
|
$result = $this->getOption('client')->decrement($id, $value); |
107
|
|
|
} |
108
|
|
|
} else { |
109
|
|
|
$result = -$value; |
110
|
|
|
|
111
|
|
|
$this->doSaveScalar($result, $id); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
* |
120
|
|
|
* @param string $id |
121
|
|
|
* @return mixed|false |
122
|
|
|
*/ |
123
|
|
|
protected function doLoad($id) |
124
|
|
|
{ |
125
|
|
|
$result = $this->getOption('client')->get($id, $this->getOption('compress', 0)); |
126
|
|
|
|
127
|
|
|
return $this->getProcessedLoadedValue($result); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
* |
133
|
|
|
* @param array $identifiers |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
protected function doLoadMany(array $identifiers) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$result = array(); |
139
|
|
|
$now = time(); |
140
|
|
|
|
141
|
|
|
foreach ($this->getOption('client')->get($identifiers, $this->getOption('compress', 0)) as $id => $source) { |
142
|
|
|
$source = $this->getProcessedLoadedValue($source); |
143
|
|
|
|
144
|
|
|
if (false !== $source) { |
145
|
|
|
$i = $this->getIdentifierWithoutPrefix($id); |
146
|
|
|
|
147
|
|
|
if (array_key_exists('expiresAt', $source) && ($source['expiresAt'] < $now)) { |
148
|
|
|
$result[$i] = false; |
149
|
|
|
} else { |
150
|
|
|
$result[$i] = $source['data']; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->fillNotFoundKeys($result, $identifiers); |
156
|
|
|
|
157
|
|
|
return $result; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
* |
163
|
|
|
* @param string $id |
164
|
|
|
* @return mixed|false |
165
|
|
|
*/ |
166
|
|
|
protected function doLoadRaw($id) |
167
|
|
|
{ |
168
|
|
|
return $this->getOption('client')->get($id, $this->getOption('compress', 0)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
* |
174
|
|
|
* @param mixed $data |
175
|
|
|
* @param string $id |
176
|
|
|
* @param array $tags |
177
|
|
|
* @return boolean |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
protected function doSave($data, $id, array $tags = array()) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$this->validateIdentifier($id); |
182
|
|
|
|
183
|
|
|
if (!empty($tags)) { |
184
|
|
|
$this->saveTagsForId($id, $tags); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this->getOption('client')->set($id, $data, $this->getOption('compress', 0)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
* |
193
|
|
|
* @param mixed $data |
194
|
|
|
* @param string $id |
195
|
|
|
* @param integer|boolean $lifetime |
196
|
|
|
* @return boolean |
197
|
|
|
*/ |
198
|
|
|
protected function doSaveScalar($data, $id, $lifetime = false) |
199
|
|
|
{ |
200
|
|
|
return $this->getOption('client')->set($id, $data, $this->getOption('compress', 0), $lifetime); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Remove an items by cache tags. |
205
|
|
|
* |
206
|
|
|
* @param array $tags |
207
|
|
|
* @return boolean |
208
|
|
|
*/ |
209
|
|
|
protected function doRemoveByTags(array $tags) |
210
|
|
|
{ |
211
|
|
|
foreach ($this->getIdsMatchingAnyTags($tags) as $id) { |
212
|
|
|
$this->remove($id); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritdoc} |
220
|
|
|
* |
221
|
|
|
* @return boolean |
222
|
|
|
*/ |
223
|
|
|
protected function doFlush() |
224
|
|
|
{ |
225
|
|
|
return $this->getOption('client')->flush(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Validates cache identifier or a tag, throws an exception in |
230
|
|
|
* case of a problem. |
231
|
|
|
* |
232
|
|
|
* @param string $id |
233
|
|
|
* @throws \Endeveit\Cache\Exception |
234
|
|
|
*/ |
235
|
|
|
protected function validateIdentifier($id) |
236
|
|
|
{ |
237
|
|
|
if (!empty($id) && !preg_match('#^[\S]+$#', $id)) { |
238
|
|
|
throw new Exception(sprintf( |
239
|
|
|
'Identifier "%s" cannot have spaces or be more than 250 characters length.', |
240
|
|
|
$id |
241
|
|
|
)); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Save the tags for identifier. |
247
|
|
|
* |
248
|
|
|
* @param string $id |
249
|
|
|
* @param array $tags |
250
|
|
|
*/ |
251
|
|
|
protected function saveTagsForId($id, array $tags) |
252
|
|
|
{ |
253
|
|
|
foreach ($tags as $tag) { |
254
|
|
|
$idsInTag = $this->getIdentifiersForTag($tag); |
255
|
|
|
|
256
|
|
|
if (!in_array($id, $idsInTag)) { |
257
|
|
|
$idsInTag[] = $id; |
258
|
|
|
|
259
|
|
|
$this->doSaveScalar(implode($this->tagSeparator, $idsInTag), $tag); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Return an array of stored cache ids which match given tags. |
266
|
|
|
* |
267
|
|
|
* @param array $tags |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
|
|
protected function getIdsMatchingAnyTags($tags = array()) |
271
|
|
|
{ |
272
|
|
|
$result = array(); |
273
|
|
|
|
274
|
|
|
foreach ($tags as $tag) { |
275
|
|
|
$result = array_merge($result, $this->getIdentifiersForTag($tag)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return array_unique($result); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Returns list of identifiers for tag. |
283
|
|
|
* |
284
|
|
|
* @param string $tag |
285
|
|
|
* @return array |
286
|
|
|
*/ |
287
|
|
|
protected function getIdentifiersForTag($tag) |
288
|
|
|
{ |
289
|
|
|
$identifiers = $this->doLoadRaw($this->getPrefixedTag($tag)); |
290
|
|
|
|
291
|
|
|
if (empty($identifiers)) { |
292
|
|
|
return array(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return array_map( |
296
|
|
|
array($this, 'getIdentifierWithoutPrefix'), |
297
|
|
|
explode($this->tagSeparator, (string) $identifiers) |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
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.