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_cas', |
33
|
1 |
|
'apcu_clear_cache', |
34
|
1 |
|
'apcu_dec', |
35
|
1 |
|
'apcu_delete', |
36
|
1 |
|
'apcu_exists', |
37
|
1 |
|
'apcu_fetch', |
38
|
1 |
|
'apcu_inc', |
39
|
1 |
|
'apcu_sma_info', |
40
|
1 |
|
'apcu_store', |
41
|
|
|
|
42
|
|
|
'apcu_version' |
43
|
1 |
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
2 |
|
public function setAdapter(AbstractAdapter $adapter) |
50
|
|
|
{ |
51
|
2 |
|
$this->adapter = $adapter; |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Caches a variable in the data store, only if it's not already stored |
56
|
|
|
* |
57
|
|
|
* Note: Unlike many other mechanisms in PHP, variables stored using apcu_add() will persist between requests |
58
|
|
|
* (until the value is removed from the cache). |
59
|
|
|
* |
60
|
|
|
* @since 3.0.13 |
61
|
|
|
* @param mixed $key Store the variable using this name. keys are cache-unique, so attempting to use apc_add() to |
62
|
|
|
* store data with a key that already exists will not overwrite the existing data, and will |
63
|
|
|
* instead return FALSE. (This is the only difference between apcu_add() and apc_store().) |
64
|
|
|
* If $key is an array set Names in key, variables in value |
65
|
|
|
* @param mixed $var The variable to store |
66
|
|
|
* If $key is an array, this parameter is unused and set to NULL |
67
|
|
|
* @param int $ttl Time To Live; store var in the cache for ttl seconds. After the ttl has passed, the stored |
68
|
|
|
* variable will be expunged from the cache (on the next request). If no ttl is supplied |
69
|
|
|
* (or if the ttl is 0), the value will persist until it is removed from the cache manually, |
70
|
|
|
* or otherwise fails to exist in the cache (clear, restart, etc.). |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
2 |
|
public function apcu_add($key, $var = null, $ttl = 0) |
74
|
|
|
{ |
75
|
2 |
|
if (is_string($key) && $var === null) { |
76
|
1 |
|
throw new \InvalidArgumentException('When $key is set $var cannot be null'); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$code = new Code(); |
80
|
1 |
|
$code->addStatement(sprintf( |
81
|
1 |
|
'return apcu_add(%s, %s, %s);', |
82
|
1 |
|
var_export($key, true), |
83
|
1 |
|
var_export($var, true), |
84
|
1 |
|
var_export($ttl, true) |
85
|
1 |
|
)); |
86
|
|
|
|
87
|
1 |
|
return $this->adapter->run($code); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Updates an old value with a new value |
92
|
|
|
* apcu_cas() updates an already existing integer value if the old parameter matches the currently stored value with the value of the new parameter. |
93
|
|
|
* |
94
|
|
|
* @since 3.1.1 |
95
|
|
|
* @param string $key The key of the value being updated. |
96
|
|
|
* @param int $old The old value (the value currently stored). |
97
|
|
|
* @param int $new The new value to update to |
98
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure. |
99
|
|
|
*/ |
100
|
1 |
|
public function apcu_cas($key, $old, $new) |
101
|
|
|
{ |
102
|
1 |
|
$code = new Code(); |
103
|
1 |
|
$code->addStatement(sprintf( |
104
|
1 |
|
'return apcu_cas(%s, %s, %s);', |
105
|
1 |
|
var_export($key, true), |
106
|
1 |
|
var_export($old, true), |
107
|
1 |
|
var_export($new, true) |
108
|
1 |
|
)); |
109
|
|
|
|
110
|
1 |
|
return $this->adapter->run($code); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Retrieves cached information from APCu's data store |
115
|
|
|
* |
116
|
|
|
* @since 2.0.0 |
117
|
|
|
* @param boolean $limited If limited is TRUE, the return value will exclude the individual list of cache |
118
|
|
|
* entries. This is useful when trying to optimize calls for statistics gathering. |
119
|
|
|
* @return boolean Array of cached data (and meta-data) or FALSE on failure |
120
|
|
|
*/ |
121
|
1 |
|
public function apcu_cache_info($limited = false) |
122
|
|
|
{ |
123
|
1 |
|
$code = new Code(); |
124
|
1 |
|
$code->addStatement(sprintf( |
125
|
1 |
|
'return apcu_cache_info(%s);', |
126
|
1 |
|
var_export($limited, true) |
127
|
1 |
|
)); |
128
|
|
|
|
129
|
1 |
|
return $this->adapter->run($code); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Clears the user/system cache |
134
|
|
|
* |
135
|
|
|
* @since 2.0.0 |
136
|
|
|
* @return boolean Always returns true |
137
|
|
|
*/ |
138
|
|
|
public function apcu_clear_cache() |
139
|
|
|
{ |
140
|
|
|
$code = new Code(); |
141
|
|
|
$code->addStatement('return apcu_clear_cache();'); |
142
|
|
|
|
143
|
|
|
return $this->adapter->run($code); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Decrease a stored number |
148
|
|
|
* |
149
|
|
|
* @since 3.1.1 |
150
|
|
|
* @param string $key The key of the value being decreased. |
151
|
|
|
* @param int $step The step, or value to decrease. |
152
|
|
|
* @param \stdClass $ref success is set to TRUE in success and FALSE in failure |
153
|
|
|
* @return mixed Returns the current value of key's value on success, or FALSE on failure |
154
|
|
|
*/ |
155
|
1 |
|
public function apcu_dec($key, $step = 1, $ref = false) |
156
|
|
|
{ |
157
|
1 |
|
$code = new Code(); |
158
|
1 |
|
$code->addStatement('$success = false;'); |
159
|
1 |
|
$code->addStatement(sprintf( |
160
|
1 |
|
'$result = apcu_dec(%s, %s, $success);', |
161
|
1 |
|
var_export($key, true), |
162
|
1 |
|
var_export($step, true) |
163
|
1 |
|
)); |
164
|
1 |
|
$code->addStatement('return array($result, $success);'); |
165
|
|
|
|
166
|
1 |
|
list($result, $success) = $this->adapter->run($code); |
167
|
|
|
|
168
|
1 |
|
if (is_object($ref)) { |
169
|
1 |
|
$ref->success = $success; |
170
|
1 |
|
} |
171
|
|
|
|
172
|
1 |
|
return $result; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Removes a stored variable from the cache |
177
|
|
|
* |
178
|
|
|
* @since 3.1.1 |
179
|
|
|
* @param mixed $key The key used to store the value (with apcu_store()). |
180
|
|
|
* @return mixed Returns TRUE on success or FALSE on failure. |
181
|
|
|
*/ |
182
|
1 |
|
public function apcu_delete($key) |
183
|
|
|
{ |
184
|
1 |
|
$code = new Code(); |
185
|
1 |
|
$code->addStatement(sprintf( |
186
|
1 |
|
'return apcu_delete(%s);', |
187
|
1 |
|
var_export($key, true) |
188
|
1 |
|
)); |
189
|
|
|
|
190
|
1 |
|
return $this->adapter->run($code); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Checks if one or more APCu keys exist. |
195
|
|
|
* |
196
|
|
|
* @since 3.1.4 |
197
|
|
|
* @param mixed $keys A string, or an array of strings, that contain keys. |
198
|
|
|
* @return mixed Returns TRUE if the key exists, otherwise FALSE Or if an array was passed to keys, then an |
199
|
|
|
* array is returned that contains all existing keys, or an empty array if none exist. |
200
|
|
|
*/ |
201
|
1 |
|
public function apcu_exists($keys) |
202
|
|
|
{ |
203
|
1 |
|
$code = new Code(); |
204
|
1 |
|
$code->addStatement(sprintf( |
205
|
1 |
|
'return apcu_exists(%s);', |
206
|
1 |
|
var_export($keys, true) |
207
|
1 |
|
)); |
208
|
|
|
|
209
|
1 |
|
return $this->adapter->run($code); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Fetch a stored variable from the cache |
214
|
|
|
* |
215
|
|
|
* @since 3.0.0 |
216
|
|
|
* @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. |
217
|
|
|
* @param \stdClass $ref success is set to TRUE in success and FALSE in failure |
218
|
|
|
* @return mixed The stored variable or array of variables on success; FALSE on failure |
219
|
|
|
*/ |
220
|
1 |
|
public function apcu_fetch($key, $ref = false) |
221
|
|
|
{ |
222
|
1 |
|
$code = new Code(); |
223
|
1 |
|
$code->addStatement('$success = false;'); |
224
|
1 |
|
$code->addStatement(sprintf('$result = apcu_fetch(%s, $success);', var_export($key, true))); |
225
|
1 |
|
$code->addStatement('return array($result, $success);'); |
226
|
|
|
|
227
|
1 |
|
list($var, $success) = $this->adapter->run($code); |
228
|
|
|
|
229
|
1 |
|
if (is_object($ref)) { |
230
|
1 |
|
$ref->success = $success; |
231
|
1 |
|
} |
232
|
|
|
|
233
|
1 |
|
return $var; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Increase a stored number |
238
|
|
|
* |
239
|
|
|
* @since 3.1.1 |
240
|
|
|
* @param string $key The key of the value being increased. |
241
|
|
|
* @param int $step The step, or value to increased. |
242
|
|
|
* @param \stdClass $ref success is set to TRUE in success and FALSE in failure |
243
|
|
|
* @return mixed Returns the current value of key's value on success, or FALSE on failure |
244
|
|
|
*/ |
245
|
1 |
|
public function apcu_inc($key, $step = 1, $ref = false) |
246
|
|
|
{ |
247
|
1 |
|
$code = new Code(); |
248
|
1 |
|
$code->addStatement('$success = false;'); |
249
|
1 |
|
$code->addStatement(sprintf( |
250
|
1 |
|
'$result = apcu_inc(%s, %s, $success);', |
251
|
1 |
|
var_export($key, true), |
252
|
1 |
|
var_export($step, true) |
253
|
1 |
|
)); |
254
|
1 |
|
$code->addStatement('return array($result, $success);'); |
255
|
|
|
|
256
|
1 |
|
list($result, $success) = $this->adapter->run($code); |
257
|
|
|
|
258
|
1 |
|
if (is_object($ref)) { |
259
|
1 |
|
$ref->success = $success; |
260
|
1 |
|
} |
261
|
|
|
|
262
|
1 |
|
return $result; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Retrieves APCu's Shared Memory Allocation information |
267
|
|
|
* |
268
|
|
|
* @since 2.0.0 |
269
|
|
|
* @param boolean $limited When set to FALSE (default) apcu_sma_info() will return a detailed information about |
270
|
|
|
* each segment. |
271
|
|
|
* @return boolean Array of Shared Memory Allocation data; FALSE on failure |
272
|
|
|
*/ |
273
|
1 |
|
public function apcu_sma_info($limited = false) |
274
|
|
|
{ |
275
|
1 |
|
$code = new Code(); |
276
|
1 |
|
$code->addStatement(sprintf( |
277
|
1 |
|
'return apcu_sma_info(%s);', |
278
|
1 |
|
var_export($limited, true) |
279
|
1 |
|
)); |
280
|
|
|
|
281
|
1 |
|
return $this->adapter->run($code); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Cache a variable in the data store |
286
|
|
|
* |
287
|
|
|
* Note: Unlike many other mechanisms in PHP, variables stored using apcu_store() will persist between requests |
288
|
|
|
* (until the value is removed from the cache). |
289
|
|
|
* |
290
|
|
|
* @since 3.0.0 |
291
|
|
|
* @param mixed $key Store the variable using this name. keys are cache-unique, so storing a second value with the |
292
|
|
|
* same key will overwrite the original value. |
293
|
|
|
* If $key is an array set Names in key, variables in value |
294
|
|
|
* @param mixed $var The variable to store |
295
|
|
|
* If $key is an array, this parameter is unused and set to NULL |
296
|
|
|
* @param int $ttl Time To Live; store var in the cache for ttl seconds. After the ttl has passed, the stored |
297
|
|
|
* variable will be expunged from the cache (on the next request). If no ttl is supplied (or if |
298
|
|
|
* the ttl is 0), the value will persist until it is removed from the cache manually, or |
299
|
|
|
* otherwise fails to exist in the cache (clear, restart, etc.). |
300
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure. Second syntax returns array with error keys. |
301
|
|
|
*/ |
302
|
2 |
|
public function apcu_store($key, $var = null, $ttl = 0) |
303
|
|
|
{ |
304
|
2 |
|
if (is_string($key) && $var === null) { |
305
|
1 |
|
throw new \InvalidArgumentException('When $key is set $var cannot be null'); |
306
|
|
|
} |
307
|
|
|
|
308
|
1 |
|
$code = new Code(); |
309
|
1 |
|
$code->addStatement(sprintf( |
310
|
1 |
|
'return apcu_store(%s, %s, %s);', |
311
|
1 |
|
var_export($key, true), |
312
|
1 |
|
var_export($var, true), |
313
|
1 |
|
var_export($ttl, true) |
314
|
1 |
|
)); |
315
|
|
|
|
316
|
1 |
|
return $this->adapter->run($code); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @return string |
321
|
|
|
*/ |
322
|
1 |
|
public function apcu_version() |
323
|
|
|
{ |
324
|
1 |
|
$code = new Code(); |
325
|
1 |
|
$code->addStatement('return phpversion("apcu");'); |
326
|
|
|
|
327
|
1 |
|
return $this->adapter->run($code); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|