|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Src/Cache/Storage/ApcStorage.php |
|
4
|
|
|
* |
|
5
|
|
|
* @package Ds\Cache\Storage |
|
6
|
|
|
* @subpackage Cache |
|
7
|
|
|
* @author Dan Smith <[email protected]> |
|
8
|
|
|
* @version v.1 (20/03/2017) |
|
9
|
|
|
* @copyright Copyright (c) 2017, Dan Smith |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Ds\Cache\Storage; |
|
12
|
|
|
|
|
13
|
|
|
use DateInterval; |
|
14
|
|
|
use Ds\Cache\CacheException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class ApcStorage |
|
18
|
|
|
* |
|
19
|
|
|
* Note: This Storage is untested and no longer supported |
|
20
|
|
|
* |
|
21
|
|
|
* @package Ds\Cache |
|
22
|
|
|
*/ |
|
23
|
|
|
class ApcStorage extends AbstractStorage |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* ApcStorage constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param DateInterval $ttl |
|
29
|
|
|
* @throws CacheException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(DateInterval $ttl) |
|
32
|
|
|
{ |
|
33
|
|
|
$foundApc = \extension_loaded('apc'); |
|
34
|
|
|
|
|
35
|
|
|
if (!$foundApc){ |
|
36
|
|
|
throw new CacheException('APC Extension not found.'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
parent::__construct($ttl); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Determines whether an item is present in the cache. |
|
44
|
|
|
* |
|
45
|
|
|
* NOTE: It is recommended that has() is only to be used for cache warming type purposes |
|
46
|
|
|
* and not to be used within your live applications operations for get/set, as this method |
|
47
|
|
|
* is subject to a race condition where your has() will return true and immediately after, |
|
48
|
|
|
* another script can remove it making the state of your app out of date. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $key The cache item key. |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function has($key){ |
|
55
|
|
|
return \apc_exists($key); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $key The key of the item to store. |
|
62
|
|
|
* @param mixed $value The value of the item to store, must be serializable. |
|
63
|
|
|
* @param null|int $ttl Optional. The TTL value of this item. If no value is sent and |
|
64
|
|
|
* the driver supports TTL then the library may set a default value |
|
65
|
|
|
* for it or let the driver take care of that. |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool True on success and false on failure. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function set($key, $value, $ttl = null) |
|
70
|
|
|
{ |
|
71
|
|
|
$expires = $this->getTtlTimestamp($ttl); |
|
72
|
|
|
\apc_store($key, $value, $expires); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Fetches a value from the cache. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $key The unique key of this item in the cache. |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed The value of the item from the cache, or $default in case of cache miss. |
|
81
|
|
|
* |
|
82
|
|
|
*/ |
|
83
|
|
|
public function get($key) |
|
84
|
|
|
{ |
|
85
|
|
|
return \apc_fetch($key); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Delete an item from the cache by its unique key. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $key The unique cache key of the item to delete. |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool True if the item was successfully removed. False if there was an error. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function delete($key) |
|
96
|
|
|
{ |
|
97
|
|
|
if (\apc_exists($key)){ |
|
98
|
|
|
\apc_delete($key); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Wipes clean the entire cache's keys. |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool True on success and false on failure. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function clear(){ |
|
108
|
|
|
|
|
109
|
|
|
\apc_clear_cache(); |
|
110
|
|
|
$apcResult = $this->getInfo(); |
|
111
|
|
|
|
|
112
|
|
|
if (!empty($apcResult['cache_list'])){ |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return true; |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get APC status. |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getInfo(){ |
|
126
|
|
|
return apc_cache_info(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|