AbstractLazyDataCached.php in Crumbs, the Breadcrumbs suite 7.2
File
lib/Container/AbstractLazyDataCached.php
View source
<?php
abstract class crumbs_Container_AbstractLazyDataCached {
private $data = array();
private $keysToCache = array();
function __construct() {
$this->keysToCache = array_fill_keys($this
->keysToCache(), TRUE);
}
protected abstract function keysToCache();
function flushCaches() {
$this->data = array();
cache_clear_all('crumbs:', 'cache', TRUE);
}
function __get($key) {
if (array_key_exists($key, $this->data)) {
return $this->data[$key];
}
return $this->data[$key] = empty($this->keysToCache[$key]) ? $this
->get($key) : $this
->getCached($key);
}
private function getCached($key) {
$cache = cache_get("crumbs:{$key}");
if (isset($cache->data)) {
return is_array($cache->data) ? $cache->data : unserialize($cache->data);
}
$data = $this
->get($key);
if (!is_array($data)) {
throw new Exception("Only arrays can be cached in crumbs_CachedLazyPluginInfo.");
}
cache_set("crumbs:{$key}", serialize($data));
return $data;
}
private function get($key) {
$method = 'get_' . $key;
if (!method_exists($this, $method)) {
$class = get_class($this);
throw new Exception("Key '{$key}' not supported in {$class}.");
}
$result = $this
->{$method}($this);
return isset($result) ? $result : FALSE;
}
}