* @package Mammut\Storage\CachedStore * @since 1.1.0.0 */ abstract class CachedStore extends \Mammut\StdObject { protected $cache; protected $ttl = 60; protected $cacheKeyPrefix = ''; public function __construct() { $this->cache = new Simple(); } public function setTTL($ttl) { $this->ttl = (int) $ttl; } public function getTTL() { return $this->ttl; } public function store($key, $value) { $this->storePersistend($key, $value); $this->cache->set($this->cacheKeyPrefix . $key, $value, $this->ttl); } public function fetch($key) { $val = NULL; if($this->cache->keyExists($key)) $val = $this->cache->get($key); if(is_null($val)) $val = $this->fetchPersistend($key); if(!is_null($val)) $this->cache->set($key, $val, $this->ttl); return $val; } public function exist($key) { if($this->cache->keyExists($key)) return true; return $this->existPersistend($key); } public function delete($key) { $this->deletePersistend($key); $this->cache->delete($key); } /** * Method which is called when a value should be stored to save the value in the * persistend storage. * * @param string $key * the value key * @param mixed $value * the new value */ protected abstract function storePersistend($key, $value); protected abstract function fetchPersistend($key); protected abstract function existPersistend($key); protected abstract function deletePersistend($key); }