* @package Mammut\Cache */ class APC extends \Mammut\StrictObject implements \Mammut\Cache\iCache { protected $prefix; protected $secret; /** * * @var MCrypt */ protected $crypt; private $hits = 0; public function __construct($prefix, $secret = false) { if(!extension_loaded('apc')) throw new ExtensionException('apc'); $this->prefix = $prefix; // encryption settings (planned) $this->secret = $secret; if(!empty($this->secret)) { $this->crypt = new MCrypt(MCrypt::CIPHER_AES); $this->crypt->setKey($secret); } } protected function genKey($key) { if (!is_scalar($key)) throw new \InvalidArgumentException('$key is not scalar'); $key = $this->prefix . $key; return $key; } public function set($key, $value, $ttl) { $key = $this->genKey($key); if ($value instanceof \Closure) $value = $value(); if(is_object($this->crypt)) $value = $this->crypt->encode(serialize($value)); apc_store($key, $value, $ttl); } public function get($key, $default = NULL) { $key = $this->genKey($key); $value = apc_fetch($key, $success); if(!$success) return $default; if(is_object($this->crypt)) $value = unserialize($this->crypt->decode($value)); $this->hits++; return $value; } public function delete($key) { $key = $this->genKey($key); apc_delete($key); } public function keyExists($key) { $key = $this->genKey($key); if(function_exists('apc_exists')) // apc_exists is only avaible in apc >= 3.1.4 return apc_exists($key); apc_fetch($key, $success); return $success; } public function clear() { apc_clear_cache('user'); } public function getHits() { return $this->hits; } }