diff --git a/src/EpiCache_Memcached.php b/src/EpiCache_Memcached.php index 42e39c4..5b52b3b 100644 --- a/src/EpiCache_Memcached.php +++ b/src/EpiCache_Memcached.php @@ -1,72 +1,64 @@ host = !empty($params[0]) ? $params[0] : 'localhost'; - $this->port = !empty($params[1]) ? $params[1] : 11211;; - $this->compress = !empty($params[2]) ? $params[2] : 0;; - $this->expiry = !empty($params[3]) ? $params[3] : 3600; - } - public function delete($key, $timeout = 0) - { - if(!$this->connect() || empty($key)) - return false; +class EpiCache_Memcached extends EpiCache { - return $this->memcached->delete($key, $timeout); - } + private static $connected = false; + private $memcached = null; + private $host = null; + private $port = null; + private $compress = null; + private $expiry = null; - public function get($key, $useCache = true) - { - if(!$this->connect() || empty($key)) - { - return null; + public function __construct($params = array()) { + $this->host = !empty($params[0]) ? $params[0] : 'localhost'; + $this->port = !empty($params[1]) ? $params[1] : 11211; + $this->compress = isset($params[2]) ? $params[2] : 0; + $this->expiry = isset($params[3]) ? $params[3] : 3600; } - else if($useCache && $getEpiCache = $this->getEpiCache($key)) - { - return $getEpiCache; + + public function delete($key, $timeout = 0) { + if (!$this->connect() || empty($key)) + return false; + + return $this->memcached->delete($key, $timeout); + } + + public function get($key, $useCache = true) { + if (!$this->connect() || empty($key)) { + return null; + } else if ($useCache && $getEpiCache = $this->getEpiCache($key)) { + return $getEpiCache; + } else { + $value = $this->memcached->get($key); + $this->setEpiCache($key, $value); + return $value; + } } - else - { - $value = $this->memcached->get($key); - $this->setEpiCache($key, $value); - return $value; + + public function set($key = null, $value = null, $ttl = null) { + if (!$this->connect() || empty($key) || $value === null) + return false; + + $expiry = $ttl === null ? $this->expiry : $ttl; + $this->memcached->set($key, $value, $expiry); + $this->setEpiCache($key, $value); + return true; } - } - public function set($key = null, $value = null, $ttl = null) - { - if(!$this->connect() || empty($key) || $value === null) - return false; + private function connect() { + if (self::$connected === true) + return true; - $expiry = $ttl === null ? $this->expiry : $ttl; - $this->memcached->set($key, $value, $expiry); - $this->setEpiCache($key, $value); - return true; - } + if (class_exists('Memcached')) { + $this->memcached = new Memcached; - private function connect() - { - if(self::$connected === true) - return true; + if ($this->memcached->addServer($this->host, $this->port)) + return self::$connected = true; + else + EpiException::raise(new EpiCacheMemcacheConnectException('Could not connect to memcache server')); + } - if(class_exists('Memcached')) - { - $this->memcached = new Memcached; - - if($this->memcached->addServer($this->host, $this->port)) - return self::$connected = true; - else - EpiException::raise(new EpiCacheMemcacheConnectException('Could not connect to memcache server')); + EpiException::raise(new EpiCacheMemcacheClientDneException('No memcache client exists')); } - EpiException::raise(new EpiCacheMemcacheClientDneException('No memcache client exists')); - } } diff --git a/src/EpiSession_Memcached.php b/src/EpiSession_Memcached.php index 8ecdae1..dede2bb 100644 --- a/src/EpiSession_Memcached.php +++ b/src/EpiSession_Memcached.php @@ -1,83 +1,84 @@ host = !empty($params[0]) ? $params[0] : 'localhost'; + $this->port = !empty($params[1]) ? $params[1] : 11211; + $this->compress = isset($params[2]) ? $params[2] : 0; + $this->expiry = isset($params[3]) ? $params[3] : 3600; } - $this->host = !empty($params[0]) ? $params[0] : 'localhost'; - $this->port = !empty($params[1]) ? $params[1] : 11211;; - $this->compress = !empty($params[2]) ? $params[2] : 0;; - $this->expiry = !empty($params[3]) ? $params[3] : 3600; - } - - public function end() - { - if(!$this->connect()) - return; - - $this->memcached->delete($this->key); - $this->store = null; - setcookie(EpiSession::COOKIE, null, time()-86400); - } - - public function get($key = null) - { - if(!$this->connect() || empty($key) || !isset($this->store[$key])) - return false; - - return $this->store[$key]; - } - - public function getAll() - { - if(!$this->connect()) - return; - - return $this->memcached->get($this->key); - } - - public function set($key = null, $value = null) - { - if(!$this->connect() || empty($key)) - return false; - - $this->store[$key] = $value; - $this->memcached->set($this->key, $this->store); - return $value; - } - - private function connect($params = null) - { - if(self::$connected) - return true; - - if(class_exists('Memcached')) - { - $this->memcached = new Memcached; - if($this->memcached->addServer($this->host, $this->port)) - { - self::$connected = true; - $this->key = empty($key) ? $_COOKIE[EpiSession::COOKIE] : $key; - $this->store = $this->getAll(); - return true; - } - else - { - EpiException::raise(new EpiSessionMemcacheConnectException('Could not connect to memcache server')); - } + + public function end() { + if (!$this->connect()) + return; + + $this->memcached->delete($this->key); + $this->store = null; + setcookie(EpiSession::COOKIE, null, time() - 86400); + } + + public function get($key = null) { + if (!$this->connect() || empty($key) || !isset($this->store[$key])) + return false; + + return $this->store[$key]; + } + + public function getAll() { + if (!$this->connect()) + return; + + return $this->memcached->get($this->key); } - EpiException::raise(new EpiSessionMemcacheClientDneException('Could not connect to memcache server')); - } + + public function set($key = null, $value = null) { + if (!$this->connect() || empty($key)) + return false; + + $this->store[$key] = $value; + $this->memcached->set($this->key, $this->store, $this->expiry); + return $value; + } + + private function connect($params = null) { + if (self::$connected) + return true; + + if (class_exists('Memcached')) { + $this->memcached = new Memcached; + if ($this->memcached->addServer($this->host, $this->port)) { + self::$connected = true; + $this->key = $_COOKIE[EpiSession::COOKIE]; + $this->store = $this->getAll(); + return true; + } else { + EpiException::raise(new EpiSessionMemcacheConnectException('Could not connect to memcache server')); + } + } + EpiException::raise(new EpiSessionMemcacheClientDneException('Could not connect to memcache server')); + } + +} + +class EpiSessionMemcacheConnectException extends EpiException { + } -class EpiSessionMemcacheConnectException extends EpiException {} -class EpiSessionMemcacheClientDneException extends EpiException {} +class EpiSessionMemcacheClientDneException extends EpiException { + +}