Update CacheHandler to use Rhymix Framework instead

This commit is contained in:
Kijin Sung 2016-04-17 14:43:16 +09:00
parent 568151b5ee
commit 7d80bbe27d
6 changed files with 17 additions and 1116 deletions

View file

@ -9,21 +9,9 @@
class CacheHandler extends Handler
{
/**
* Instances are stored here.
* The instance is stored here.
*/
protected static $_instances = array();
/**
* instance of cache handler
* @var CacheBase
*/
protected $handler = null;
/**
* Version of key group
* @var int
*/
protected $keyGroupVersions = null;
protected static $_instance = null;
/**
* Get a instance of CacheHandler(for singleton)
@ -35,12 +23,11 @@ class CacheHandler extends Handler
*/
public static function getInstance($target = 'object', $info = null, $always_use_file = false)
{
$key = 'object' . ($always_use_file ? '_file' : '');
if (!isset(self::$_instances[$key]))
if (!self::$_instance)
{
self::$_instances[$key] = new self($target, $info, $always_use_file);
self::$_instance = new self;
}
return self::$_instances[$key];
return self::$_instance;
}
/**
@ -54,48 +41,9 @@ class CacheHandler extends Handler
* @param boolean $always_use_file If set true, use a file cache always
* @return CacheHandler
*/
protected function __construct($target, $info = null, $always_use_file = false)
protected function __construct()
{
// Allow using custom cache info for backward compatibility.
if (is_object($info) && $info->use_object_cache)
{
$cache_config = $cache_config_array = $info->use_object_cache;
}
else
{
$cache_config = $cache_config_array = config('cache');
if (is_array($cache_config) && count($cache_config))
{
$cache_config = array_first($cache_config);
}
}
// 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_array);
// 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);
}
}
/**
@ -103,19 +51,9 @@ class CacheHandler extends Handler
*
* @return boolean
*/
public function isSupport($type = null, $cache_config = null)
public function isSupport()
{
if ($type === null)
{
return ($this->handler && $this->handler->isSupport());
}
else
{
$class = 'Cache' . ucfirst(str_replace('memcached', 'memcache', $type));
include_once sprintf('%sclasses/cache/%s.class.php', _XE_PATH_, $class);
$handler = $class::getInstance($cache_config, true);
return $handler->isSupport();
}
return true;
}
/**
@ -126,7 +64,7 @@ class CacheHandler extends Handler
*/
public function getCacheKey($key)
{
return RX_VERSION . ':' . str_replace('/', ':', $key);
return Rhymix\Framework\Cache::getRealKey($key);
}
/**
@ -139,8 +77,8 @@ class CacheHandler extends Handler
*/
public function get($key, $modified_time = 0)
{
if (!$key) return false;
return $this->handler ? $this->handler->get($this->getCacheKey($key), $modified_time) : false;
$value = Rhymix\Framework\Cache::get($key);
return $value === null ? false : $value;
}
/**
@ -155,8 +93,7 @@ class CacheHandler extends Handler
*/
public function put($key, $obj, $valid_time = 0)
{
if (!$key) return false;
return $this->handler ? $this->handler->put($this->getCacheKey($key), $obj, $valid_time) : false;
return Rhymix\Framework\Cache::set($key, $obj, $valid_time);
}
/**
@ -167,8 +104,7 @@ class CacheHandler extends Handler
*/
public function delete($key)
{
if (!$key) return false;
return $this->handler ? $this->handler->delete($this->getCacheKey($key)) : false;
return Rhymix\Framework\Cache::delete($key);
}
/**
@ -181,8 +117,7 @@ class CacheHandler extends Handler
*/
public function isValid($key, $modified_time = 0)
{
if (!$key) return false;
return $this->handler ? $this->handler->isValid($this->getCacheKey($key), $modified_time) : false;
return Rhymix\Framework\Cache::exists($key);
}
/**
@ -192,7 +127,7 @@ class CacheHandler extends Handler
*/
public function truncate()
{
return $this->handler ? $this->handler->truncate() : false;
return Rhymix\Framework\Cache::clearAll();
}
/**
@ -213,13 +148,7 @@ class CacheHandler extends Handler
*/
public function getGroupKey($keyGroupName, $key)
{
if(!$this->keyGroupVersions[$keyGroupName])
{
$this->keyGroupVersions[$keyGroupName] = 1;
$this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
}
return 'cache_group_' . $this->keyGroupVersions[$keyGroupName] . ':' . $keyGroupName . ':' . $key;
return Rhymix\Framework\Cache::getRealKey($key, $keyGroupName, false);
}
/**
@ -230,93 +159,9 @@ class CacheHandler extends Handler
*/
public function invalidateGroupKey($keyGroupName)
{
$this->keyGroupVersions[$keyGroupName]++;
$this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
return Rhymix\Framework\Cache::clearGroup($keyGroupName);
}
}
/**
* Base class of Cache
*
* @author NAVER (developer@xpressengine.com)
*/
class CacheBase
{
/**
* Default valid time
* @var int
*/
public $valid_time = 36000;
/**
* Get cached data
*
* @param string $key Cache key
* @param int $modified_time Unix time of data modified.
* 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.
*/
public function get($key, $modified_time = 0)
{
return false;
}
/**
* Put data into cache
*
* @param string $key Cache key
* @param mixed $obj Value of a variable to store. $value supports all data types except resources, such as file handlers.
* @param int $valid_time Time for the variable to live in the cache in seconds.
* After the value specified in ttl has passed the stored variable will be deleted from the cache.
* 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.
*/
public function put($key, $obj, $valid_time = 0)
{
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
*
* @param string $key Cache key
* @param int $modified_time Unix time of data modified.
* If stored time is older then modified time, the data is invalid.
* @return bool Return true on valid or false on invalid.
*/
public function isValid($key, $modified_time = 0)
{
return false;
}
/**
* Return whether support or not support cache
*
* @return boolean
*/
public function isSupport()
{
return false;
}
/**
* Truncate all cache
*
* @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
*/
public function truncate()
{
return false;
}
}
/* End of file CacheHandler.class.php */
/* Location: ./classes/cache/CacheHandler.class.php */