1
|
|
|
<?php namespace Comodojo\Cache\Drivers; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Utils\UniqueId; |
4
|
|
|
use \Exception; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @package Comodojo Cache |
8
|
|
|
* @author Marco Giovinazzi <[email protected]> |
9
|
|
|
* @license MIT |
10
|
|
|
* |
11
|
|
|
* LICENSE: |
12
|
|
|
* |
13
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19
|
|
|
* THE SOFTWARE. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class Apc extends AbstractDriver { |
23
|
|
|
|
24
|
|
|
const DRIVER_NAME = "apc"; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
47 |
|
public function __construct(array $configuration = []) { |
30
|
|
|
|
31
|
47 |
|
if ( extension_loaded('apc') === false ) throw new Exception("ext-apc not available"); |
32
|
|
|
|
33
|
|
|
// In cli, apcu SHOULD NOT use the request time for cache retrieve/invalidation. |
34
|
|
|
// This is because in cli the request time is allways the same. |
35
|
47 |
|
if ( php_sapi_name() === 'cli' ) { |
36
|
47 |
|
ini_set('apc.use_request_time', 0); |
37
|
|
|
} |
38
|
|
|
|
39
|
47 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
47 |
|
public function test() { |
45
|
|
|
|
46
|
47 |
|
return (bool) ini_get('apc.enabled'); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
33 |
|
public function get($key, $namespace) { |
54
|
|
|
|
55
|
33 |
|
$scope = $this->getNamespaceKey($namespace); |
56
|
|
|
|
57
|
33 |
|
if ( $scope === false ) return null; |
|
|
|
|
58
|
|
|
|
59
|
25 |
|
$shadowName = "$scope-$key"; |
60
|
|
|
|
61
|
25 |
|
$item = apc_fetch($shadowName, $success); |
62
|
|
|
|
63
|
25 |
|
return $success === false ? null : $item; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
27 |
|
public function set($key, $namespace, $value, $ttl = null) { |
71
|
|
|
|
72
|
27 |
|
if ( $ttl == null ) $ttl = 0; |
73
|
|
|
|
74
|
27 |
|
$scope = $this->getNamespaceKey($namespace); |
75
|
|
|
|
76
|
27 |
|
if ( $scope === false ) $scope = $this->setNamespaceKey($namespace); |
|
|
|
|
77
|
|
|
|
78
|
27 |
|
if ( $scope === false ) return false; |
|
|
|
|
79
|
|
|
|
80
|
27 |
|
$shadowName = "$scope-$key"; |
81
|
|
|
|
82
|
27 |
|
return apc_store($shadowName, $value, $ttl); |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
3 |
|
public function delete($key, $namespace) { |
90
|
|
|
|
91
|
3 |
|
$scope = $this->getNamespaceKey($namespace); |
92
|
|
|
|
93
|
3 |
|
if ( $scope === false ) return false; |
|
|
|
|
94
|
|
|
|
95
|
3 |
|
$shadowName = "$scope-$key"; |
96
|
|
|
|
97
|
3 |
|
return apc_delete($shadowName); |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
11 |
|
public function clear($namespace = null) { |
105
|
|
|
|
106
|
11 |
|
if ( $namespace == null ) { |
107
|
|
|
|
108
|
9 |
|
return apc_clear_cache("user"); |
109
|
|
|
|
110
|
|
|
} else { |
111
|
|
|
|
112
|
2 |
|
$scope = $this->getNamespaceKey($namespace); |
113
|
|
|
|
114
|
2 |
|
if ( $scope === false ) return false; |
|
|
|
|
115
|
|
|
|
116
|
2 |
|
return apc_delete($namespace); |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
2 |
|
public function getMultiple(array $keys, $namespace) { |
126
|
|
|
|
127
|
2 |
|
$keypad = array_combine($keys, array_fill(0, count($keys), null)); |
128
|
|
|
|
129
|
2 |
|
$scope = $this->getNamespaceKey($namespace); |
130
|
|
|
|
131
|
2 |
|
if ( $scope === false ) return $keypad; |
|
|
|
|
132
|
|
|
|
133
|
2 |
|
$keyscope = array_map(function($key) use($scope) { |
134
|
2 |
|
return "$scope-$key"; |
135
|
2 |
|
}, $keys); |
136
|
|
|
|
137
|
2 |
|
$data = apc_fetch($keyscope, $success); |
138
|
|
|
|
139
|
2 |
|
$return = []; |
140
|
|
|
|
141
|
2 |
|
foreach ( $data as $scoped_key => $value ) { |
142
|
2 |
|
$key = substr($scoped_key, strlen("$scope-")); |
143
|
2 |
|
$return[$key] = $value; |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
return array_replace($keypad, $return); |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
1 |
|
public function setMultiple(array $key_values, $namespace, $ttl = null) { |
154
|
|
|
|
155
|
1 |
|
if ( $ttl == null ) $ttl = 0; |
156
|
|
|
|
157
|
1 |
|
$scope = $this->getNamespaceKey($namespace); |
158
|
|
|
|
159
|
1 |
|
if ( $scope === false ) $scope = $this->setNamespaceKey($namespace); |
|
|
|
|
160
|
|
|
|
161
|
1 |
|
if ( $scope === false ) return false; |
|
|
|
|
162
|
|
|
|
163
|
1 |
|
$shadowNames = []; |
164
|
|
|
|
165
|
1 |
|
foreach ( $key_values as $key => $value ) { |
166
|
1 |
|
$shadowNames["$scope-$key"] = $value; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
$data = apc_store($shadowNames, null, $ttl); |
170
|
|
|
|
171
|
1 |
|
return empty($data) ? true : false; |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
3 |
|
public function deleteMultiple(array $keys, $namespace) { |
179
|
|
|
|
180
|
3 |
|
$scope = $this->getNamespaceKey($namespace); |
181
|
|
|
|
182
|
3 |
|
if ( $scope === false ) return false; |
|
|
|
|
183
|
|
|
|
184
|
3 |
|
$shadowNames = array_map(function($key) use($scope) { |
185
|
3 |
|
return "$scope-$key"; |
186
|
3 |
|
}, $keys); |
187
|
|
|
|
188
|
3 |
|
$delete = apc_delete($shadowNames); |
189
|
|
|
|
190
|
3 |
|
return empty($delete) ? true : false; |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
5 |
|
public function has($key, $namespace) { |
198
|
|
|
|
199
|
5 |
|
$scope = $this->getNamespaceKey($namespace); |
200
|
|
|
|
201
|
5 |
|
if ( $scope === false ) return false; |
|
|
|
|
202
|
|
|
|
203
|
5 |
|
return apc_exists("$scope-$key"); |
|
|
|
|
204
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
2 |
|
public function stats() { |
211
|
|
|
|
212
|
2 |
|
return apc_cache_info("user", true); |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Set namespace key |
218
|
|
|
* |
219
|
|
|
* @return mixed |
220
|
|
|
*/ |
221
|
5 |
|
private function setNamespaceKey($namespace) { |
222
|
|
|
|
223
|
5 |
|
$uId = UniqueId::generate(64); |
224
|
|
|
|
225
|
5 |
|
return apc_store($namespace, $uId, 0) === false ? false : $uId; |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get namespace key |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
37 |
|
private function getNamespaceKey($namespace) { |
235
|
|
|
|
236
|
37 |
|
return apc_fetch($namespace); |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|