1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of CacheTool. |
5
|
|
|
* |
6
|
|
|
* (c) Samuel Gordalina <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CacheTool\Proxy; |
13
|
|
|
|
14
|
|
|
use CacheTool\Adapter\AbstractAdapter; |
15
|
|
|
use CacheTool\Code; |
16
|
|
|
|
17
|
|
|
class ApcuProxy implements ProxyInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var AbstractAdapter |
21
|
|
|
*/ |
22
|
|
|
protected $adapter; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
1 |
|
public function getFunctions() |
28
|
|
|
{ |
29
|
|
|
return array( |
30
|
1 |
|
'apcu_add', |
31
|
1 |
|
'apcu_cache_info', |
32
|
1 |
|
'apcu_regexp_get_keys', |
33
|
1 |
|
'apcu_cas', |
34
|
1 |
|
'apcu_clear_cache', |
35
|
1 |
|
'apcu_dec', |
36
|
1 |
|
'apcu_delete', |
37
|
1 |
|
'apcu_regexp_delete', |
38
|
1 |
|
'apcu_exists', |
39
|
1 |
|
'apcu_fetch', |
40
|
1 |
|
'apcu_inc', |
41
|
|
|
'apcu_sma_info', |
42
|
|
|
'apcu_store', |
43
|
1 |
|
'apcu_version' |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
2 |
|
*/ |
50
|
|
|
public function setAdapter(AbstractAdapter $adapter) |
51
|
2 |
|
{ |
52
|
2 |
|
$this->adapter = $adapter; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Caches a variable in the data store, only if it's not already stored |
57
|
|
|
* |
58
|
|
|
* Note: Unlike many other mechanisms in PHP, variables stored using apcu_add() will persist between requests |
59
|
|
|
* (until the value is removed from the cache). |
60
|
|
|
* |
61
|
|
|
* @since 3.0.13 |
62
|
|
|
* @param mixed $key Store the variable using this name. keys are cache-unique, so attempting to use apc_add() to |
63
|
|
|
* store data with a key that already exists will not overwrite the existing data, and will |
64
|
|
|
* instead return FALSE. (This is the only difference between apcu_add() and apc_store().) |
65
|
|
|
* If $key is an array set Names in key, variables in value |
66
|
|
|
* @param mixed $var The variable to store |
67
|
|
|
* If $key is an array, this parameter is unused and set to NULL |
68
|
|
|
* @param int $ttl Time To Live; store var in the cache for ttl seconds. After the ttl has passed, the stored |
69
|
|
|
* variable will be expunged from the cache (on the next request). If no ttl is supplied |
70
|
|
|
* (or if the ttl is 0), the value will persist until it is removed from the cache manually, |
71
|
|
|
* or otherwise fails to exist in the cache (clear, restart, etc.). |
72
|
|
|
* @return mixed |
73
|
2 |
|
*/ |
74
|
|
|
public function apcu_add($key, $var = null, $ttl = 0) |
75
|
2 |
|
{ |
76
|
1 |
|
if (is_string($key) && $var === null) { |
77
|
|
|
throw new \InvalidArgumentException('When $key is set $var cannot be null'); |
78
|
|
|
} |
79
|
1 |
|
|
80
|
1 |
|
$code = new Code(); |
81
|
1 |
|
$code->addStatement(sprintf( |
82
|
1 |
|
'return apcu_add(%s, %s, %s);', |
83
|
1 |
|
var_export($key, true), |
84
|
1 |
|
var_export($var, true), |
85
|
1 |
|
var_export($ttl, true) |
86
|
|
|
)); |
87
|
1 |
|
|
88
|
|
|
return $this->adapter->run($code); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Updates an old value with a new value |
93
|
|
|
* apcu_cas() updates an already existing integer value if the old parameter matches the currently stored value with the value of the new parameter. |
94
|
|
|
* |
95
|
|
|
* @since 3.1.1 |
96
|
|
|
* @param string $key The key of the value being updated. |
97
|
|
|
* @param int $old The old value (the value currently stored). |
98
|
|
|
* @param int $new The new value to update to |
99
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure. |
100
|
1 |
|
*/ |
101
|
|
|
public function apcu_cas($key, $old, $new) |
102
|
1 |
|
{ |
103
|
1 |
|
$code = new Code(); |
104
|
1 |
|
$code->addStatement(sprintf( |
105
|
1 |
|
'return apcu_cas(%s, %s, %s);', |
106
|
1 |
|
var_export($key, true), |
107
|
1 |
|
var_export($old, true), |
108
|
1 |
|
var_export($new, true) |
109
|
|
|
)); |
110
|
1 |
|
|
111
|
|
|
return $this->adapter->run($code); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retrieves cached information from APCu's data store |
116
|
|
|
* |
117
|
|
|
* @since 2.0.0 |
118
|
|
|
* @param boolean $limited If limited is TRUE, the return value will exclude the individual list of cache |
119
|
|
|
* entries. This is useful when trying to optimize calls for statistics gathering. |
120
|
|
|
* @return boolean Array of cached data (and meta-data) or FALSE on failure |
121
|
1 |
|
*/ |
122
|
|
|
public function apcu_cache_info($limited = false) |
123
|
1 |
|
{ |
124
|
1 |
|
$code = new Code(); |
125
|
1 |
|
$code->addStatement(sprintf( |
126
|
1 |
|
'return apcu_cache_info(%s);', |
127
|
1 |
|
var_export($limited, true) |
128
|
|
|
)); |
129
|
1 |
|
|
130
|
|
|
return $this->adapter->run($code); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Retrieves caches keys & TTL from APCu's data store |
135
|
|
|
* |
136
|
|
|
* @since 3.1.1 |
137
|
|
|
* |
138
|
|
|
* @param null|string $regexp If is regex the return contains keys & ttl of entries found by regex, otherwise - |
139
|
|
|
* keys & ttl of all entries |
140
|
|
|
* |
141
|
|
|
* @return boolean|array Array of cached data's keys and ttl or FALSE on failure |
142
|
|
|
*/ |
143
|
|
|
public function apcu_regexp_get_keys($regexp = null) |
144
|
|
|
{ |
145
|
|
|
if ($regexp && !preg_match("/^\/.+\/[a-z]*$/i", $regexp)) { |
|
|
|
|
146
|
|
|
throw new \RuntimeException("{$regexp} is not a valid Regex"); |
147
|
|
|
} |
148
|
|
|
$code = new Code(); |
149
|
|
|
$code->addStatement(sprintf( |
150
|
|
|
'$keys = new \APCIterator("user", %s, APC_ITER_ALL, 10);', |
151
|
|
|
var_export($regexp, true) |
152
|
|
|
)); |
153
|
|
|
$code->addStatement('$result = [];'); |
154
|
|
|
$code->addStatement(' |
155
|
1 |
|
if ($keys->getTotalCount()){ |
156
|
|
|
foreach ($keys as $key){ |
157
|
1 |
|
$result[] = [ |
158
|
1 |
|
"key" => $key["key"], |
159
|
1 |
|
"ttl" => $key["ttl"] |
160
|
1 |
|
]; |
161
|
1 |
|
} |
162
|
1 |
|
}'); |
163
|
1 |
|
$code->addStatement('return $result;'); |
164
|
1 |
|
return $this->adapter->run($code); |
165
|
|
|
} |
166
|
1 |
|
|
167
|
|
|
/** |
168
|
1 |
|
* Clears the user/system cache |
169
|
1 |
|
* |
170
|
1 |
|
* @since 2.0.0 |
171
|
|
|
* @return boolean Always returns true |
172
|
1 |
|
*/ |
173
|
|
|
public function apcu_clear_cache() |
174
|
|
|
{ |
175
|
|
|
$code = new Code(); |
176
|
|
|
$code->addStatement('return apcu_clear_cache();'); |
177
|
|
|
|
178
|
|
|
return $this->adapter->run($code); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
1 |
|
* Decrease a stored number |
183
|
|
|
* |
184
|
1 |
|
* @since 3.1.1 |
185
|
1 |
|
* @param string $key The key of the value being decreased. |
186
|
1 |
|
* @param int $step The step, or value to decrease. |
187
|
1 |
|
* @param \stdClass $ref success is set to TRUE in success and FALSE in failure |
188
|
1 |
|
* @return mixed Returns the current value of key's value on success, or FALSE on failure |
189
|
|
|
*/ |
190
|
1 |
|
public function apcu_dec($key, $step = 1, $ref = false) |
191
|
|
|
{ |
192
|
|
|
$code = new Code(); |
193
|
|
|
$code->addStatement('$success = false;'); |
194
|
|
|
$code->addStatement(sprintf( |
195
|
|
|
'$result = apcu_dec(%s, %s, $success);', |
196
|
|
|
var_export($key, true), |
197
|
|
|
var_export($step, true) |
198
|
|
|
)); |
199
|
|
|
$code->addStatement('return array($result, $success);'); |
200
|
|
|
|
201
|
1 |
|
list($result, $success) = $this->adapter->run($code); |
202
|
|
|
|
203
|
1 |
|
if (is_object($ref)) { |
204
|
1 |
|
$ref->success = $success; |
205
|
1 |
|
} |
206
|
1 |
|
|
207
|
1 |
|
return $result; |
208
|
|
|
} |
209
|
1 |
|
|
210
|
|
|
/** |
211
|
|
|
* Removes a stored variable from the cache |
212
|
|
|
* |
213
|
|
|
* @since 3.1.1 |
214
|
|
|
* @param mixed $key The key used to store the value (with apcu_store()). |
215
|
|
|
* @return mixed Returns TRUE on success or FALSE on failure. |
216
|
|
|
*/ |
217
|
|
|
public function apcu_delete($key) |
218
|
|
|
{ |
219
|
|
|
$code = new Code(); |
220
|
1 |
|
$code->addStatement(sprintf( |
221
|
|
|
'return apcu_delete(%s);', |
222
|
1 |
|
var_export($key, true) |
223
|
1 |
|
)); |
224
|
1 |
|
|
225
|
1 |
|
return $this->adapter->run($code); |
226
|
|
|
} |
227
|
1 |
|
|
228
|
|
|
/** |
229
|
1 |
|
* Retrieves caches keys & TTL from APCu's data store |
230
|
1 |
|
* |
231
|
1 |
|
* @since 3.1.1 |
232
|
|
|
* |
233
|
1 |
|
* @param null|string $regexp If is regex removes caches with keys found by regex, |
234
|
|
|
* otherwise - removes all entries |
235
|
|
|
* |
236
|
|
|
* @return boolean Return result |
237
|
|
|
*/ |
238
|
|
|
public function apcu_regexp_delete($regexp = null) |
239
|
|
|
{ |
240
|
|
|
if ($regexp && !preg_match("/^\/.+\/[a-z]*$/i", $regexp)) { |
|
|
|
|
241
|
|
|
throw new \RuntimeException("{$regexp} is not a valid Regex"); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$code = new Code(); |
245
|
1 |
|
$code->addStatement(sprintf( |
246
|
|
|
'$keys = new \APCIterator("user", %s, APC_ITER_KEY, 10);', |
247
|
1 |
|
var_export($regexp, true) |
248
|
1 |
|
)); |
249
|
1 |
|
$code->addStatement('return apc_delete($keys);'); |
250
|
1 |
|
return $this->adapter->run($code); |
251
|
1 |
|
} |
252
|
1 |
|
|
253
|
1 |
|
/** |
254
|
1 |
|
* Checks if one or more APCu keys exist. |
255
|
|
|
* |
256
|
1 |
|
* @since 3.1.4 |
257
|
|
|
* @param mixed $keys A string, or an array of strings, that contain keys. |
258
|
1 |
|
* @return mixed Returns TRUE if the key exists, otherwise FALSE Or if an array was passed to keys, then an |
259
|
1 |
|
* array is returned that contains all existing keys, or an empty array if none exist. |
260
|
1 |
|
*/ |
261
|
|
|
public function apcu_exists($keys) |
262
|
1 |
|
{ |
263
|
|
|
$code = new Code(); |
264
|
|
|
$code->addStatement(sprintf( |
265
|
|
|
'return apcu_exists(%s);', |
266
|
|
|
var_export($keys, true) |
267
|
|
|
)); |
268
|
|
|
|
269
|
|
|
return $this->adapter->run($code); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
1 |
|
* Fetch a stored variable from the cache |
274
|
|
|
* |
275
|
1 |
|
* @since 3.0.0 |
276
|
1 |
|
* @param mixed $key The key used to store the value (with apcu_store()). If an array is passed then each element is fetched and returned. |
277
|
1 |
|
* @param \stdClass $ref success is set to TRUE in success and FALSE in failure |
278
|
1 |
|
* @return mixed The stored variable or array of variables on success; FALSE on failure |
279
|
1 |
|
*/ |
280
|
|
|
public function apcu_fetch($key, $ref = false) |
281
|
1 |
|
{ |
282
|
|
|
$code = new Code(); |
283
|
|
|
$code->addStatement('$success = false;'); |
284
|
|
|
$code->addStatement(sprintf('$result = apcu_fetch(%s, $success);', var_export($key, true))); |
285
|
|
|
$code->addStatement('return array($result, $success);'); |
286
|
|
|
|
287
|
|
|
list($var, $success) = $this->adapter->run($code); |
288
|
|
|
|
289
|
|
|
if (is_object($ref)) { |
290
|
|
|
$ref->success = $success; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $var; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Increase a stored number |
298
|
|
|
* |
299
|
|
|
* @since 3.1.1 |
300
|
|
|
* @param string $key The key of the value being increased. |
301
|
|
|
* @param int $step The step, or value to increased. |
302
|
1 |
|
* @param \stdClass $ref success is set to TRUE in success and FALSE in failure |
303
|
|
|
* @return mixed Returns the current value of key's value on success, or FALSE on failure |
304
|
1 |
|
*/ |
305
|
1 |
|
public function apcu_inc($key, $step = 1, $ref = false) |
306
|
1 |
|
{ |
307
|
1 |
|
$code = new Code(); |
308
|
1 |
|
$code->addStatement('$success = false;'); |
309
|
1 |
|
$code->addStatement(sprintf( |
310
|
1 |
|
'$result = apcu_inc(%s, %s, $success);', |
311
|
|
|
var_export($key, true), |
312
|
1 |
|
var_export($step, true) |
313
|
|
|
)); |
314
|
|
|
$code->addStatement('return array($result, $success);'); |
315
|
|
|
|
316
|
|
|
list($result, $success) = $this->adapter->run($code); |
317
|
|
|
|
318
|
1 |
|
if (is_object($ref)) { |
319
|
|
|
$ref->success = $success; |
320
|
1 |
|
} |
321
|
1 |
|
|
322
|
|
|
return $result; |
323
|
1 |
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Retrieves APCu's Shared Memory Allocation information |
327
|
|
|
* |
328
|
|
|
* @since 2.0.0 |
329
|
|
|
* @param boolean $limited When set to FALSE (default) apcu_sma_info() will return a detailed information about |
330
|
|
|
* each segment. |
331
|
|
|
* @return boolean Array of Shared Memory Allocation data; FALSE on failure |
332
|
|
|
*/ |
333
|
|
|
public function apcu_sma_info($limited = false) |
334
|
|
|
{ |
335
|
|
|
$code = new Code(); |
336
|
|
|
$code->addStatement(sprintf( |
337
|
|
|
'return apcu_sma_info(%s);', |
338
|
|
|
var_export($limited, true) |
339
|
|
|
)); |
340
|
|
|
|
341
|
|
|
return $this->adapter->run($code); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Cache a variable in the data store |
346
|
|
|
* |
347
|
|
|
* Note: Unlike many other mechanisms in PHP, variables stored using apcu_store() will persist between requests |
348
|
|
|
* (until the value is removed from the cache). |
349
|
|
|
* |
350
|
|
|
* @since 3.0.0 |
351
|
|
|
* @param mixed $key Store the variable using this name. keys are cache-unique, so storing a second value with the |
352
|
|
|
* same key will overwrite the original value. |
353
|
|
|
* If $key is an array set Names in key, variables in value |
354
|
|
|
* @param mixed $var The variable to store |
355
|
|
|
* If $key is an array, this parameter is unused and set to NULL |
356
|
|
|
* @param int $ttl Time To Live; store var in the cache for ttl seconds. After the ttl has passed, the stored |
357
|
|
|
* variable will be expunged from the cache (on the next request). If no ttl is supplied (or if |
358
|
|
|
* the ttl is 0), the value will persist until it is removed from the cache manually, or |
359
|
|
|
* otherwise fails to exist in the cache (clear, restart, etc.). |
360
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure. Second syntax returns array with error keys. |
361
|
|
|
*/ |
362
|
|
|
public function apcu_store($key, $var = null, $ttl = 0) |
363
|
|
|
{ |
364
|
|
|
$code = new Code(); |
365
|
|
|
$code->addStatement(sprintf( |
366
|
|
|
'return apcu_store(%s, %s, %s);', |
367
|
|
|
var_export($key, true), |
368
|
|
|
var_export($var, true), |
369
|
|
|
var_export($ttl, true) |
370
|
|
|
)); |
371
|
|
|
|
372
|
|
|
return $this->adapter->run($code); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return string |
377
|
|
|
*/ |
378
|
|
|
public function apcu_version() |
379
|
|
|
{ |
380
|
|
|
$code = new Code(); |
381
|
|
|
$code->addStatement('return phpversion("apcu");'); |
382
|
|
|
|
383
|
|
|
return $this->adapter->run($code); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: