* @package Mammut\Cache */ class WinCache extends \Mammut\StrictObject implements \Mammut\Cache\iCache { protected $prefix; protected $secret; /** * * @var \Mammut\Crypt\MCrypt */ protected $crypt; private $hits = 0; public function __construct($prefix, $secret = false) { if(!extension_loaded('wincache')) throw new ExtensionException('wincache'); $this->prefix = $prefix; // encryption settings (planned) $this->secret = $secret; if(!empty($this->secret)) { $this->crypt = new MCrypt(MCrypt::CIPHER_AES); $this->crypt->setKey($secret); } } public function set($key, $value, $ttl) { $key = $this->prefix . $key; if($value instanceof \Closure) $value = $value(); if(is_object($this->crypt)) $value = $this->crypt->encode(serialize($value)); wincache_ucache_set($key, $value, $ttl); } public function get($key, $default = NULL) { $key = $this->prefix . $key; if(!wincache_ucache_exists($key)) return ($default instanceof \Closure) ? $default() : $default; $value = wincache_ucache_get($key, $success); if(!$success) return ($default instanceof \Closure) ? $default() : $default; if(is_object($this->crypt)) $value = unserialize($this->crypt->decode($value)); $this->hits++; return $value; } public function delete($key) { $key = $this->prefix . $key; wincache_ucache_delete($key); } public function keyExists($key) { $key = $this->prefix . $key; return wincache_ucache_exists($key); } public function clear() { wincache_ucache_clear(); } public function getHits() { return $this->hits; } }