Redis implementation, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@149 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
36
cache/CacheKey.php
vendored
36
cache/CacheKey.php
vendored
@ -12,31 +12,33 @@
|
||||
class CacheKey
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Cacher
|
||||
*/
|
||||
protected $cacher;
|
||||
|
||||
/**
|
||||
* @var CacheKey
|
||||
*/
|
||||
protected $key;
|
||||
protected $params = '';
|
||||
protected $expire = 0;
|
||||
|
||||
/**
|
||||
* @param Cacher $cacher
|
||||
* @param string $key
|
||||
* @param mixed $params
|
||||
* @param iCacheable $cacheable
|
||||
* @param int $expire
|
||||
* @return CacheKey
|
||||
*/
|
||||
public function __construct($key, $params = array(), $cacheable)
|
||||
public function __construct($cacher, $key, $params = array(), $expire = 0)
|
||||
{
|
||||
$this->cacher = $cacher;
|
||||
$this->key = $key;
|
||||
if (!$cacheable instanceof iCacheable) {
|
||||
throw new GeneralException('CacheKey depends on iCacheable instance');
|
||||
if ($params) {
|
||||
$params = (is_array($params)) ? implode('|', $params) : $params;
|
||||
$this->key = $key . '_' . $params;
|
||||
}
|
||||
$this->cache = $cacheable->getCache();
|
||||
$this->expire = $cacheable->getKeyExpire($this->key);
|
||||
$this->params = (is_array($params)) ? implode('|', $params) : $params;
|
||||
}
|
||||
|
||||
protected function getCacheKey()
|
||||
{
|
||||
$params = ($this->params) ? ('_' . $this->params) : '';
|
||||
return $this->key . $params;
|
||||
$this->expire = $expire;
|
||||
}
|
||||
|
||||
protected function getExpire()
|
||||
@ -54,7 +56,7 @@ class CacheKey
|
||||
|
||||
public function get()
|
||||
{
|
||||
return $this->cache->get($this->getCacheKey());
|
||||
return $this->cacher->get($this->key);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,11 +64,11 @@ class CacheKey
|
||||
*/
|
||||
public function set($value)
|
||||
{
|
||||
return $this->cache->set($this->getCacheKey(), $value, $this->expire);
|
||||
return $this->cacher->set($this->key, $value, $this->expire);
|
||||
}
|
||||
|
||||
public function del()
|
||||
{
|
||||
return $this->cache->del($this->getCacheKey());
|
||||
return $this->cacher->del($this->key);
|
||||
}
|
||||
}
|
48
cache/CacheKeySet.php
vendored
48
cache/CacheKeySet.php
vendored
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-10
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class CacheKeySet extends CacheKey
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
$set = $this->cache->get($this->key);
|
||||
$item_key = $this->getCacheKey();
|
||||
|
||||
if (!is_array($set) || !array_key_exists($item_key, $set)) {
|
||||
return false;
|
||||
}
|
||||
return $this->cache->get($item_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set($value)
|
||||
{
|
||||
$set = $this->cache->get($this->key);
|
||||
if (!is_array($set)) {
|
||||
$set = array();
|
||||
}
|
||||
|
||||
$item_key = $this->getCacheKey();
|
||||
if (!$this->cache->set($item_key, $value, $this->expire)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$set[$item_key] = $this->cache->getExpire($this->expire);
|
||||
return $this->cache->set($this->key, $set, $this->expire);
|
||||
}
|
||||
|
||||
public function del()
|
||||
{
|
||||
return $this->cache->del($this->key);
|
||||
}
|
||||
}
|
11
cache/MemcacheCache.php
vendored
11
cache/MemcacheCache.php
vendored
@ -26,7 +26,6 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
protected $expire = 3600;
|
||||
|
||||
protected $keys = array();
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
@ -150,19 +149,11 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
protected function getKey($key)
|
||||
{
|
||||
if (!isset($this->keys[$key])) {
|
||||
$this->keys[$key] = md5($this->key_salt . $key);
|
||||
}
|
||||
return $this->keys[$key];
|
||||
return md5($this->key_salt . $key);
|
||||
}
|
||||
|
||||
public function getExpire($expire)
|
||||
{
|
||||
return ($expire > 0) ? $expire : $this->expire;
|
||||
}
|
||||
|
||||
public function cleanKeys()
|
||||
{
|
||||
$this->keys = array();
|
||||
}
|
||||
}
|
24
cache/iCacheable.php
vendored
24
cache/iCacheable.php
vendored
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage cache
|
||||
* @since 2010-03-12
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
interface iCacheable
|
||||
{
|
||||
/**
|
||||
* @return Cache
|
||||
*/
|
||||
public function getCache();
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return Expiration time in seconds
|
||||
*/
|
||||
public function getKeyExpire($key);
|
||||
}
|
Reference in New Issue
Block a user