Memcache, refactoring, View helpers, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@124 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
54
cache/MemcacheCache.php
vendored
54
cache/MemcacheCache.php
vendored
@ -17,10 +17,26 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
protected $connection = null;
|
||||
|
||||
protected $key_salt = null;
|
||||
|
||||
/**
|
||||
* One hour to live default
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $expire = 3600;
|
||||
|
||||
protected $keys = array();
|
||||
|
||||
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) {
|
||||
@ -42,7 +58,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function add($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->add($key, $value, null, $expire);
|
||||
return $this->connection->add($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,7 +70,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function decrement($key, $decrement = 1)
|
||||
{
|
||||
return $this->connection->decrement($key, $decrement);
|
||||
return $this->connection->decrement($this->getKey($key), $decrement);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,7 +82,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function del($key)
|
||||
{
|
||||
return $this->connection->delete($key);
|
||||
return $this->connection->delete($this->getKey($key));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,12 +98,12 @@ class MemcacheCache extends Cache
|
||||
/**
|
||||
* Retrieve item from the cache
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
return $this->connection->get($key);
|
||||
return $this->connection->get($this->getKey($key));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,7 +115,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function increment($key, $increment = 1)
|
||||
{
|
||||
return $this->connection->increment($key, $increment);
|
||||
return $this->connection->increment($this->getKey($key), $increment);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,7 +128,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function replace($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->replace($key, $value, null, $expire);
|
||||
return $this->connection->replace($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,6 +141,28 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function set($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->set($key, $value, null, $expire);
|
||||
return $this->connection->set($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function getKey($key)
|
||||
{
|
||||
if (!isset($this->keys[$key])) {
|
||||
$this->keys[$key] = md5($this->key_salt . $key);
|
||||
}
|
||||
return $this->keys[$key];
|
||||
}
|
||||
|
||||
public function getExpire($expire)
|
||||
{
|
||||
return ($expire > 0) ? $expire : $this->expire;
|
||||
}
|
||||
|
||||
public function cleanKeys()
|
||||
{
|
||||
$this->keys = array();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user