Clean up CacheHandler

This commit is contained in:
Kijin Sung 2016-02-11 11:08:25 +09:00
parent 4dbb0239c6
commit b8b93159e2

View file

@ -8,17 +8,22 @@
*/ */
class CacheHandler extends Handler class CacheHandler extends Handler
{ {
/**
* Instances are stored here.
*/
protected static $_instances = array();
/** /**
* instance of cache handler * instance of cache handler
* @var CacheBase * @var CacheBase
*/ */
var $handler = null; protected $handler = null;
/** /**
* Version of key group * Version of key group
* @var int * @var int
*/ */
var $keyGroupVersions = null; protected $keyGroupVersions = null;
/** /**
* Get a instance of CacheHandler(for singleton) * Get a instance of CacheHandler(for singleton)
@ -28,14 +33,14 @@ class CacheHandler extends Handler
* @param boolean $always_use_file If set true, use a file cache always * @param boolean $always_use_file If set true, use a file cache always
* @return CacheHandler * @return CacheHandler
*/ */
function getInstance($target = 'object', $info = null, $always_use_file = false) public static function getInstance($target = 'object', $info = null, $always_use_file = false)
{ {
$cache_handler_key = $target . ($always_use_file ? '_file' : ''); $key = 'object' . ($always_use_file ? '_file' : '');
if(!$GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key]) if (!isset(self::$_instances[$key]))
{ {
$GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key] = new CacheHandler($target, $info, $always_use_file); self::$_instances[$key] = new self($target, $info, $always_use_file);
} }
return $GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key]; return self::$_instances[$key];
} }
/** /**
@ -44,84 +49,53 @@ class CacheHandler extends Handler
* Do not use this directly. You can use getInstance() instead. * Do not use this directly. You can use getInstance() instead.
* *
* @see CacheHandler::getInstance * @see CacheHandler::getInstance
* @param string $target type of cache (object|template) * @param string $target type of cache (object)
* @param object $info info. of DB * @param object $info info. of DB
* @param boolean $always_use_file If set true, use a file cache always * @param boolean $always_use_file If set true, use a file cache always
* @return CacheHandler * @return CacheHandler
*/ */
function __construct($target, $info = null, $always_use_file = false) protected function __construct($target, $info = null, $always_use_file = false)
{ {
if(!$info) // Allow using custom cache info for backward compatibility.
if (is_object($info) && $info->use_object_cache)
{ {
$info = Context::getDBInfo(); $cache_config = $info->use_object_cache;
} }
else
if($info)
{ {
if($target == 'object') $cache_config = config('cache');
if (is_array($cache_config) && count($cache_config))
{ {
if($info->use_object_cache == 'apc') $cache_config = array_first($cache_config);
{
$type = 'apc';
}
else if(substr($info->use_object_cache, 0, 8) == 'memcache')
{
$type = 'memcache';
$url = $info->use_object_cache;
}
else if(substr($info->use_object_cache, 0, 5) == 'redis')
{
$type = 'redis';
$url = $info->use_object_cache;
}
else if($info->use_object_cache == 'wincache')
{
$type = 'wincache';
}
else if($info->use_object_cache == 'file')
{
$type = 'file';
}
else if($always_use_file)
{
$type = 'file';
}
}
else if($target == 'template')
{
if($info->use_template_cache == 'apc')
{
$type = 'apc';
}
else if(substr($info->use_template_cache, 0, 8) == 'memcache')
{
$type = 'memcache';
$url = $info->use_template_cache;
}
else if(substr($info->use_template_cache, 0, 5) == 'redis')
{
$type = 'redis';
$url = $info->use_template_cache;
}
else if($info->use_template_cache == 'wincache')
{
$type = 'wincache';
}
}
if($type)
{
$class = 'Cache' . ucfirst($type);
include_once sprintf('%sclasses/cache/%s.class.php', _XE_PATH_, $class);
$this->handler = call_user_func(array($class, 'getInstance'), $url);
$this->keyGroupVersions = $this->handler->get('key_group_versions', 0);
if(!$this->keyGroupVersions)
{
$this->keyGroupVersions = array();
$this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
}
} }
} }
// Handle various types of cache backend.
if (preg_match('/^(apc|memcache|redis|wincache|file)/', strval($cache_config), $matches))
{
$type = $matches[1];
}
elseif ($always_use_file)
{
$type = 'file';
}
else
{
return;
}
// 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);
// Initialize key group versions.
$this->keyGroupVersions = $this->handler->get('key_group_versions', 0);
if(!$this->keyGroupVersions)
{
$this->keyGroupVersions = array();
$this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
}
} }
/** /**
@ -129,14 +103,9 @@ class CacheHandler extends Handler
* *
* @return boolean * @return boolean
*/ */
function isSupport() public function isSupport()
{ {
if($this->handler && $this->handler->isSupport()) return ($this->handler && $this->handler->isSupport());
{
return true;
}
return false;
} }
/** /**
@ -145,11 +114,9 @@ class CacheHandler extends Handler
* @param string $key The key that will be associated with the item. * @param string $key The key that will be associated with the item.
* @return string Returns cache name * @return string Returns cache name
*/ */
function getCacheKey($key) public function getCacheKey($key)
{ {
$key = str_replace('/', ':', $key); return RX_VERSION . ':' . str_replace('/', ':', $key);
return __XE_VERSION__ . ':' . $key;
} }
/** /**
@ -160,16 +127,9 @@ class CacheHandler extends Handler
* If stored time is older then modified time, return false. * 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. * @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)
{ {
if(!$this->handler) return $this->handler ? $this->handler->get($this->getCacheKey($key), $modified_time) : false;
{
return false;
}
$key = $this->getCacheKey($key);
return $this->handler->get($key, $modified_time);
} }
/** /**
@ -182,16 +142,9 @@ class CacheHandler extends Handler
* If no ttl is supplied, use the default valid time. * If no ttl is supplied, use the default valid time.
* @return bool|void Returns true on success or false on failure. If use CacheFile, returns void. * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
*/ */
function put($key, $obj, $valid_time = 0) public function put($key, $obj, $valid_time = 0)
{ {
if(!$this->handler && !$key) return $this->handler ? $this->handler->put($this->getCacheKey($key), $obj, $valid_time) : false;
{
return false;
}
$key = $this->getCacheKey($key);
return $this->handler->put($key, $obj, $valid_time);
} }
/** /**
@ -200,16 +153,9 @@ class CacheHandler extends Handler
* @param string $key Cache key * @param string $key Cache key
* @return void * @return void
*/ */
function delete($key) public function delete($key)
{ {
if(!$this->handler) return $this->handler ? $this->handler->delete($this->getCacheKey($key)) : false;
{
return false;
}
$key = $this->getCacheKey($key);
return $this->handler->delete($key);
} }
/** /**
@ -220,16 +166,9 @@ class CacheHandler extends Handler
* If stored time is older then modified time, the data is invalid. * If stored time is older then modified time, the data is invalid.
* @return bool Return true on valid or false on invalid. * @return bool Return true on valid or false on invalid.
*/ */
function isValid($key, $modified_time) public function isValid($key, $modified_time = 0)
{ {
if(!$this->handler) return $this->handler ? $this->handler->isValid($this->getCacheKey($key), $modified_time) : false;
{
return false;
}
$key = $this->getCacheKey($key);
return $this->handler->isValid($key, $modified_time);
} }
/** /**
@ -237,14 +176,9 @@ class CacheHandler extends Handler
* *
* @return bool|void Returns true on success or false on failure. If use CacheFile, returns void. * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
*/ */
function truncate() public function truncate()
{ {
if(!$this->handler) return $this->handler ? $this->handler->truncate() : false;
{
return false;
}
return $this->handler->truncate();
} }
/** /**
@ -263,7 +197,7 @@ class CacheHandler extends Handler
* @param string $key Cache key * @param string $key Cache key
* @return string * @return string
*/ */
function getGroupKey($keyGroupName, $key) public function getGroupKey($keyGroupName, $key)
{ {
if(!$this->keyGroupVersions[$keyGroupName]) if(!$this->keyGroupVersions[$keyGroupName])
{ {
@ -280,12 +214,11 @@ class CacheHandler extends Handler
* @param string $keyGroupName Group name * @param string $keyGroupName Group name
* @return void * @return void
*/ */
function invalidateGroupKey($keyGroupName) public function invalidateGroupKey($keyGroupName)
{ {
$this->keyGroupVersions[$keyGroupName]++; $this->keyGroupVersions[$keyGroupName]++;
$this->handler->put('key_group_versions', $this->keyGroupVersions, 0); $this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
} }
} }
/** /**
@ -299,7 +232,7 @@ class CacheBase
* Default valid time * Default valid time
* @var int * @var int
*/ */
var $valid_time = 36000; public $valid_time = 36000;
/** /**
* Get cached data * Get cached data
@ -309,7 +242,7 @@ class CacheBase
* If stored time is older then modified time, return false. * 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. * @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)
{ {
return false; return false;
} }
@ -324,10 +257,18 @@ class CacheBase
* If no ttl is supplied, use the default valid time. * If no ttl is supplied, use the default valid time.
* @return bool|void Returns true on success or false on failure. If use CacheFile, returns void. * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
*/ */
function put($key, $obj, $valid_time = 0) public function put($key, $obj, $valid_time = 0)
{ {
return false; return false;
} }
/**
* Alias of put()
*/
public function set($key, $obj, $valid_time = 0)
{
return $this->put($key, $obj, $valid_time = 0);
}
/** /**
* Return whether cache is valid or invalid * Return whether cache is valid or invalid
@ -337,7 +278,7 @@ class CacheBase
* If stored time is older then modified time, the data is invalid. * If stored time is older then modified time, the data is invalid.
* @return bool Return true on valid or false on invalid. * @return bool Return true on valid or false on invalid.
*/ */
function isValid($key, $modified_time = 0) public function isValid($key, $modified_time = 0)
{ {
return false; return false;
} }
@ -347,7 +288,7 @@ class CacheBase
* *
* @return boolean * @return boolean
*/ */
function isSupport() public function isSupport()
{ {
return false; return false;
} }
@ -357,7 +298,7 @@ class CacheBase
* *
* @return bool|void Returns true on success or false on failure. If use CacheFile, returns void. * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
*/ */
function truncate() public function truncate()
{ {
return false; return false;
} }