Clean up Memcached and Redis cache handlers

This commit is contained in:
Kijin Sung 2016-02-11 13:56:17 +09:00
parent f3d3122787
commit 7955aaddd0
3 changed files with 79 additions and 104 deletions

View file

@ -59,11 +59,11 @@ class CacheHandler extends Handler
// Allow using custom cache info for backward compatibility.
if (is_object($info) && $info->use_object_cache)
{
$cache_config = $info->use_object_cache;
$cache_config = $cache_config_array = $info->use_object_cache;
}
else
{
$cache_config = config('cache');
$cache_config = $cache_config_array = config('cache');
if (is_array($cache_config) && count($cache_config))
{
$cache_config = array_first($cache_config);
@ -87,7 +87,7 @@ class CacheHandler extends Handler
// Create an instance of cache backend.
$class = 'Cache' . ucfirst($type);
include_once sprintf('%sclasses/cache/%s.class.php', _XE_PATH_, $class);
$this->handler = $class::getInstance($cache_config);
$this->handler = $class::getInstance($cache_config_array);
// Initialize key group versions.
$this->keyGroupVersions = $this->handler->get('key_group_versions', 0);

View file

@ -9,11 +9,12 @@
class CacheMemcache extends CacheBase
{
/**
* instance of Memcahe
* @var Memcahe
* Instance of Memcache
*/
var $Memcache;
var $SelectedExtension;
protected static $_instance;
protected $_conn;
protected $_status;
protected $_useExtension;
/**
* Get instance of CacheMemcache
@ -21,14 +22,13 @@ class CacheMemcache extends CacheBase
* @param string $url url of memcache
* @return CacheMemcache instance of CacheMemcache
*/
function getInstance($url, $force_new_instance = false)
public static function getInstance($url, $force_new_instance = false)
{
if(!$GLOBALS['__CacheMemcache__'] || $force_new_instance)
if(!self::$_instance || $force_new_instance)
{
$GLOBALS['__CacheMemcache__'] = new CacheMemcache($url);
unset($GLOBALS['XE_MEMCACHE_SUPPORT']);
self::$_instance = new self($url);
}
return $GLOBALS['__CacheMemcache__'];
return self::$_instance;
}
/**
@ -38,19 +38,19 @@ class CacheMemcache extends CacheBase
* @param string $url url of memcache
* @return void
*/
function __construct($url)
protected function __construct($url)
{
//$config['url'] = array('memcache://localhost:11211');
$config['url'] = is_array($url) ? $url : array($url);
if(class_exists('Memcached'))
{
$this->Memcache = new Memcached;
$this->SelectedExtension = 'Memcached';
$this->_conn = new Memcached;
$this->_useExtension = 'Memcached';
}
elseif(class_exists('Memcache'))
{
$this->Memcache = new Memcache;
$this->SelectedExtension = 'Memcache';
$this->_conn = new Memcache;
$this->_useExtension = 'Memcache';
}
else
{
@ -60,7 +60,7 @@ class CacheMemcache extends CacheBase
foreach($config['url'] as $url)
{
$info = parse_url($url);
$this->Memcache->addServer($info['host'], $info['port']);
$this->_conn->addServer($info['host'], $info['port']);
}
}
@ -69,24 +69,24 @@ class CacheMemcache extends CacheBase
*
* @return bool Return true on support or false on not support
*/
function isSupport()
public function isSupport()
{
if(isset($GLOBALS['XE_MEMCACHE_SUPPORT']))
if(isset($this->_status))
{
return $GLOBALS['XE_MEMCACHE_SUPPORT'];
return $this->_status;
}
if($this->SelectedExtension === 'Memcached')
if($this->_useExtension === 'Memcached')
{
return $GLOBALS['XE_MEMCACHE_SUPPORT'] = $this->Memcache->set('xe', 'xe', 1);
return $this->_status = $this->_conn->set('xe', 'xe', 1);
}
elseif($this->SelectedExtension === 'Memcache')
elseif($this->_useExtension === 'Memcache')
{
return $GLOBALS['XE_MEMCACHE_SUPPORT'] = $this->Memcache->set('xe', 'xe', MEMCACHE_COMPRESSED, 1);
return $this->_status = $this->_conn->set('xe', 'xe', MEMCACHE_COMPRESSED, 1);
}
else
{
return $GLOBALS['XE_MEMCACHE_SUPPORT'] = false;
return $this->_status = false;
}
}
@ -96,7 +96,7 @@ class CacheMemcache extends CacheBase
* @param string $key Cache key
* @return string Return unique key
*/
function getKey($key)
protected function getKey($key)
{
return md5(_XE_PATH_ . $key);
}
@ -118,20 +118,20 @@ class CacheMemcache extends CacheBase
* If it's equal to zero, use the default valid time CacheMemcache::valid_time.
* @return bool Returns true on success or false on failure.
*/
function put($key, $buff, $valid_time = 0)
public function put($key, $buff, $valid_time = 0)
{
if($valid_time == 0)
{
$valid_time = $this->valid_time;
}
if($this->SelectedExtension === 'Memcached')
if($this->_useExtension === 'Memcached')
{
return $this->Memcache->set($this->getKey($key), array($_SERVER['REQUEST_TIME'], $buff), $valid_time);
return $this->_conn->set($this->getKey($key), array(time(), $buff), $valid_time);
}
else
{
return $this->Memcache->set($this->getKey($key), array($_SERVER['REQUEST_TIME'], $buff), MEMCACHE_COMPRESSED, $valid_time);
return $this->_conn->set($this->getKey($key), array(time(), $buff), MEMCACHE_COMPRESSED, $valid_time);
}
}
@ -143,20 +143,17 @@ class CacheMemcache extends CacheBase
* If stored time is older then modified time, the data is invalid.
* @return bool Return true on valid or false on invalid.
*/
function isValid($key, $modified_time = 0)
public function isValid($key, $modified_time = 0)
{
$_key = $this->getKey($key);
$obj = $this->Memcache->get($_key);
$obj = $this->_conn->get($this->getKey($key));
if(!$obj || !is_array($obj))
{
return false;
}
unset($obj[1]);
if($modified_time > 0 && $modified_time > $obj[0])
{
$this->_delete($_key);
$this->delete($key);
return false;
}
@ -173,10 +170,9 @@ class CacheMemcache extends CacheBase
* If stored time is older then modified time, return false.
* @return false|mixed Return false on failure or older then modified time. Return the string associated with the $key on success.
*/
function get($key, $modified_time = 0)
public function get($key, $modified_time = 0)
{
$_key = $this->getKey($key);
$obj = $this->Memcache->get($_key);
$obj = $this->_conn->get($this->getKey($key));
if(!$obj || !is_array($obj))
{
return false;
@ -184,12 +180,10 @@ class CacheMemcache extends CacheBase
if($modified_time > 0 && $modified_time > $obj[0])
{
$this->_delete($_key);
$this->delete($key);
return false;
}
unset($obj[0]);
return $obj[1];
}
@ -201,22 +195,9 @@ class CacheMemcache extends CacheBase
* @param string $key The key associated with the item to delete.
* @return void
*/
function delete($key)
public function delete($key)
{
$_key = $this->getKey($key);
$this->_delete($_key);
}
/**
* Delete item from the server(private)
*
* @see CacheMemcache::delete()
* @param string $_key The key associated with the item to delete.
* @return void
*/
function _delete($_key)
{
$this->Memcache->delete($_key);
return $this->_conn->delete($this->getKey($key));
}
/**
@ -228,9 +209,9 @@ class CacheMemcache extends CacheBase
*
* @return bool Returns true on success or false on failure.
*/
function truncate()
public function truncate()
{
return $this->Memcache->flush();
return $this->_conn->flush();
}
}

View file

@ -9,11 +9,11 @@
class CacheRedis extends CacheBase
{
/**
* instance of Redis
* @var redis
* Instance of Memcache
*/
var $redis;
var $status;
protected static $_instance;
protected $_conn;
protected $_status;
/**
* Get instance of CacheRedis
@ -21,13 +21,13 @@ class CacheRedis extends CacheBase
* @param string $url url of Redis
* @return CacheRedis instance of CacheRedis
*/
function getInstance($url, $force_new_instance = false)
public static function getInstance($url, $force_new_instance = false)
{
if(!$GLOBALS['__CacheRedis__'] || $force_new_instance)
if(!self::$_instance || $force_new_instance)
{
$GLOBALS['__CacheRedis__'] = new CacheRedis($url);
self::$_instance = new self($url);
}
return $GLOBALS['__CacheRedis__'];
return self::$_instance;
}
/**
@ -37,34 +37,33 @@ class CacheRedis extends CacheBase
* @param string $url url of Redis
* @return void
*/
function __construct($url)
protected function __construct($url)
{
//$config['url'] = 'redis://localhost:6379/1';
$config['url'] = is_array($url) ? reset($url) : $url;
//$url = 'redis://localhost:6379/1';
$url = is_array($url) ? reset($url) : $url;
if(!class_exists('Redis'))
{
return $this->status = false;
$this->_status = false;
}
try
{
$this->redis = new Redis;
$this->_conn = new Redis;
$info = parse_url($url);
$this->redis->connect($info['host'], $info['port'], 0.15);
$this->_conn->connect($info['host'], $info['port'], 0.15);
if(isset($info['user']) || isset($info['pass']))
{
$this->redis->auth(isset($info['user']) ? $info['user'] : $info['pass']);
$this->_conn->auth(isset($info['user']) ? $info['user'] : $info['pass']);
}
if(isset($info['path']) && $dbnum = intval(substr($info['path'], 1)))
{
$this->redis->select($dbnum);
$this->_conn->select($dbnum);
}
return $this->status = true;
}
catch(RedisException $e)
{
return $this->status = false;
$this->_status = false;
}
}
@ -73,20 +72,20 @@ class CacheRedis extends CacheBase
*
* @return bool Return true on support or false on not support
*/
function isSupport()
public function isSupport()
{
if($this->status !== null)
if($this->_status !== null)
{
return $this->status;
return $this->_status;
}
try
{
return $this->redis->setex('xe', 1, 'xe');
return $this->_conn->setex('xe', 1, 'xe');
}
catch(RedisException $e)
{
return $this->status = false;
return $this->_status = false;
}
}
@ -96,7 +95,7 @@ class CacheRedis extends CacheBase
* @param string $key Cache key
* @return string Return unique key
*/
function getKey($key)
protected function getKey($key)
{
static $prefix = null;
if($prefix === null)
@ -123,7 +122,7 @@ class CacheRedis extends CacheBase
* If it's equal to zero, use the default valid time CacheRedis::valid_time.
* @return bool Returns true on success or false on failure.
*/
function put($key, $buff, $valid_time = 0)
public function put($key, $buff, $valid_time = 0)
{
if($valid_time > 60 * 60 * 24 * 30)
{
@ -136,11 +135,11 @@ class CacheRedis extends CacheBase
try
{
return $this->redis->setex($this->getKey($key), $valid_time, serialize(array($_SERVER['REQUEST_TIME'], $buff)));
return $this->_conn->setex($this->getKey($key), $valid_time, serialize(array($_SERVER['REQUEST_TIME'], $buff)));
}
catch(RedisException $e)
{
return $this->status = false;
return $this->_status = false;
}
}
@ -152,20 +151,18 @@ class CacheRedis extends CacheBase
* If stored time is older then modified time, the data is invalid.
* @return bool Return true on valid or false on invalid.
*/
function isValid($key, $modified_time = 0)
public function isValid($key, $modified_time = 0)
{
$_key = $this->getKey($key);
$obj = $this->redis->get($_key);
$obj = $this->_conn->get($this->getKey($key));
$obj = $obj ? unserialize($obj) : false;
if(!$obj || !is_array($obj))
{
return false;
}
unset($obj[1]);
if($modified_time > 0 && $modified_time > $obj[0])
{
$this->redis->del($_key);
$this->_conn->del($this->getKey($key));
return false;
}
@ -182,10 +179,9 @@ class CacheRedis extends CacheBase
* If stored time is older then modified time, return false.
* @return false|mixed Return false on failure or older then modified time. Return the string associated with the $key on success.
*/
function get($key, $modified_time = 0)
public function get($key, $modified_time = 0)
{
$_key = $this->getKey($key);
$obj = $this->redis->get($_key);
$obj = $this->_conn->get($this->getKey($key));
$obj = $obj ? unserialize($obj) : false;
if(!$obj || !is_array($obj))
{
@ -194,7 +190,7 @@ class CacheRedis extends CacheBase
if($modified_time > 0 && $modified_time > $obj[0])
{
$this->redis->del($_key);
$this->_conn->del($this->getKey($key));
return false;
}
@ -209,17 +205,15 @@ class CacheRedis extends CacheBase
* @param string $key The key associated with the item to delete.
* @return void
*/
function delete($key)
public function delete($key)
{
$_key = $this->getKey($key);
try
{
$this->redis->del($_key);
return $this->_conn->del($this->getKey($key));
}
catch(RedisException $e)
{
return $this->status = false;
return $this->_status = false;
}
}
@ -231,15 +225,15 @@ class CacheRedis extends CacheBase
*
* @return bool Returns true on success or false on failure.
*/
function truncate()
public function truncate()
{
try
{
return $this->redis->flushDB();
return $this->_conn->flushDB();
}
catch(RedisException $e)
{
return $this->status = false;
return $this->_status = false;
}
}