ApcuProxy::apcu_regexp_get_keys()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 1
dl 0
loc 22
ccs 0
cts 11
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
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 15
    public function getFunctions()
28
    {
29
        return [
30 15
            'apcu_add',
31
            'apcu_cache_info',
32
            'apcu_regexp_get_keys',
33
            'apcu_cas',
34
            'apcu_clear_cache',
35
            'apcu_dec',
36
            'apcu_delete',
37
            'apcu_regexp_delete',
38
            'apcu_exists',
39
            'apcu_fetch',
40
            'apcu_inc',
41
            'apcu_sma_info',
42
            'apcu_store',
43
            'apcu_version'
44
        ];
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 16
    public function setAdapter(AbstractAdapter $adapter)
51
    {
52 16
        $this->adapter = $adapter;
53 16
    }
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
     */
74 2
    public function apcu_add($key, $var = null, $ttl = 0)
75
    {
76 2
        if (is_string($key) && $var === null) {
77 1
            throw new \InvalidArgumentException('When $key is set $var cannot be null');
78
        }
79
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
88 1
        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
     */
101 1
    public function apcu_cas($key, $old, $new)
102
    {
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
111 1
        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
     */
122 3
    public function apcu_cache_info($limited = false)
123
    {
124 3
        $code = new Code();
125 3
        $code->addStatement(sprintf(
126 3
            'return apcu_cache_info(%s);',
127 3
            var_export($limited, true)
128
        ));
129
130 3
        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) !== null) {
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
          if ($keys->getTotalCount()){
156
            foreach ($keys as $key){
157
              $result[] = [
158
              "key" => $key["key"],
159
              "ttl" => $key["ttl"]
160
              ];
161
            }
162
          }');
163
        $code->addStatement('return $result;');
164
        return $this->adapter->run($code);
165
    }
166
167
    /**
168
     * Clears the user/system cache
169
     *
170
     * @since  2.0.0
171
     * @return boolean             Always returns true
172
     */
173 1
    public function apcu_clear_cache()
174
    {
175 1
        $code = new Code();
176 1
        $code->addStatement('return apcu_clear_cache();');
177
178 1
        return $this->adapter->run($code);
179
    }
180
181
    /**
182
     * Decrease a stored number
183
     *
184
     * @since  3.1.1
185
     * @param  string    $key   The key of the value being decreased.
186
     * @param  int       $step  The step, or value to decrease.
187
     * @param  \stdClass $ref   success is set to TRUE in success and FALSE in failure
188
     * @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 1
        $code = new Code();
193 1
        $code->addStatement('$success = false;');
194 1
        $code->addStatement(sprintf(
195 1
            '$result = apcu_dec(%s, %s, $success);',
196 1
            var_export($key, true),
197 1
            var_export($step, true)
198
        ));
199 1
        $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
        }
206
207 1
        return $result;
208
    }
209
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 2
    public function apcu_delete($key)
218
    {
219 2
        $code = new Code();
220 2
        $code->addStatement(sprintf(
221 2
            'return apcu_delete(%s);',
222 2
            var_export($key, true)
223
        ));
224
225 2
        return $this->adapter->run($code);
226
    }
227
228
    /**
229
     * Retrieves caches keys & TTL from APCu's data store
230
     *
231
     * @since  3.1.1
232
     *
233
     * @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) !== null) {
241
            throw new \RuntimeException("{$regexp} is not a valid Regex");
242
        }
243
244
        $code = new Code();
245
        $code->addStatement(sprintf(
246
            '$keys = new \APCIterator("user", %s, APC_ITER_KEY, 10);',
247
            var_export($regexp, true)
248
        ));
249
        $code->addStatement('return apc_delete($keys);');
250
        return $this->adapter->run($code);
251
    }
252
253
    /**
254
     * Checks if one or more APCu keys exist.
255
     *
256
     * @since  3.1.4
257
     * @param  mixed $keys A string, or an array of strings, that contain keys.
258
     * @return mixed       Returns TRUE if the key exists, otherwise FALSE Or if an array was passed to keys, then an
259
     *                     array is returned that contains all existing keys, or an empty array if none exist.
260
     */
261 2
    public function apcu_exists($keys)
262
    {
263 2
        $code = new Code();
264 2
        $code->addStatement(sprintf(
265 2
            'return apcu_exists(%s);',
266 2
            var_export($keys, true)
267
        ));
268
269 2
        return $this->adapter->run($code);
270
    }
271
272
    /**
273
     * Fetch a stored variable from the cache
274
     *
275
     * @since  3.0.0
276
     * @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
     * @param  \stdClass $ref success is set to TRUE in success and FALSE in failure
278
     * @return mixed          The stored variable or array of variables on success; FALSE on failure
279
     */
280 2
    public function apcu_fetch($key, $ref = false)
281
    {
282 2
        $code = new Code();
283 2
        $code->addStatement('$success = false;');
284 2
        $code->addStatement(sprintf('$result = apcu_fetch(%s, $success);', var_export($key, true)));
285 2
        $code->addStatement('return array($result, $success);');
286
287 2
        list($var, $success) = $this->adapter->run($code);
288
289 2
        if (is_object($ref)) {
290 2
            $ref->success = $success;
291
        }
292
293 2
        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
     * @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
     */
305 1
    public function apcu_inc($key, $step = 1, $ref = false)
306
    {
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 1
            var_export($key, true),
312 1
            var_export($step, true)
313
        ));
314 1
        $code->addStatement('return array($result, $success);');
315
316 1
        list($result, $success) = $this->adapter->run($code);
317
318 1
        if (is_object($ref)) {
319 1
            $ref->success = $success;
320
        }
321
322 1
        return $result;
323
    }
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 2
    public function apcu_sma_info($limited = false)
334
    {
335 2
        $code = new Code();
336 2
        $code->addStatement(sprintf(
337 2
            'return apcu_sma_info(%s);',
338 2
            var_export($limited, true)
339
        ));
340
341 2
        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 2
    public function apcu_store($key, $var = null, $ttl = 0)
363
    {
364 2
        $code = new Code();
365 2
        $code->addStatement(sprintf(
366 2
            'return apcu_store(%s, %s, %s);',
367 2
            var_export($key, true),
368 2
            var_export($var, true),
369 2
            var_export($ttl, true)
370
        ));
371
372 2
        return $this->adapter->run($code);
373
    }
374
375
    /**
376
     * @return string
377
     */
378 1
    public function apcu_version()
379
    {
380 1
        $code = new Code();
381 1
        $code->addStatement('return phpversion("apcu");');
382
383 1
        return $this->adapter->run($code);
384
    }
385
}
386