|
|
@ -11,31 +11,33 @@ |
|
|
|
|
|
|
|
class MemcacheCache extends Cache |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @var Memcache |
|
|
|
*/ |
|
|
|
protected $connection = null; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @var null|string |
|
|
|
*/ |
|
|
|
protected $key_salt = null; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* One hour to live default |
|
|
|
* |
|
|
|
* |
|
|
|
* @var int |
|
|
|
*/ |
|
|
|
protected $expire = 3600; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct($config) |
|
|
|
{ |
|
|
|
$this->connection = new Memcache(); |
|
|
|
|
|
|
|
|
|
|
|
if (isset($config['key_salt'])) { |
|
|
|
$this->key_salt = $config['key_salt']; |
|
|
|
unset($config['key_salt']); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$required = array('hostname', 'port'); |
|
|
|
foreach ($config as $c) { |
|
|
|
foreach ($required as $option) { |
|
|
@ -46,10 +48,10 @@ class MemcacheCache extends Cache |
|
|
|
$this->connection->addServer($c['hostname'], $c['port']); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Add an item to the cache |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $key |
|
|
|
* @param mixed $value |
|
|
|
* @param int $expire |
|
|
@ -59,10 +61,10 @@ class MemcacheCache extends Cache |
|
|
|
{ |
|
|
|
return $this->connection->add($this->getKey($key), $value, null, $this->getExpire($expire)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Decrement item's value |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $key |
|
|
|
* @param int $decrement |
|
|
|
* @return bool |
|
|
@ -71,32 +73,32 @@ class MemcacheCache extends Cache |
|
|
|
{ |
|
|
|
return $this->connection->decrement($this->getKey($key), $decrement); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Delete item from the cache |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $key |
|
|
|
* @param int $value |
|
|
|
* @internal param int $value |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
public function del($key) |
|
|
|
{ |
|
|
|
return $this->connection->delete($this->getKey($key), 0); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Flush all existing items |
|
|
|
* |
|
|
|
* |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
public function flush() |
|
|
|
{ |
|
|
|
return $this->connection->flush(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Retrieve item from the cache |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $key |
|
|
|
* @return mixed |
|
|
|
*/ |
|
|
@ -104,10 +106,10 @@ class MemcacheCache extends Cache |
|
|
|
{ |
|
|
|
return $this->connection->get($this->getKey($key)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Increment item's value |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $key |
|
|
|
* @param int $increment |
|
|
|
* @return bool |
|
|
@ -116,23 +118,24 @@ class MemcacheCache extends Cache |
|
|
|
{ |
|
|
|
return $this->connection->increment($this->getKey($key), $increment); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Replace value of the existing item |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $key |
|
|
|
* @param mixed $var |
|
|
|
* @param mixed $value |
|
|
|
* @param int $expire |
|
|
|
* @internal param mixed $var |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
public function replace($key, $value, $expire = 0) |
|
|
|
{ |
|
|
|
return $this->connection->replace($this->getKey($key), $value, null, $this->getExpire($expire)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Store data in the cache |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $key |
|
|
|
* @param mixed $value |
|
|
|
* @param int $expire |
|
|
@ -142,7 +145,7 @@ class MemcacheCache extends Cache |
|
|
|
{ |
|
|
|
return $this->connection->set($this->getKey($key), $value, null, $this->getExpire($expire)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param string $key |
|
|
|
* @return string |
|
|
@ -151,7 +154,7 @@ class MemcacheCache extends Cache |
|
|
|
{ |
|
|
|
return md5($this->key_salt . $key); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getExpire($expire) |
|
|
|
{ |
|
|
|
return ($expire > 0) ? $expire : $this->expire; |
|
|
|