54 if (array_key_exists(
"file_backed_cache_directory", $config))
58 else if (ini_get(
"apc.enabled") && PHP_SAPI !==
'cli')
60 if (PHP_MAJOR_VERSION >= 7)
69 else if (ini_get(
"wincache.ucenabled") && PHP_SAPI !==
'cli')
88 static function get($key)
106 static function put($key, $obj, $ttl = 0)
168 trace(
"Creating Simple In-memory Cache", 4);
169 $this->cache = array();
174 return $this->cache[$key];
177 function put($key, $obj, $ttl = 0)
179 $this->cache[$key] = $obj;
186 unset($this->cache[$key]);
192 $this->pattern = $pattern;
194 unset($this->pattern);
203 return !preg_match(
"/".$this->pattern.
"/", $key);
208 $this->cache = array();
213 throw new FakoliException(
"Not implemented");
227 trace(
"Creating APC Cache", 4);
234 $s = apc_fetch($key, $success);
235 if (!$success)
return null;
237 return unserialize($s);
240 function put($key, $obj, $ttl = 0)
242 return apc_store($key, serialize($obj), $ttl);
247 return apc_delete($key);
252 trace(
"invalidateMatching: $pattern", 3);
266 apc_clear_cache(
'user');
271 echo
"<table class='list'><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>\n";
275 foreach (
new APCIterator(
'user',
'/.*/') as $kvp)
277 if (!
startsWith($kvp[
'key'], $prefix))
continue;
278 $key = str_replace($prefix,
"", $kvp[
'key']);
279 $value = htmlSafe($kvp[
'value']);
281 echo
"<tr><td><b>$key</b></td><td>{$value}</td></tr>\n";
283 echo
"</tbody></table>";
298 trace(
"Creating APCU Cache", 4);
305 $s = apcu_fetch($key, $success);
306 if (!$success)
return null;
308 return unserialize($s);
311 function put($key, $obj, $ttl = 0)
313 return apcu_store($key, serialize($obj), $ttl);
318 return apcu_delete($key);
323 trace(
"invalidateMatching: $pattern", 3);
337 apcu_clear_cache(
'user');
342 echo
"<table class='list'><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>\n";
346 foreach (
new APCUIterator(
'/.*/') as $kvp)
348 if (!
startsWith($kvp[
'key'], $prefix))
continue;
349 $key = str_replace($prefix,
"", $kvp[
'key']);
350 $value = htmlSafe($kvp[
'value']);
352 echo
"<tr><td><b>$key</b></td><td>{$value}</td></tr>\n";
354 echo
"</tbody></table>";
365 trace(
"Creating WinCache cache", 4);
370 $value = wincache_ucache_get($key, $success);
371 if (!$success)
return null;
373 return unserialize($value);
376 function put($key, $obj, $ttl = 0)
378 return wincache_ucache_set($key, serialize($obj), $ttl);
383 return wincache_ucache_delete($key);
388 $this->pattern = $pattern;
390 $info = wincache_ucache_info();
394 foreach($info[
'ucache_entries'] as $entry)
396 $keys[] = $entry[
'key_name'];
398 $doomedKeys = array_filter($keys, array($this,
filterKey));
400 foreach($doomedKeys as $key)
405 unset($this->pattern);
414 return preg_match(
"/".$this->pattern.
"/", $key);
419 wincache_ucache_clear();
424 $info = wincache_ucache_info();
426 echo
"<table class='list'><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>\n";
427 foreach ($info[
'ucache_entries'] as $kvp)
429 if (!
startsWith($kvp[
'key_name'], $prefix))
continue;
430 $key = str_replace($prefix,
"", $kvp[
'key_name']);
432 echo
"<tr><td><b>{$key}</b></td><td>".htmlSafe(wincache_ucache_get($kvp[
'key_name'])).
"</td></tr>\n";
434 echo
"</tbody></table>";
453 trace(
"Creating File-backed Cache", 4);
455 $this->cacheDir = $config[
"file_backed_cache_directory"];
456 $this->cache = array();
458 trace(
"Cache Directory: {$this->cacheDir}", 3);
463 return $this->cacheDir . DIRECTORY_SEPARATOR . $key .
".txt";
468 if (array_key_exists($key, $this->cache))
return $this->cache[$key];
470 if (file_exists($file))
472 $c = unserialize(file_get_contents($file));
473 $this->cache[$key] = $c;
479 function put($key, $obj, $ttl = 0)
481 $this->cache[$key] = $obj;
485 if (!file_exists($file))
487 $fp = fopen($file,
"w");
488 fwrite($fp, serialize($obj));
493 $fp = fopen($file,
"w+");
495 if (flock($fp, LOCK_EX))
498 fwrite($fp, serialize($obj));
503 echo
"Couldn't get the lock!";
516 if (file_exists($file)) unlink($file);
517 unset($this->cache[$key]);
523 throw new FakoliException(
"FileBackedCache currently does not support the invalidateMatching() method");
528 foreach(glob($this->cacheDir . DIRECTORY_SEPARATOR .
"*.txt") as $doomed)
Provides a simple cacheing mechanism using the Alternative PHP Cache extension.
invalidateMatching($pattern)
Provides a simple cacheing mechanism using the Alternative PHP Cache extension.
invalidateMatching($pattern)
The Cache class provides a simple caching interface.
static invalidate($key)
Invalidates the specifed entry in the cache.
static dump()
Output a dump of the cache for debugging purposes.
static clear()
Clear the cache, deleting all records.
static put($key, $obj, $ttl=0)
Store the specified object in the cache at the specified key.
static invalidateMatching($pattern)
Invalidate all entries in the cache that match a specific pattern.
Provides a simple File-backed caching implementation for when no shared memory caching extension is p...
invalidateMatching($pattern)
Implements a simple in-memory cache when other cacheing mechanisms are not available.
invalidateMatching($pattern)
filterKey($key)
Internal callback function that supports the invalidateMatching function.
Provides a simple cacheing mechanism using the WinCache PHP extension (for use under IIS).
invalidateMatching($pattern)
filterKey($key)
Internal callback function that supports the invalidateMatching function.
codify($name)
Takes a text string and converts it into a code-compliant format, suitable for use as a variable name...
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
arrayFilterKey( $input, $callback)
Filter an array based on applying the specified callback to the keys.
startsWith($text, $start)
Tests whether a string starts with a given sub-string.