-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPCCache.class.php
373 lines (342 loc) · 10.4 KB
/
APCCache.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
// apc dependecy checks
if (!in_array('apc', get_loaded_extensions())) {
throw new Exception('APC extension needs to be installed.');
}
// json dependecy checks
if (!in_array('json', get_loaded_extensions())) {
throw new Exception('JSON extension needs to be installed.');
}
/**
* APCCache
*
* Provides accessors for reading, writing and flushing an apc-level
* cache/data-store.
*
* @author Oliver Nassar <[email protected]>
* @abstract
* @notes handles false-value caching through <json> encoding
* @todo implement prefixing
* @example
* <code>
* // dependency
* require_once APP . '/vendors/PHP-APCCache/APCCache.class.php';
* APCCache::init('namespace');
*
* // write to cache; read; exit script
* APCCache::write('oliver', 'nassar');
* echo APCCache::read('oliver');
* exit(0);
* </code>
*/
abstract class APCCache
{
/**
* _analytics
*
* APC cache request/writing statistics array.
*
* @var array
* @access protected
*/
protected static $_analytics = array(
'deletes' => 0,
'misses' => 0,
'reads' => 0,
'writes' => 0
);
/**
* _bypass
*
* @var boolean
* @access protected
*/
protected static $_bypass = false;
/**
* _namespace
*
* @var string
* @access protected
*/
protected static $_namespace;
/**
* _clean
*
* @access protected
* @static
* @param string $str
* @return string
*/
protected static function _clean($str)
{
$str = (self::$_namespace) . ($str);
return md5($str);
}
/**
* checkForFlushing
*
* @note If you are using apc for session storage, this will clear
* them!
* @access public
* @static
* @param string $key
* @return void
*/
public static function checkForFlushing($key)
{
if (isset($_GET[$key])) {
self::flush();
}
}
/**
* delete
*
* @access public
* @static
* @param string $key
* @param boolean $throwException (false)
* @return void
*/
public static function delete($key, $throwException = false)
{
// ensure namespace set
if (is_null(self::$_namespace)) {
throw new Exception('Namespace not set');
}
// safely attempt to delete from APC store
try {
// delete from store
$key = self::_clean($key);
$response = apc_delete($key);
if ($response === false) {
throw new Exception('Error deleting');
}
// increment statistic (after store call to allow for exception)
++self::$_analytics['deletes'];
} catch(Exception $exception) {
if ($throwException === true) {
throw new Exception(
'APCCache Error: Exception while attempting to delete ' .
'from store.'
);
}
}
}
/**
* init
*
* @access public
* @static
* @param string $namespace
* @return void
*/
public static function init($namespace)
{
self::$_namespace = $namespace;
}
/**
* flush
*
* Empties apc-level cache records and bytecode/opcode stores.
*
* @access public
* @static
* @return void
*/
public static function flush()
{
// safely try to flush resource
try {
apc_clear_cache();
apc_clear_cache('user');
} catch(Exception $exception) {
throw new Exception(
'APCCache Error: Exception while attempting to flush store.'
);
}
}
/**
* getDeletes
*
* @access public
* @static
* @return integer
*/
public static function getDeletes()
{
return self::$_analytics['deletes'];
}
/**
* getMisses
*
* Returns the number of apc-level missed cache reads.
*
* @access public
* @static
* @return integer number of read/fetch misses for APC requests
*/
public static function getMisses()
{
return self::$_analytics['misses'];
}
/**
* getReads
*
* Returns the number of apc-level successful cache reads.
*
* @access public
* @static
* @return integer number of read/fetch requests for APC
*/
public static function getReads()
{
return self::$_analytics['reads'];
}
/**
* getStats
*
* Returns an associative array of apc-level cache performance
* statistics.
*
* @access public
* @static
* @return array associative array of key APC statistics
*/
public static function getStats()
{
return self::$_analytics;
}
/**
* getWrites
*
* Returns the number of successful apc-level cache writes.
*
* @access public
* @static
* @return integer number of times a mixed value was written to APC
*/
public static function getWrites()
{
return self::$_analytics['writes'];
}
/**
* read
*
* Attempts to read an apc-level cache record, returning null if it
* couldn't be accessed. Handles false/null return value logic.
*
* @access public
* @static
* @param string $key key for the cache position
* @return mixed cache record value, or else null if it's not present
*/
public static function read($key)
{
// ensure namespace set
if (is_null(self::$_namespace)) {
throw new Exception('Namespace not set');
}
// safely attempt to read from APC store
try {
// Bypassing checking
if (self::$_bypass === true) {
++self::$_analytics['misses'];
return null;
}
// check apc
$key = self::_clean($key);
$response = apc_fetch($key);
// not found
if ($response === false) {
++self::$_analytics['misses'];
return null;
}
// increment apc-reads
++self::$_analytics['reads'];
// falsy value, not `not-found` value
if ($response === json_encode(false)) {
return false;
}
// previously set response
return $response;
} catch(Exception $exception) {
throw new Exception(
'APCache Error: Exception while attempting to read from ' .
'store.'
);
}
}
/**
* setupBypassing
*
* @access public
* @static
* @param string $key The key, which if found in _GET, will turn
* caching off
* @return void
*/
public static function setupBypassing($key)
{
if (isset($_GET[$key])) {
self::$_bypass = true;
}
}
/**
* write
*
* Writes a value to the apc-level cache, based on the passed in key.
* Handles false/null value storage logic.
*
* @access public
* @static
* @param string $key key for the cache value in the hash
* @param mixed $value value for the cache key, which cannot be an
* object or object reference
* @param integer $ttl. (default: 0) time to live (ttl) for the cache
* value, after which it won't be accessible in the store (in
* seconds)
* @return void
*/
public static function write($key, $value, $ttl = 0)
{
// ensure namespace set
if (is_null(self::$_namespace)) {
throw new Exception('Namespace not set');
}
// null value storage-attempt check
if ($value === null) {
throw new Exception(
'Cannot perform APCCache write: attempted to store null' .
'value in key *' . ($key) . '*.');
}
/**
* <false> string storage-attempt check, which conflicts with method
* of storing boolean false-value
*/
elseif ($value === 'false') {
throw new Exception(
'Cannot perform APCCache write: attempted to store string' .
'value of *false* in key *' . ($key) . '*.');
}
// false value check, and encoding
elseif ($value === false) {
$value = json_encode($value);
}
// safely attempt to write to APC store
try {
// write to store
$key = self::_clean($key);
$response = apc_store($key, $value, $ttl);
if ($response === false) {
throw new Exception('Error writing');
}
// increment statistic (after store call to allow for exception)
++self::$_analytics['writes'];
} catch(Exception $exception) {
throw new Exception(
'APCCache Error: Exception while attempting to write to' .
'store.'
);
}
}
}