/
class.cache.php
821 lines (683 loc) · 25.9 KB
/
class.cache.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
<?php
/**
* Cache layer base class
*
* All cache objects should extend this to ensure a consistent public api for
* caching.
*
* @author Tim Gunter <tim@vanillaforums.com>
* @copyright 2009-2015 Vanilla Forums Inc.
* @license http://www.opensource.org/licenses/gpl-2.0.php GNU GPL v2
* @package Core
* @since 2.0.10
* @abstract
*/
abstract class Gdn_Cache {
/** @var array List of cache containers. */
protected $containers;
/** @var array List of features this cache system supports. */
protected $features;
/** @var string Type of cache this this: one of CACHE_TYPE_MEMORY, CACHE_TYPE_FILE, CACHE_TYPE_NULL. */
protected $cacheType;
/** @var array Memory copy of store containers. */
protected static $stores = array();
/** Allows items to be internally compressed/decompressed. */
const FEATURE_COMPRESS = 'f_compress';
/** Allows items to auto-expire (seconds). */
const FEATURE_EXPIRY = 'f_expiry';
/** Allows set/get timeouts (seconds). */
const FEATURE_TIMEOUT = 'f_timeout';
/** Allows disabling usage of key prefix. */
const FEATURE_NOPREFIX = 'f_noprefix';
/** Allows forcing alternate key prefix. */
const FEATURE_FORCEPREFIX = 'f_forceprefix';
/** Allows querying DB for missing keys, or firing a callback. */
const FEATURE_FALLBACK = 'f_fallback';
/** In incr/decr ops, what should the initial value be. */
const FEATURE_INITIAL = 'f_initial';
/** Allows sharding large keys across all servers [Add,Store,Get,Replace,Remove]. */
const FEATURE_SHARD = 'f_shard';
/** Allows control over localcache usage. */
const FEATURE_LOCAL = 'f_local';
/** Location - SERVER:IP, Filepath, etc. */
const CONTAINER_LOCATION = 'c_location';
/** Persistent - Whether to use connect() or pconnect() where applicable. */
const CONTAINER_PERSISTENT = 'c_persistent';
/** Pool Size - When using pconnect(), how many connections should we use in the pool? */
const CONTAINER_POOLSIZE = 'c_poolsize';
/** Pool Key - When using pconnect(), what should the pool key look like? */
const CONTAINER_POOLKEY = 'c_poolkey';
/** Weight - Allows for differently weighted storage locations. */
const CONTAINER_WEIGHT = 'c_weight';
/** Persistent - Retry delay inverval in seconds. */
const CONTAINER_RETRYINT = 'c_retryint';
/** Timeout - How long to wait before timing out while connecting. */
const CONTAINER_TIMEOUT = 'c_timeout';
/** Online - If this container is available for requests. */
const CONTAINER_ONLINE = 'c_online';
/** Callback - Method to call if the location fails to be added. */
const CONTAINER_CALLBACK = 'c_callback';
/** Cache status. */
const CACHEOP_FAILURE = false;
/** Cache status. */
const CACHEOP_SUCCESS = true;
/** Cache type. */
const CACHE_TYPE_MEMORY = 'ct_memory';
/** Cache type. */
const CACHE_TYPE_FILE = 'ct_file';
/** Cache type. */
const CACHE_TYPE_NULL = 'ct_null';
/** Seconds. */
const CACHE_EJECT_DURATION = 60;
/** Seconds. */
const APC_CACHE_DURATION = 300;
/** Max number of shards. 0 = no limit */
const CACHE_SHARD_MAX_SHARDS = 0;
/** Min size for a shard, in bytes. 0 = no limit */
const CACHE_SHARD_MIN_SIZE = 10000;
/** Auto shard keys that are larger than this, in bytes. */
const CACHE_SHARD_AUTO_SIZE = 100000;
/** @var array Local in-memory cache of fetched data. This prevents duplicate gets to memcache. */
protected static $localCache = array();
/** @var bool */
public static $trace = true;
/** @var array */
public static $trackGet = array();
/** @var int */
public static $trackGets = 0;
/** @var array */
public static $trackSet = array();
/** @var int */
public static $trackSets = 0;
/** @var int */
public static $trackTime = 0;
/**
*
*/
public function __construct() {
$this->containers = array();
$this->features = array();
}
/**
* Determines the currently installed cache solution and returns a fresh instance of its object.
*
* @return Gdn_Cache
*/
public static function initialize($ForceEnable = false, $ForceMethod = false) {
$AllowCaching = self::activeEnabled($ForceEnable);
$ActiveCache = Gdn_Cache::activeCache();
if ($ForceMethod !== false) {
$ActiveCache = $ForceMethod;
}
$ActiveCacheClass = 'Gdn_'.ucfirst($ActiveCache);
if (!$AllowCaching || !$ActiveCache || !class_exists($ActiveCacheClass)) {
$CacheObject = new Gdn_Dirtycache();
} else {
$CacheObject = new $ActiveCacheClass();
}
// Null caches should not acount as enabled.
if (!$ForceEnable && $CacheObject->type() === Gdn_Cache::CACHE_TYPE_NULL) {
SaveToConfig('Cache.Enabled', false, false);
}
if (method_exists($CacheObject, 'Autorun')) {
$CacheObject->autorun();
}
// This should only fire when cache is loading automatically
if (!func_num_args() && Gdn::pluginManager() instanceof Gdn_PluginManager) {
Gdn::pluginManager()->fireEvent('AfterActiveCache');
}
return $CacheObject;
}
/**
* Gets the short name of the currently active cache.
*
* This method retrieves the name of the active cache according to the config file.
* It fires an event thereafter, allowing that value to be overridden
* by loaded plugins.
*
* @return string shortname of current auto active cache
*/
public static function activeCache() {
/*
* There is a catch 22 with caching the config file. We need
* an external way to define the cache layer before needing it
* in the config.
*/
if (defined('CACHE_METHOD_OVERRIDE')) {
$ActiveCache = CACHE_METHOD_OVERRIDE;
} else {
$ActiveCache = C('Cache.Method', false);
}
// This should only fire when cache is loading automatically
if (!func_num_args() && Gdn::pluginManager() instanceof Gdn_PluginManager) {
Gdn::pluginManager()->EventArguments['ActiveCache'] = &$ActiveCache;
Gdn::pluginManager()->fireEvent('BeforeActiveCache');
}
return $ActiveCache;
}
/**
* Get the status of the active cache.
*
* Return whether or not the current cache method is enabled.
*
* @param type $ForceEnable
* @return bool status of active cache
*/
public static function activeEnabled($ForceEnable = false) {
$AllowCaching = false;
if (defined('CACHE_ENABLED_OVERRIDE')) {
$AllowCaching |= CACHE_ENABLED_OVERRIDE;
}
$AllowCaching |= C('Cache.Enabled', false);
$AllowCaching |= $ForceEnable;
return (bool)$AllowCaching;
}
/**
* Returns the storage data for the active cache.
*
* For FileCache, the folder. For Memcache, the server(s).
*
* @param type $ForceMethod
* @return mixed Active Store Location
*/
public static function activeStore($ForceMethod = null) {
// Get the active cache name
$ActiveCache = self::activeCache();
if (!is_null($ForceMethod)) {
$ActiveCache = $ForceMethod;
}
$ActiveCache = ucfirst($ActiveCache);
// Overrides
if (defined('CACHE_STORE_OVERRIDE') && defined('CACHE_METHOD_OVERRIDE') && CACHE_METHOD_OVERRIDE == $ActiveCache) {
return unserialize(CACHE_STORE_OVERRIDE);
}
// Use APC cache?
$apc = false;
if (C('Garden.Apc', false) && C('Garden.Cache.ApcPrecache', false) && function_exists('apc_fetch')) {
$apc = true;
}
$LocalStore = null;
$ActiveStore = null;
$ActiveStoreKey = "Cache.{$ActiveCache}.Store";
// Check memory
if (is_null($LocalStore)) {
if (array_key_exists($ActiveCache, Gdn_Cache::$stores)) {
$LocalStore = Gdn_Cache::$stores[$ActiveCache];
}
}
// Check APC cache
if (is_null($LocalStore) && $apc) {
$LocalStore = apc_fetch($ActiveStoreKey);
if ($LocalStore) {
Gdn_Cache::$stores[$ActiveCache] = $LocalStore;
}
}
if (is_array($LocalStore)) {
// Convert to ActiveStore format (with 'Active' key)
$Save = false;
$ActiveStore = array();
foreach ($LocalStore as $StoreServerName => &$StoreServer) {
$IsDelayed = &$StoreServer['Delay'];
$IsActive = &$StoreServer['Active'];
if (is_numeric($IsDelayed)) {
if ($IsDelayed < time()) {
$IsActive = true;
$IsDelayed = false;
$StoreServer['Fails'] = 0;
$Save = true;
} else {
if ($IsActive) {
$IsActive = false;
$Save = true;
}
}
}
// Add active servers to ActiveStore array
if ($IsActive) {
$ActiveStore[] = $StoreServer['Server'];
}
}
}
// No local copy, get from config
if (is_null($ActiveStore)) {
$ActiveStore = c($ActiveStoreKey, false);
// Convert to LocalStore format
$LocalStore = array();
$ActiveStore = (array)$ActiveStore;
foreach ($ActiveStore as $StoreServer) {
$StoreServerName = md5($StoreServer);
$LocalStore[$StoreServerName] = array(
'Server' => $StoreServer,
'Active' => true,
'Delay' => false,
'Fails' => 0
);
}
$Save = true;
}
if ($Save) {
// Save to memory
Gdn_Cache::$stores[$ActiveCache] = $LocalStore;
// Save back to APC for later
if ($apc) {
apc_store($ActiveStoreKey, $LocalStore, Gdn_Cache::APC_CACHE_DURATION);
}
}
return $ActiveStore;
}
/**
* Register a temporary server connection failure.
*
* This method will attempt to temporarily excise the offending server from
* the connect roster for a period of time.
*
* @param string $server
*/
public function fail($server) {
// Use APC?
$apc = false;
if (C('Garden.Apc', false) && function_exists('apc_fetch')) {
$apc = true;
}
// Get the active cache name
$activeCache = Gdn_Cache::activeCache();
$activeCache = ucfirst($activeCache);
$activeStoreKey = "Cache.{$activeCache}.Store";
// Get the local store.
$localStore = val($activeCache, Gdn_Cache::$stores, null);
if (is_null($localStore)) {
Gdn_Cache::activeStore();
$localStore = val($activeCache, Gdn_Cache::$stores, null);
if (is_null($localStore)) {
return false;
}
}
$storeServerName = md5($server);
if (!array_key_exists($storeServerName, $localStore)) {
return false;
}
$storeServer = &$localStore[$storeServerName];
$isActive = &$storeServer['Active'];
if (!$isActive) {
return false;
}
$fails = &$storeServer['Fails'];
$fails++;
$active = $isActive ? 'active' : 'inactive';
// Check if we need to deactivate for 5 minutes
if ($isActive && $storeServer['Fails'] > 3) {
$isActive = false;
$storeServer['Delay'] = time() + Gdn_Cache::CACHE_EJECT_DURATION;
}
// Save
Gdn_Cache::$stores[$activeCache] = $localStore;
// Save to APC
if ($apc) {
apc_store($activeStoreKey, $localStore, Gdn_Cache::APC_CACHE_DURATION);
}
return true;
}
/**
* Returns a constant describing the type of cache implementation this object represents.
*
* @return string Type of cache. One of CACHE_TYPE_MEMORY, CACHE_TYPE_FILE, CACHE_TYPE_NULL
*/
public function type() {
return $this->cacheType;
}
/**
* Add a value to the cache.
*
* This fails if the item already exists in the cache.
*
* @param string $Key Cache key used for storage
* @param mixed $Value Value to be cached
* @param array $Options
* @return boolean true on success or false on failure.
*/
abstract public function add($Key, $Value, $Options = array());
public function stripKey($Key, $Options) {
$UsePrefix = !val(Gdn_Cache::FEATURE_NOPREFIX, $Options, false);
$ForcePrefix = val(Gdn_Cache::FEATURE_FORCEPREFIX, $Options, null);
if ($UsePrefix) {
$Key = substr($Key, strlen($this->getPrefix($ForcePrefix)) + 1);
}
return $Key;
}
/**
* Store a value in the cache.
*
* This works regardless of whether the item already exists in the cache.
*
* @param string $Key Cache key used for storage.
* @param mixed $Value Value to be cached.
* @param array $Options An array of cache feature constants.
* - FEATURE_COMPRESS: Allows items to be internally compressed/decompressed (bool).
* - FEATURE_EXPIRY: Allows items to autoexpire (seconds).
* - FEATURE_NOPREFIX: Allows disabling usage of key prefix (bool).
* - FEATURE_FORCEPREFIX: Allows forcing alternate key prefix (string).
* - FEATURE_FALLBACK: Allows querying DB for missing keys, or firing a callback (see Gdn_Cache->Fallback).
* @return boolean true on success or false on failure.
*/
abstract public function store($Key, $Value, $Options = array());
/**
* Check if a value exists in the cache.
*
* @param string $Key Cache key used for storage.
* @return array array(key => value) for existing key or false if not found.
*/
abstract public function exists($Key);
/**
* Retrieve a key's value from the cache.
*
* @param string $Key Cache key used for storage.
* @param array $Options
* @return mixed key value or false on failure or not found.
*/
abstract public function get($Key, $Options = array());
/**
* Remove a key/value pair from the cache.
*
* @param string $Key Cache key used for storage.
* @param array $Options
* @return boolean true on success or false on failure.
*/
abstract public function remove($Key, $Options = array());
/**
* Replace an existing key's value with the provided value.
*
* This will fail if the provided key does not already exist.
*
* @param string $Key Cache key used for storage.
* @param mixed $Value Value to be cached.
* @param array $Options
* @return boolean true on success or false on failure.
*/
abstract public function replace($Key, $Value, $Options = array());
/**
* Increment the value of the provided key by {@link $Amount}.
*
* This will fail if the key does not already exist. Cannot take the value
* of $Key below 0.
*
* @param string $Key Cache key used for storage.
* @param int $Amount Amount to shift value up.
* @return int new value or false on failure.
*/
abstract public function increment($Key, $Amount = 1, $Options = array());
/**
* Decrement the value of the provided key by {@link $Amount}.
*
* This will fail if the key does not already exist. Cannot take the value
* of $Key below 0.
*
* @param string $Key Cache key used for storage.
* @param int $Amount Amount to shift value down.
* @return int new value or false on failure.
*/
abstract public function decrement($Key, $Amount = 1, $Options = array());
/**
* Add a container to the cache pool.
*
* @param array $Options An array of options with container constants as keys.
* - CONTAINER_LOCATION: required. the location of the container. SERVER:IP, Filepath, etc.
* - CONTAINER_PERSISTENT: optional (default true). whether to use connect() or pconnect() where applicable.
* - CONTAINER_WEIGHT: optional (default 1). number of buckets to create for this server which in turn control its probability of it being selected.
* - CONTAINER_RETRYINT: optional (default 15s). controls how often a failed container will be retried, the default value is 15 seconds.
* - CONTAINER_TIMEOUT: optional (default 1s). amount of time to wait for connection to container before timing out.
* - CONTAINER_CALLBACK: optional (default null). callback to execute if container fails to open/connect.
* @return boolean true on success or false on failure.
*/
abstract public function addContainer($Options);
/**
* Invalidate all items in the cache.
*
* Gdn_Cache::Flush() invalidates all existing cache items immediately.
* After invalidation none of the items will be returned in response to a
* retrieval command (unless it's stored again under the same key after
* Gdn_Cache::Flush() has invalidated the items).
*
* @return boolean true on success of false on failure.
*/
abstract public function flush();
/**
*
*
* @param string $Key Cache key.
* @param array $Options
* @return mixed
*/
protected function fallback($Key, $Options) {
$Fallback = val(Gdn_Cache::FEATURE_FALLBACK, $Options, null);
if (is_null($Fallback)) {
return Gdn_Cache::CACHEOP_FAILURE;
}
$FallbackType = array_shift($Fallback);
switch ($FallbackType) {
case 'query':
$QueryFallbackField = array_shift($Fallback);
$QueryFallbackCode = array_shift($Fallback);
$FallbackResult = Gdn::database()->query($QueryFallbackCode);
if ($FallbackResult->numRows()) {
if (!is_null($QueryFallbackField)) {
$FallbackResult = val($QueryFallbackField, $FallbackResult->firstRow(DATASET_TYPE_ARRAY));
} else {
$FallbackResult = $FallbackResult->resultArray();
}
}
break;
case 'callback':
$CallbackFallbackMethod = array_shift($Fallback);
$CallbackFallbackArgs = $Fallback;
$FallbackResult = call_user_func_array($CallbackFallbackMethod, $CallbackFallbackArgs);
break;
}
Gdn::cache()->store($Key, $FallbackResult);
return $FallbackResult;
}
public function getPrefix($ForcePrefix = null, $WithRevision = true) {
static $ConfigPrefix = false;
// Allow overriding the prefix
if (!is_null($ForcePrefix)) {
return $ForcePrefix;
}
// Keep searching for the prefix until it is defined
if ($ConfigPrefix === false) {
// Allow vfcom-infrastructure to set the prefix automatically
if (defined('FORCE_CACHE_PREFIX')) {
$ConfigPrefix = FORCE_CACHE_PREFIX;
}
if ($ConfigPrefix === false) {
$ConfigPrefix = C('Cache.Prefix', false);
}
}
// Lookup Revision if we have a prefix.
$RevisionNumber = false;
if ($WithRevision && $ConfigPrefix !== false) {
$CacheRevision = $this->getRevision($ConfigPrefix);
if (!is_null($CacheRevision)) {
$RevisionNumber = $CacheRevision;
}
}
$Response = $ConfigPrefix;
if ($WithRevision && $RevisionNumber !== false && $ConfigPrefix !== false) {
$Response .= ".rev{$RevisionNumber}";
}
return ($ConfigPrefix === false) ? null : $Response;
}
public function getRevision($ForcePrefix = null, $Force = false) {
static $CacheRevision = false;
if ($CacheRevision === false || $Force) {
$ConfigPrefix = $ForcePrefix;
if (is_null($ConfigPrefix)) {
$ConfigPrefix = $this->getPrefix(null, false);
}
$CacheRevisionKey = "{$ConfigPrefix}.Revision";
$CacheRevision = $this->get($CacheRevisionKey, array(
Gdn_Cache::FEATURE_NOPREFIX => true
));
if ($CacheRevision === Gdn_Cache::CACHEOP_FAILURE) {
$CacheRevision = 1;
}
}
return $CacheRevision;
}
public function incrementRevision() {
$CachePrefix = $this->getPrefix(null, false);
if ($CachePrefix === false) {
return false;
}
$CacheRevisionKey = "{$CachePrefix}.Revision";
$Incremented = $this->increment($CacheRevisionKey, 1, array(
Gdn_Cache::FEATURE_NOPREFIX => true
));
if (!$Incremented) {
return $this->store($CacheRevisionKey, 2, array(
Gdn_Cache::FEATURE_NOPREFIX => true
));
}
return true;
}
public function makeKey($Key, $Options) {
$UsePrefix = !val(Gdn_Cache::FEATURE_NOPREFIX, $Options, false);
$ForcePrefix = val(Gdn_Cache::FEATURE_FORCEPREFIX, $Options, null);
$Prefix = '';
if ($UsePrefix) {
$Prefix = $this->getPrefix($ForcePrefix).'!';
}
if (is_array($Key)) {
$Result = array();
foreach ($Key as $i => $v) {
$Result[$i] = $Prefix.$v;
}
} else {
$Result = $Prefix.$Key;
}
return $Result;
}
/*
* Get the value of a store-specific option.
*
* The option keys are specific to the active cache type, but are always
* stored under $Configuration['Cache'][ActiveCacheName]['Option'][*].
*
* @param string|integer $Option The option key to retrieve
* @return mixed The value associated with the given option key
*/
public function option($Option = null, $Default = null) {
static $ActiveOptions = null;
if (is_null($ActiveOptions)) {
$ActiveCacheShortName = ucfirst($this->activeCache());
$OptionKey = "Cache.{$ActiveCacheShortName}.Option";
$ActiveOptions = c($OptionKey, array());
}
if (is_null($Option) || !array_key_exists($Option, $ActiveOptions)) {
return $ActiveOptions;
}
return val($Option, $ActiveOptions, $Default);
}
/*
* Get the value of a store-specific config
*
* The option keys are generic and cross-cache, but are always
* stored under $Configuration['Cache'][ActiveCacheName]['Config'][*].
*
* @param string|integer $Key The config key to retrieve
* @return mixed The value associated with the given config key
*/
public function config($Key = null, $Default = null) {
static $ActiveConfig = null;
if (is_null($ActiveConfig)) {
$ActiveCacheShortName = ucfirst($this->activeCache());
$ConfigKey = "Cache.{$ActiveCacheShortName}.Config";
$ActiveConfig = c($ConfigKey, array());
}
if (is_null($Key)) {
return $ActiveConfig;
}
if (!array_key_exists($Key, $ActiveConfig)) {
return $Default;
}
return val($Key, $ActiveConfig, $Default);
}
/**
*
*/
public static function trace($trace = null) {
if (!is_null($trace)) {
Gdn_Cache::$trace = (bool)$trace;
}
return Gdn_Cache::$trace;
}
protected function localGet($key) {
if (!$this->hasFeature(Gdn_Cache::FEATURE_LOCAL)) {
return Gdn_Cache::CACHEOP_FAILURE;
}
if (!array_key_exists($key, self::$localCache)) {
return Gdn_Cache::CACHEOP_FAILURE;
}
return self::$localCache[$key];
}
protected function localSet($key, $value = null) {
if (!$this->hasFeature(Gdn_Cache::FEATURE_LOCAL)) {
return;
}
if (!is_array($key)) {
$key = array($key => $value);
}
self::$localCache = array_merge(self::$localCache, $key);
}
/**
* Clear local cache (process memory cache).
*/
protected static function localClear() {
self::$localCache = array();
}
/**
* Flag this cache as being capable of performing a feature.
*
* FEATURE_COMPRESS: this cache can compress and decompress values on the fly
* FEATURE_TIMEOUT: this cache can timeout while reading / writing
* FEATURE_EXPIRY: this cache can expire keys
*
* @param int $Feature One of the feature constants.
* @param mixed $Meta An optional data to return when calling HasFeature. default true.
*/
public function registerFeature($Feature, $Meta = true) {
$this->features[$Feature] = $Meta;
}
/**
* Remove feature flag from this cache, for the specific feature.
*
* @param int $Feature One of the feature constants.
*/
public function unregisterFeature($Feature) {
if (isset($this->features[$Feature])) {
unset($this->features[$Feature]);
}
}
/**
* Check whether this cache supports the specified feature.
*
* @param int $Feature One of the feature constants.
* @return mixed $Meta returns the meta data supplied during RegisterFeature().
*/
public function hasFeature($Feature) {
return isset($this->features[$Feature]) ? $this->features[$Feature] : Gdn_Cache::CACHEOP_FAILURE;
}
/**
* Is the current cache available?
*
* @return boolean
*/
public function online() {
return true;
}
protected function failure($Message) {
if (debug()) {
throw new Exception($Message);
} else {
return Gdn_Cache::CACHEOP_FAILURE;
}
}
}