Completed
Push — 1.x ( 2f995a...fa8c59 )
by Samuel
18:04
created

ApcProxy::apc_store()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
nc 2
cc 3
eloc 10
nop 3
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 ApcProxy implements ProxyInterface
18
{
19
    /**
20
     * @var AbstractAdapter
21
     */
22
    protected $adapter;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getFunctions()
28
    {
29
        return array(
30
            'apc_add',
31
            'apc_bin_dump',
32
            'apc_bin_dumpfile',
33
            'apc_bin_load',
34
            'apc_bin_loadfile',
35
            'apc_cas',
36
            'apc_cache_info',
37
            'apc_clear_cache',
38
            'apc_compile_file',
39
            'apc_dec',
40
            'apc_define_constants',
41
            'apc_delete_file',
42
            'apc_delete',
43
            'apc_exists',
44
            'apc_fetch',
45
            'apc_inc',
46
            'apc_load_constants',
47
            'apc_sma_info',
48
            'apc_store',
49
50
            'apc_version'
51
        );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function setAdapter(AbstractAdapter $adapter)
58
    {
59
        $this->adapter = $adapter;
60
    }
61
62
    /**
63
     * Caches a variable in the data store, only if it's not already stored
64
     *
65
     * Note: Unlike many other mechanisms in PHP, variables stored using apc_add() will persist between requests
66
     *       (until the value is removed from the cache).
67
     *
68
     * @since  3.0.13
69
     * @param  mixed $key Store the variable using this name. keys are cache-unique, so attempting to use apc_add() to
70
     *                    store data with a key that already exists will not overwrite the existing data, and will
71
     *                    instead return FALSE. (This is the only difference between apc_add() and apc_store().)
72
     *                    If $key is an array set Names in key, variables in value
73
     * @param  mixed $var The variable to store
74
     *                    If $key is an array, this parameter is unused and set to NULL
75
     * @param  int $ttl   Time To Live; store var in the cache for ttl seconds. After the ttl has passed, the stored
76
     *                    variable will be expunged from the cache (on the next request). If no ttl is supplied
77
     *                    (or if the ttl is 0), the value will persist until it is removed from the cache manually,
78
     *                    or otherwise fails to exist in the cache (clear, restart, etc.).
79
     * @return mixed
80
     */
81
    public function apc_add($key, $var = null, $ttl = 0)
82
    {
83
        if (is_string($key) && $var === null) {
84
            throw new \InvalidArgumentException('When $key is set $var cannot be null');
85
        }
86
87
        $code = new Code();
88
        $code->addStatement(sprintf(
89
            'return apc_add(%s, %s, %s);',
90
            var_export($key, true),
91
            var_export($var, true),
92
            var_export($ttl, true)
93
        ));
94
95
        return $this->adapter->run($code);
96
    }
97
98
    /**
99
     * Get a binary dump of the given files and user variables
100
     *
101
     * Returns a binary dump of the given files and user variables from the APC cache. A NULL for files or user_vars
102
     * signals a dump of every entry, whereas array() will dump nothing
103
     *
104
     * @since  3.1.4
105
     * @param  mixed $files     The files. Passing in NULL signals a dump of every entry, while passing in array() will
106
     *                          dump nothing.
107
     * @param  mixed $user_vars The user vars. Passing in NULL signals a dump of every entry, while passing in array()
108
     *                          will dump nothing.
109
     * @return mixed            Returns a binary dump of the given files and user variables from the APC cache, FALSE if
110
     *                          APC is not enabled, or NULL if an unknown error is encountered.
111
     */
112
    public function apc_bin_dump(array $files = null, $user_vars = null)
113
    {
114
        $code = new Code();
115
        $code->addStatement(sprintf(
116
            'return apc_bin_dump(%s, %s);',
117
            var_export($files, true),
118
            var_export($user_vars, true)
119
        ));
120
121
        return $this->adapter->run($code);
122
    }
123
124
    /**
125
     * Outputs a binary dump of the given files and user variables from the APC cache to the named file.
126
     *
127
     * @since  3.1.4
128
     * @param  array    $files     The file names being dumped
129
     * @param  array    $user_vars The user variables being dumped
130
     * @param  string   $filename  The filename where the dump is being saved
131
     * @param  integer  $flags     Flags passed to the filename stream. See the file_put_contents() documentation for
132
     *                             details.
133
     * @param  resource $context   The context passed to the filename stream. See the file_put_contents() documentation
134
     *                             for details.
135
     * @return integer             The number of bytes written to the file, otherwise FALSE if APC is not enabled,
136
     *                             filename is an invalid file name, filename can't be opened, the file dump can't be
137
     *                             completed (e.g., the hard drive is out of disk space), or an unknown error was
138
     *                             encountered.
139
     */
140
    public function apc_bin_dumpfile(array $files, array $user_vars, $filename, $flags = 0, $context = null)
141
    {
142
        $code = new Code();
143
        $code->addStatement(sprintf(
144
            'return apc_bin_dumpfile(%s, %s, %s, %s, %s);',
145
            var_export($files, true),
146
            var_export($user_vars, true),
147
            var_export($filename, true),
148
            var_export($flags, true),
149
            var_export($context, true)
150
        ));
151
152
        return $this->adapter->run($code);
153
    }
154
155
    /**
156
     * Loads the given binary dump into the APC file/user cache
157
     *
158
     * @since  3.1.4
159
     * @param  string  $data  The binary dump being loaded, likely from apc_bin_dump().
160
     * @param  integer $flags Either APC_BIN_VERIFY_CRC32, APC_BIN_VERIFY_MD5, or both.
161
     * @return boolean        Returns TRUE if the binary dump data was loaded with success, otherwise FALSE is returned.
162
     *                        FALSE is returned if APC is not enabled, or if the data is not a valid APC binary dump (e.g., unexpected size)
163
     */
164
    public function apc_bin_load($data, $flags = 0)
165
    {
166
        $code = new Code();
167
        $code->addStatement(sprintf(
168
            'return apc_bin_load(%s, %s);',
169
            var_export($data, true),
170
            var_export($flags, true)
171
        ));
172
173
        return $this->adapter->run($code);
174
    }
175
176
    /**
177
     * Load a binary dump from a file into the APC file/user cache
178
     *
179
     * @since  3.1.4
180
     * @param  string   $filename The file name containing the dump, likely from apc_bin_dumpfile().
181
     * @param  resource $context  The files context.
182
     * @param  integer  $flags    Either APC_BIN_VERIFY_CRC32, APC_BIN_VERIFY_MD5, or both.
183
     * @return boolean            Returns TRUE on success, otherwise FALSE Reasons it may return FALSE include APC is not
184
     *                            enabled, filename is an invalid file name or empty, filename can't be opened, the file
185
     *                            dump can't be completed, or if the data is not a valid APC binary dump (e.g.,
186
     *                            unexpected size).
187
     */
188
    public function apc_bin_loadfile($filename, $context = null, $flags = 0)
189
    {
190
        $code = new Code();
191
        $code->addStatement(sprintf(
192
            'return apc_bin_loadfile(%s, %s, %s);',
193
            var_export($filename, true),
194
            var_export($context, true),
195
            var_export($flags, true)
196
        ));
197
198
        return $this->adapter->run($code);
199
    }
200
201
    /**
202
     * Updates an old value with a new value
203
     * apc_cas() updates an already existing integer value if the old parameter matches the currently stored value with the value of the new parameter.
204
     *
205
     * @since  3.1.1
206
     * @param  string $key The key of the value being updated.
207
     * @param  int    $old The old value (the value currently stored).
208
     * @param  int    $new The new value to update to
209
     * @return boolean     Returns TRUE on success or FALSE on failure.
210
     */
211
    public function apc_cas($key, $old, $new)
212
    {
213
        $code = new Code();
214
        $code->addStatement(sprintf(
215
            'return apc_cas(%s, %s, %s);',
216
            var_export($key, true),
217
            var_export($old, true),
218
            var_export($new, true)
219
        ));
220
221
        return $this->adapter->run($code);
222
    }
223
224
    /**
225
     * Retrieves cached information from APC's data store
226
     *
227
     * @since  2.0.0
228
     * @param  string  $cache_type If cache_type is "user", information about the user cache will be returned.
229
     *                             If cache_type is "filehits", information about which files have been served from the
230
     *                             bytecode cache for the current request will be returned. This feature must be enabled
231
     *                             at compile time using --enable-filehits .
232
     *                             If an invalid or no cache_type is specified, information about the system cache
233
     *                             (cached files) will be returned.
234
     * @param  boolean $limited    If limited is TRUE, the return value will exclude the individual list of cache
235
     *                             entries. This is useful when trying to optimize calls for statistics gathering.
236
     * @return boolean             Array of cached data (and meta-data) or FALSE on failure
237
     */
238
    public function apc_cache_info($cache_type = "", $limited = false)
239
    {
240
        $code = new Code();
241
        $code->addStatement(sprintf(
242
            'return apc_cache_info(%s, %s);',
243
            var_export($cache_type, true),
244
            var_export($limited, true)
245
        ));
246
247
        return $this->adapter->run($code);
248
    }
249
250
    /**
251
     * Clears the user/system cache
252
     *
253
     * @since  2.0.0
254
     * @param  string  $cache_type If cache_type is "user", the user cache will be cleared; otherwise, the system cache
255
     *                             (cached files) will be cleared.
256
     * @return boolean             Always returns true
257
     */
258
    public function apc_clear_cache($cache_type = "")
259
    {
260
        $code = new Code();
261
        $code->addStatement(sprintf(
262
            'return apc_clear_cache(%s);',
263
            var_export($cache_type, true)
264
        ));
265
266
        return $this->adapter->run($code);
267
    }
268
269
    /**
270
     * Stores a file in the bytecode cache, bypassing all filters.
271
     *
272
     * @since  3.0.13
273
     * @param  string  $filename Full or relative path to a PHP file that will be compiled and stored in the bytecode cache.
274
     * @param  boolean $atomic   defaults to true
275
     * @return boolean           Returns TRUE on success or FALSE on failure
276
     */
277
    public function apc_compile_file($filename, $atomic = true)
278
    {
279
        $code = new Code();
280
        $code->addStatement(sprintf(
281
            'return apc_compile_file(%s, %s);',
282
            var_export($filename, true),
283
            var_export($atomic, true)
284
        ));
285
286
        return $this->adapter->run($code);
287
    }
288
289
    /**
290
     * Decrease a stored number
291
     *
292
     * @since  3.1.1
293
     * @param  string    $key   The key of the value being decreased.
294
     * @param  int       $step  The step, or value to decrease.
295
     * @param  \stdClass $ref   success is set to TRUE in success and FALSE in failure
296
     * @return mixed            Returns the current value of key's value on success, or FALSE on failure
297
     */
298
    public function apc_dec($key, $step = 1, $ref = false)
299
    {
300
        $code = new Code();
301
        $code->addStatement('$success = false;');
302
        $code->addStatement(sprintf(
303
            '$result = apc_dec(%s, %s, $success);',
304
            var_export($key, true),
305
            var_export($step, true)
306
        ));
307
        $code->addStatement('return array($result, $success);');
308
309
        list($result, $success) = $this->adapter->run($code);
310
311
        if (is_object($ref)) {
312
            $ref->success = $success;
313
        }
314
315
        return $result;
316
    }
317
318
    /**
319
     * Defines a set of constants for retrieval and mass-definition
320
     *
321
     * define() is notoriously slow. Since the main benefit of APC is to increase the performance of
322
     * scripts/applications, this mechanism is provided to streamline the process of mass constant definition. However,
323
     * this function does not perform as well as anticipated.
324
     *
325
     * @since  3.0.0
326
     * @param  string  $key            The key serves as the name of the constant set being stored. This key is used to
327
     *                                 retrieve the stored constants in apc_load_constants().
328
     * @param  array   $constants      An associative array of constant_name => value pairs. The constant_name must
329
     *                                 follow the normal constant naming rules. value must evaluate to a scalar value.
330
     * @param  boolean $case_sensitive The default behaviour for constants is to be declared case-sensitive; i.e.
331
     *                                 CONSTANT and Constant represent different values. If this parameter evaluates to
332
     *                                 FALSE the constants will be declared as case-insensitive symbols.
333
     * @return boolean                 Returns TRUE on success or FALSE on failure.
334
     */
335
    public function apc_define_constants($key, array $constants, $case_sensitive = true)
336
    {
337
        $code = new Code();
338
        $code->addStatement(sprintf(
339
            'return apc_define_constants(%s, %s, %s);',
340
            var_export($key, true),
341
            var_export($constants, true),
342
            var_export($case_sensitive, true)
343
        ));
344
345
        return $this->adapter->run($code);
346
    }
347
348
    /**
349
     * Deletes files from the opcode cache
350
     *
351
     * @since  3.1.1
352
     * @param  mixed $keys The files to be deleted. Accepts a string, array of strings, or an APCIterator object.
353
     * @return mixed       Returns TRUE on success or FALSE on failure. Or if keys is an array, then an empty array is
354
     *                     returned on success, or an array of failed files is returned.
355
     */
356
    public function apc_delete_file($keys)
357
    {
358
        $code = new Code();
359
        $code->addStatement(sprintf(
360
            'return apc_delete_file(%s);',
361
            var_export($keys, true)
362
        ));
363
364
        return $this->adapter->run($code);
365
    }
366
367
    /**
368
     * Removes a stored variable from the cache
369
     *
370
     * @since  3.1.1
371
     * @param  mixed $key The key used to store the value (with apc_store()).
372
     * @return mixed      Returns TRUE on success or FALSE on failure.
373
     */
374
    public function apc_delete($key)
375
    {
376
        $code = new Code();
377
        $code->addStatement(sprintf(
378
            'return apc_delete(%s);',
379
            var_export($key, true)
380
        ));
381
382
        return $this->adapter->run($code);
383
    }
384
385
    /**
386
     * Checks if one or more APC keys exist.
387
     *
388
     * @since  3.1.4
389
     * @param  mixed $keys A string, or an array of strings, that contain keys.
390
     * @return mixed       Returns TRUE if the key exists, otherwise FALSE Or if an array was passed to keys, then an
391
     *                     array is returned that contains all existing keys, or an empty array if none exist.
392
     */
393
    public function apc_exists($keys)
394
    {
395
        $code = new Code();
396
        $code->addStatement(sprintf(
397
            'return apc_exists(%s);',
398
            var_export($keys, true)
399
        ));
400
401
        return $this->adapter->run($code);
402
    }
403
404
    /**
405
     * Fetch a stored variable from the cache
406
     *
407
     * @since  3.0.0
408
     * @param  mixed     $key The key used to store the value (with apc_store()). If an array is passed then each element is fetched and returned.
409
     * @param  \stdClass $ref success is set to TRUE in success and FALSE in failure
410
     * @return mixed          The stored variable or array of variables on success; FALSE on failure
411
     */
412
    public function apc_fetch($key, $ref = false)
413
    {
414
        $code = new Code();
415
        $code->addStatement('$success = false;');
416
        $code->addStatement(sprintf('$result = apc_fetch(%s, $success);', var_export($key, true)));
417
        $code->addStatement('return array($result, $success);');
418
419
        list($var, $success) = $this->adapter->run($code);
420
421
        if (is_object($ref)) {
422
            $ref->success = $success;
423
        }
424
425
        return $var;
426
    }
427
428
    /**
429
     * Increase a stored number
430
     *
431
     * @since  3.1.1
432
     * @param  string    $key  The key of the value being increased.
433
     * @param  int       $step The step, or value to increased.
434
     * @param  \stdClass $ref  success is set to TRUE in success and FALSE in failure
435
     * @return mixed           Returns the current value of key's value on success, or FALSE on failure
436
     */
437
    public function apc_inc($key, $step = 1, $ref = false)
438
    {
439
        $code = new Code();
440
        $code->addStatement('$success = false;');
441
        $code->addStatement(sprintf(
442
            '$result = apc_inc(%s, %s, $success);',
443
            var_export($key, true),
444
            var_export($step, true)
445
        ));
446
        $code->addStatement('return array($result, $success);');
447
448
        list($result, $success) = $this->adapter->run($code);
449
450
        if (is_object($ref)) {
451
            $ref->success = $success;
452
        }
453
454
        return $result;
455
    }
456
457
    /**
458
     * Loads a set of constants from the cache
459
     *
460
     * @since  3.0.0
461
     * @param  mixed   $key            The name of the constant set (that was stored with apc_define_constants()) to be
462
     *                                 retrieved.
463
     * @param  boolean $case_sensitive The default behaviour for constants is to be declared case-sensitive; i.e.
464
     *                                 CONSTANT and Constant represent different values. If this parameter evaluates to
465
     *                                 FALSE the constants will be declared as case-insensitive symbols.
466
     * @return boolean                 Returns TRUE on success or FALSE on failure
467
     */
468
    public function apc_load_constants($key, $case_sensitive = true)
469
    {
470
        $code = new Code();
471
        $code->addStatement(sprintf(
472
            'return apc_load_constants(%s, %s);',
473
            var_export($key, true),
474
            var_export($case_sensitive, true)
475
        ));
476
477
        return $this->adapter->run($code);
478
    }
479
480
    /**
481
     * Retrieves APC's Shared Memory Allocation information
482
     *
483
     * @since  2.0.0
484
     * @param  boolean $limited When set to FALSE (default) apc_sma_info() will return a detailed information about
485
     *                          each segment.
486
     * @return boolean          Array of Shared Memory Allocation data; FALSE on failure
487
     */
488
    public function apc_sma_info($limited = false)
489
    {
490
        $code = new Code();
491
        $code->addStatement(sprintf(
492
            'return apc_sma_info(%s);',
493
            var_export($limited, true)
494
        ));
495
496
        return $this->adapter->run($code);
497
    }
498
499
    /**
500
     * Cache a variable in the data store
501
     *
502
     * Note: Unlike many other mechanisms in PHP, variables stored using apc_store() will persist between requests
503
     * (until the value is removed from the cache).
504
     *
505
     * @since  3.0.0
506
     * @param  mixed $key Store the variable using this name. keys are cache-unique, so storing a second value with the
507
     *                    same key will overwrite the original value.
508
     *                    If $key is an array set Names in key, variables in value
509
     * @param  mixed $var The variable to store
510
     *                    If $key is an array, this parameter is unused and set to NULL
511
     * @param  int $ttl   Time To Live; store var in the cache for ttl seconds. After the ttl has passed, the stored
512
     *                    variable will be expunged from the cache (on the next request). If no ttl is supplied (or if
513
     *                    the ttl is 0), the value will persist until it is removed from the cache manually, or
514
     *                    otherwise fails to exist in the cache (clear, restart, etc.).
515
     * @return boolean    Returns TRUE on success or FALSE on failure. Second syntax returns array with error keys.
516
     */
517
    public function apc_store($key, $var = null, $ttl = 0)
518
    {
519
        if (is_string($key) && $var === null) {
520
            throw new \InvalidArgumentException('When $key is set $var cannot be null');
521
        }
522
523
        $code = new Code();
524
        $code->addStatement(sprintf(
525
            'return apc_store(%s, %s, %s);',
526
            var_export($key, true),
527
            var_export($var, true),
528
            var_export($ttl, true)
529
        ));
530
531
        return $this->adapter->run($code);
532
    }
533
534
    /**
535
     * @return string
536
     */
537
    public function apc_version()
538
    {
539
        $code = new Code();
540
        $code->addStatement('return phpversion("apc");');
541
542
        return $this->adapter->run($code);
543
    }
544
}
545