-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.ddwinucache.php
606 lines (551 loc) · 16.4 KB
/
class.ddwinucache.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<?php
/**
* Copyright (c) 2010, Daniel Doezema
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the contributors and/or copyright holder may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL DANIEL DOEZEMA BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class DDWinUCacheException extends Exception {}
/**
* Class for interacting with WinCache's User Cache functionality.
*
* @copyright Copyright (c) 2010 Daniel Doezema. (http://dan.doezema.com)
* @license http://dan.doezema.com/licenses/new-bsd New BSD License
*/
class DDWinUCache implements ArrayAccess {
/**
* The TTL in seconds used when $ttl is omitted in $this->set();
* @var int
*/
protected $default_ttl = 0; // 0 = Never Expires = Wincache Default
/**
* Results from wincache_ucache_info();
*
* @var array
*/
protected $info_array = array();
/**
* Results from wincache_ucache_meminfo();
*
* @var array
*/
protected $mem_info_array = array();
/**
* Success result from the last $this->dec();
*
* @var bool
*/
protected $last_dec_result = false;
/**
* Success result from the last $this->inc();
*
* @var bool
*/
protected $last_inc_result = false;
/**
* Success result from the last $this->get();
*
* @var bool
*/
protected $last_get_result = false;
/**
* Used to determine if a search string is a regular expression.
*
* @var string
*/
protected $reg_exp_delimiter = '/';
/**
* Singleton :: The DDWinUCache instance.
*
* @var DDWinUCache
*/
protected static $instance;
/**
* Returns a single instance of DDWinUCache within the current request's global scope.
*
* @return DDWinUCache
*/
public static function getInstance() {
if((self::$instance instanceof DDWinUCache) == false) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Creates a WinUCache instance.
*
* @throws Exception When the WinCache version >= 1.1.0 is not installed.
*/
protected function __construct() {
if(!$this->isWinUCacheInstalled()) {
throw new DDWinUCacheException('WinCache version >= 1.1.0 is not installed.');
}
}
/**
* Get the default TTL used when $ttl is omitted in $this->set();
*
* @return int
*/
public function getDefaultTTL() {
return $this->default_ttl;
}
/**
* A shortcut to the wincache_ucahce_info()['ucache_entries'] array.
*
* @see wincache_ucache_meminfo();
* @return array
*/
public function getEntriesArray() {
$info_array = $this->getInfoArray();
return isset($info_array['ucache_entries']) ? $info_array['ucache_entries'] : array();
}
/**
* Get the success result of the last $this->dec() call.
*
* @return bool
*/
public function getLastDecResult() {
return $this->last_dec_result;
}
/**
* Get the success result of the last $this->get() call.
*
* @return bool
*/
public function getLastGetResult() {
return $this->last_get_result;
}
/**
* Get the success result of the last $this->inc() call.
*
* @return bool
*/
public function getLastIncResult() {
return $this->last_inc_result;
}
/**
* Get the amount of free memory in bytes available for the user cache.
*
* @see wincache_ucache_meminfo();
* @return int
*/
public function getMemoryFree() {
$mem_info_array = $this->getMemInfoArray();
return $mem_info_array['memory_free'];
}
/**
* Get the number of free memory blocks available for the user cache.
*
* @see wincache_ucache_meminfo();
* @return int
*/
public function getMemoryFreeBlocks() {
$mem_info_array = $this->getMemInfoArray();
return $mem_info_array['num_free_blks'];
}
/**
* Get the amount of memory in bytes used for the user cache internal structures.
*
* @see wincache_ucache_meminfo();
* @return int
*/
public function getMemoryOverhead() {
$mem_info_array = $this->getMemInfoArray();
return $mem_info_array['memory_overhead'];
}
/**
* Get the amount of memory in bytes allocated for the user cache.
*
* @see wincache_ucache_meminfo();
* @return int
*/
public function getMemoryTotal() {
$mem_info_array = $this->getMemInfoArray();
return $mem_info_array['memory_total'];
}
/**
* Get the percentage of memory used for the user cache.
*
* @param int
* @return float
*/
public function getMemoryUsedPercent($precision = 2) {
return round((($this->getMemoryUsed() / $this->getMemoryTotal()) * 100), (int) $precision);
}
/**
* Get the amount of memory in bytes used by the user cache.
*
* @return int
*/
public function getMemoryUsed() {
return ($this->getMemoryTotal() - $this->getMemoryFree());
}
/**
* Get the number of memory blocks used by the user cache.
*
* @see wincache_ucache_meminfo();
* @return int
*/
public function getMemoryUsedBlocks() {
$mem_info_array = $this->getMemInfoArray();
return $mem_info_array['num_used_blks'];
}
/**
* Get the number of times the data has been served from the the user cache.
*
* @see wincache_ucache_info();
* @return int
*/
public function getTotalHitCount() {
$info_array = $this->getInfoArray();
return $info_array['total_hit_count'];
}
/**
* Get the total number of elements that are currently in the user cache.
*
* @see wincache_ucache_info();
* @return int
*/
public function getTotalItemCount() {
$info_array = $this->getInfoArray();
return $info_array['total_item_count'];
}
/**
* Get the number of times the data has not been found in the user cache.
*
* @see wincache_ucache_info();
* @return int
*/
public function getTotalMissCount() {
$info_array = $this->getInfoArray();
return $info_array['total_miss_count'];
}
/**
* Get the total time in seconds that the user cache has been active.
*
* @see wincache_ucache_info();
* @return int
*/
public function getTotalUptime() {
$info_array = $this->getInfoArray();
return $info_array['total_cache_uptime'];
}
/**
* Checks if cache is local.
*
* true if the cache metadata is for a local cache instance,
* false if the metadata is for the global cache.
*
* @see wincache_ucache_info();
* @return bool
*/
public function isLocalCache() {
$info_array = $this->getInfoArray();
return $info_array['is_local_cache'];
}
/**
* @see wincache_ucache_add();
*/
public function add($key, $value, $ttl = 0) {
$result = wincache_ucache_add($key, $value, $ttl);
$this->clearInfoArrays();
return $result;
}
/**
* @see wincache_ucache_clear();
*/
public function clear() {
$result = wincache_ucache_clear();
$this->clearInfoArrays();
return $result;
}
/**
* Decrements the value associated with the key.
*
* The third parameter has been replaced by $this->getLastDecResult();
*
* @see wincache_ucache_dec();
*/
public function dec($key, $dec_by = 1) {
$result = wincache_ucache_dec($key, $dec_by, $this->lastDecResult);
$this->clearInfoArrays();
return $result;
}
/**
* Delete a cache entry via three possible methods...
* + Pass a Regular Expression (PCRE) string.
* + Requirement: $this->reg_exp_delimiter must be the delimiter.
* + Pass a search string with *unix based wild cards (* and ?).
* + Pass the key name of the cache entry.
*
* @see wincache_ucache_del();
* @param string;
* @return array; The number of cache entries deleted.
*/
public function delete($search) {
$reg_exp = false;
if($this->isSearchRegExp($search)) {
$reg_exp = $search;
} else if($this->isSearchWildCard($search)) {
$reg_exp = $this->getWildCardRegExp($search);
}
$key = $search;
if(is_string($reg_exp) && (count($cache_ids_array = $this->getCacheIdsByRegExp($reg_exp)) > 0)) {
$key = $cache_ids_array;
}
if($result = wincache_ucache_delete($key)) {
$this->clearInfoArrays();
}
return $result;
}
/**
* @see wincache_ucache_exists();
*/
public function exists($key) {
return wincache_ucache_exists($key);
}
/**
* Gets a variable stored in the user cache.
*
* The second parameter has been replaced by $this->getLastGetResult();
*
* @see wincache_ucache_get();
*/
public function get($key) {
return wincache_ucache_get($key, $this->last_get_result);
}
/**
* Increments the value associated with the key
*
* The third parameter has been replaced by $this->getLastIncResult();
*
* @see wincache_ucache_inc();
*/
public function inc($key, $inc_by = 1) {
$result = wincache_ucache_inc($key, $inc_by, $this->last_inc_result);
$this->clearInfoArrays();
return $result;
}
/**
* @see wincache_ucache_info();
*/
public function info($summary_only = false, $key = null) {
if(is_string($key)) {
return wincache_ucache_info($summary_only, $key);
}
return wincache_ucache_info($summary_only);
}
/**
* @see wincache_ucache_meminfo();
*/
public function memInfo() {
return wincache_ucache_meminfo();
}
/**
* @see wincache_ucache_set();
*/
public function set($key, $value, $ttl = null) {
$result = wincache_ucache_set($key, $value, (is_null($ttl) ? $this->getDefaultTTL() : $ttl));
$this->clearInfoArrays();
return $result;
}
/**
* Set the default TTL used when $ttl is omitted in $this->set();
*
* @param int; time in seconds
* @return void
*/
public function setDefaultTTL($ttl) {
$this->default_ttl = $ttl;
}
/**
* Internal method that returns $this->info_array.
*
* If the array is not populated it will be lazy loaded.
*
* @return array
*/
protected function getInfoArray() {
if(!$this->hasInfo()) {
$this->populateInfoArray();
}
return $this->info_array;
}
/**
* Internal method that checks if $this->info_array has been populated.
*
* @return bool
*/
protected function hasInfo() {
return isset($this->info_array['ucache_entries']);
}
/**
* Internal method that clears $this->info_array.
*
* @return void
*/
protected function clearInfo() {
$this->info_array = array();
}
/**
* Internal method that populates $this->info_array.
*
* This internal array is used to avoid extra calls to wincache_ucahce_info();
*
* @return bool
*/
protected function populateInfoArray() {
$this->info_array = $this->info();
return $this->hasInfo();
}
/**
* Internal method that returns $this->mem_info_array.
*
* If the array is not populated it will be lazy loaded.
*
* @return array
*/
protected function getMemInfoArray() {
if(!$this->hasMemInfo()) {
$this->populateMemInfoArray();
}
return $this->mem_info_array;
}
/**
* Internal method that checks if $this->mem_info_array has been populated.
*
* @return bool
*/
protected function hasMemInfo() {
return isset($this->mem_info_array['memory_total']);
}
/**
* Internal method that clears $this->mem_info_array.
*
* @return void
*/
protected function clearMemInfo() {
$this->mem_info_array = array();
}
/**
* Internal method that populates $this->mem_info_array.
*
* This internal array is used to avoid extra calls to wincache_ucahce_meminfo();
*
* @return bool
*/
protected function populateMemInfoArray() {
$this->mem_info_array = $this->memInfo();
return $this->hasMemInfo();
}
/**
* Internal shortcut method that clears both $this->info_array and $this->mem_info_array.
*
* @return void
*/
protected function clearInfoArrays() {
$this->clearInfo();
$this->clearMemInfo();
}
/**
* Internal method to test if the proper version of WinCache is installed.
*
* @return bool
*/
protected function isWinUCacheInstalled() {
return function_exists('wincache_ucache_info');
}
/**
* Internal method to get an array of cache entry
* key_names that match a passed regular expression.
*
* @param string
* @return array
*/
protected function getCacheIdsByRegExp($reg_exp) {
$results = array();
foreach($this->getEntriesArray() as $key => $entry) {
if(preg_match($reg_exp, $entry['key_name'])) {
$results[$key] = $entry['key_name'];
}
}
return $results;
}
/**
* Internal method to create the regular expression
* string from a *unix wild card based search string.
*
* @param string
* @return string
*/
protected function getWildCardRegExp($string) {
$reg_exp = '/';
$reg_exp .= (substr($string,0,1) != '*') ? '^' : '';
$reg_exp .= str_replace(array('*','?'), array('.*?','.{1}'), $string);
$reg_exp .= (substr($string,-1) != '*') ? '$' : '';
$reg_exp .= '/';
return $reg_exp;
}
/**
* Internal method to check if a passed search string is a regular
* expression based on the presence of $this->reg_exp_delimiter
* at the beginning of the string.
*
* @param string
* @return bool
*/
protected function isSearchRegExp($string) {
return (substr($string, 0, 1) == $this->reg_exp_delimiter);
}
/**
* Internal method to check if a passed search string contains
* any *unix based wild card (* or ?).
*
* @param string
* @return bool
*/
protected function isSearchWildCard($string) {
return (strpos($string, '*') !== false) || (strpos($string, '?') !== false);
}
/**
* ArrayAccess Methods
*/
public function offsetSet($offset, $value) {
if (is_null($offset)) {
throw new DDWinUCacheException('A key_name must be given.');
} else {
$this->set($offset, $value);
}
}
public function offsetExists($offset) {
return $this->exists($offset) ? true : false;
}
public function offsetUnset($offset) {
$this->delete($offset);
}
public function offsetGet($offset) {
$valueMix = $this->get($offset);
return $this->getLastGetResult() ? $valueMix : null;
}
}