mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 01:01:41 +09:00
Update CacheHandler to use Rhymix Framework instead
This commit is contained in:
parent
568151b5ee
commit
7d80bbe27d
6 changed files with 17 additions and 1116 deletions
154
classes/cache/CacheApc.class.php
vendored
154
classes/cache/CacheApc.class.php
vendored
|
|
@ -1,154 +0,0 @@
|
|||
<?php
|
||||
/* Copyright (C) NAVER <http://www.navercorp.com> */
|
||||
|
||||
/**
|
||||
* Cache class for APC
|
||||
*
|
||||
* @author NAVER (developer@xpressengine.com)
|
||||
* */
|
||||
class CacheApc extends CacheBase
|
||||
{
|
||||
public static $isSupport = false;
|
||||
|
||||
/**
|
||||
* Get instance of CacheApc
|
||||
*
|
||||
* @param void $opt Not used
|
||||
* @return CacheApc instance of CacheApc
|
||||
*/
|
||||
function getInstance($opt = null)
|
||||
{
|
||||
if(!$GLOBALS['__CacheApc__'])
|
||||
{
|
||||
$GLOBALS['__CacheApc__'] = new CacheApc();
|
||||
}
|
||||
return $GLOBALS['__CacheApc__'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether support or not support cache
|
||||
*
|
||||
* @return bool Return true on support or false on not support
|
||||
*/
|
||||
function isSupport()
|
||||
{
|
||||
return self::$isSupport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache a variable in the data store
|
||||
*
|
||||
* @param string $key Store the variable using this name. $key are cache-unique, so storing a second value with the same $key will overwrite the original value.
|
||||
* @param mixed $buff The variable to store
|
||||
* @param int $valid_time Time To Live; store $buff in the cache for ttl seconds.
|
||||
* After the ttl has passed., the stored variable will be expunged from the cache (on the next request).
|
||||
* If no ttl is supplied, use the default valid time CacheApc::valid_time.
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
function put($key, $buff, $valid_time = 0)
|
||||
{
|
||||
if($valid_time == 0)
|
||||
{
|
||||
$valid_time = $this->valid_time;
|
||||
}
|
||||
|
||||
return apc_store(md5(_XE_PATH_ . $key), array($_SERVER['REQUEST_TIME'], $buff), $valid_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
function isValid($key, $modified_time = 0)
|
||||
{
|
||||
$_key = md5(_XE_PATH_ . $key);
|
||||
$obj = apc_fetch($_key, $success);
|
||||
if(!$success || !is_array($obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
unset($obj[1]);
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0])
|
||||
{
|
||||
$this->delete($key);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a stored variable from the cache
|
||||
*
|
||||
* @param string $key The $key used to store the value.
|
||||
* @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.
|
||||
*/
|
||||
function get($key, $modified_time = 0)
|
||||
{
|
||||
$_key = md5(_XE_PATH_ . $key);
|
||||
$obj = apc_fetch($_key, $success);
|
||||
if(!$success || !is_array($obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0])
|
||||
{
|
||||
$this->delete($key);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $obj[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete variable from the cache
|
||||
*
|
||||
* @param string $key Used to store the value.
|
||||
* @return void
|
||||
*/
|
||||
function delete($key)
|
||||
{
|
||||
$_key = md5(_XE_PATH_ . $key);
|
||||
return apc_delete($_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate all existing variables at the cache
|
||||
*
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
function truncate()
|
||||
{
|
||||
return apc_clear_cache('user');
|
||||
}
|
||||
|
||||
/**
|
||||
* @DEPRECATED
|
||||
*/
|
||||
function _delete($key)
|
||||
{
|
||||
return $this->delete($key);
|
||||
}
|
||||
}
|
||||
|
||||
CacheApc::$isSupport = function_exists('apc_add');
|
||||
|
||||
/* End of file CacheApc.class.php */
|
||||
/* Location: ./classes/cache/CacheApc.class.php */
|
||||
171
classes/cache/CacheFile.class.php
vendored
171
classes/cache/CacheFile.class.php
vendored
|
|
@ -1,171 +0,0 @@
|
|||
<?php
|
||||
/* Copyright (C) NAVER <http://www.navercorp.com> */
|
||||
|
||||
/**
|
||||
* Cache class for file
|
||||
*
|
||||
* Filedisk Cache Handler
|
||||
*
|
||||
* @author NAVER (developers@xpressengine.com)
|
||||
*/
|
||||
class CacheFile extends CacheBase
|
||||
{
|
||||
/**
|
||||
* Path that value to stored
|
||||
* @var string
|
||||
*/
|
||||
var $cache_dir = 'files/cache/store/';
|
||||
|
||||
/**
|
||||
* Get instance of CacheFile
|
||||
*
|
||||
* @return CacheFile instance of CacheFile
|
||||
*/
|
||||
function getInstance()
|
||||
{
|
||||
if(!$GLOBALS['__CacheFile__'])
|
||||
{
|
||||
$GLOBALS['__CacheFile__'] = new CacheFile();
|
||||
}
|
||||
return $GLOBALS['__CacheFile__'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
$this->cache_dir = _XE_PATH_ . $this->cache_dir;
|
||||
FileHandler::makeDir($this->cache_dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache file name by key
|
||||
*
|
||||
* @param string $key The key that will be associated with the item.
|
||||
* @return string Returns cache file path
|
||||
*/
|
||||
function getCacheFileName($key)
|
||||
{
|
||||
return $this->cache_dir . str_replace(':', DIRECTORY_SEPARATOR, $key) . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether support or not support cache
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
function isSupport()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache a variable in the data store
|
||||
*
|
||||
* @param string $key Store the variable using this name.
|
||||
* @param mixed $obj The variable to store
|
||||
* @param int $valid_time Not used
|
||||
* @return void
|
||||
*/
|
||||
function put($key, $obj, $valid_time = 0)
|
||||
{
|
||||
$cache_file = $this->getCacheFileName($key);
|
||||
$content = array();
|
||||
$content[] = '<?php';
|
||||
$content[] = 'if(!defined(\'__XE__\')) { exit(); }';
|
||||
$content[] = 'return \'' . addslashes(serialize($obj)) . '\';';
|
||||
FileHandler::writeFile($cache_file, implode(PHP_EOL, $content));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, return false.
|
||||
* @return bool Return true on valid or false on invalid.
|
||||
*/
|
||||
function isValid($key, $modified_time = 0)
|
||||
{
|
||||
$cache_file = $this->getCacheFileName($key);
|
||||
|
||||
if(file_exists($cache_file))
|
||||
{
|
||||
if($modified_time > 0 && filemtime($cache_file) < $modified_time)
|
||||
{
|
||||
FileHandler::removeFile($cache_file);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a stored variable from the cache
|
||||
*
|
||||
* @param string $key The $key used to store the value.
|
||||
* @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. Return the string associated with the $key on success.
|
||||
*/
|
||||
function get($key, $modified_time = 0)
|
||||
{
|
||||
if(!$cache_file = FileHandler::exists($this->getCacheFileName($key)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if($modified_time > 0 && filemtime($cache_file) < $modified_time)
|
||||
{
|
||||
FileHandler::removeFile($cache_file);
|
||||
return false;
|
||||
}
|
||||
|
||||
$content = include($cache_file);
|
||||
|
||||
return unserialize(stripslashes($content));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete variable from the cache(private)
|
||||
*
|
||||
* @param string $_key Used to store the value.
|
||||
* @return void
|
||||
*/
|
||||
function _delete($_key)
|
||||
{
|
||||
$cache_file = $this->getCacheFileName($_key);
|
||||
FileHandler::removeFile($cache_file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete variable from the cache
|
||||
*
|
||||
* @param string $key Used to store the value.
|
||||
* @return void
|
||||
*/
|
||||
function delete($key)
|
||||
{
|
||||
$this->_delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate all existing variables at the cache
|
||||
*
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
function truncate()
|
||||
{
|
||||
FileHandler::removeFilesInDir($this->cache_dir);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
/* End of file CacheFile.class.php */
|
||||
/* Location: ./classes/cache/CacheFile.class.php */
|
||||
189
classes/cache/CacheHandler.class.php
vendored
189
classes/cache/CacheHandler.class.php
vendored
|
|
@ -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 */
|
||||
|
|
|
|||
219
classes/cache/CacheMemcache.class.php
vendored
219
classes/cache/CacheMemcache.class.php
vendored
|
|
@ -1,219 +0,0 @@
|
|||
<?php
|
||||
/* Copyright (C) NAVER <http://www.navercorp.com> */
|
||||
|
||||
/**
|
||||
* Cache class for memcache
|
||||
*
|
||||
* @author NAVER (developer@xpressengine.com)
|
||||
*/
|
||||
class CacheMemcache extends CacheBase
|
||||
{
|
||||
/**
|
||||
* Instance of Memcache
|
||||
*/
|
||||
protected static $_instance;
|
||||
protected $_conn;
|
||||
protected $_status;
|
||||
protected $_useExtension;
|
||||
|
||||
/**
|
||||
* Get instance of CacheMemcache
|
||||
*
|
||||
* @param string $url url of memcache
|
||||
* @return CacheMemcache instance of CacheMemcache
|
||||
*/
|
||||
public static function getInstance($url, $force_new_instance = false)
|
||||
{
|
||||
if(!self::$_instance || $force_new_instance)
|
||||
{
|
||||
self::$_instance = new self($url);
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* Do not use this directly. You can use getInstance() instead.
|
||||
* @param string $url url of memcache
|
||||
* @return void
|
||||
*/
|
||||
protected function __construct($url)
|
||||
{
|
||||
//$config['url'] = array('memcache://localhost:11211');
|
||||
$config['url'] = is_array($url) ? $url : array($url);
|
||||
if(class_exists('Memcached'))
|
||||
{
|
||||
$this->_conn = new Memcached;
|
||||
$this->_useExtension = 'Memcached';
|
||||
}
|
||||
elseif(class_exists('Memcache'))
|
||||
{
|
||||
$this->_conn = new Memcache;
|
||||
$this->_useExtension = 'Memcache';
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach($config['url'] as $url)
|
||||
{
|
||||
$info = parse_url($url);
|
||||
$this->_conn->addServer($info['host'], $info['port']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether support or not support cache
|
||||
*
|
||||
* @return bool Return true on support or false on not support
|
||||
*/
|
||||
public function isSupport()
|
||||
{
|
||||
if(isset($this->_status))
|
||||
{
|
||||
return $this->_status;
|
||||
}
|
||||
|
||||
if($this->_useExtension === 'Memcached')
|
||||
{
|
||||
return $this->_status = $this->_conn->set('xe', 'xe', 1);
|
||||
}
|
||||
elseif($this->_useExtension === 'Memcache')
|
||||
{
|
||||
return $this->_status = $this->_conn->set('xe', 'xe', MEMCACHE_COMPRESSED, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->_status = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unique key of given key by path of XE
|
||||
*
|
||||
* @param string $key Cache key
|
||||
* @return string Return unique key
|
||||
*/
|
||||
protected function getKey($key)
|
||||
{
|
||||
return md5(_XE_PATH_ . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store data at the server
|
||||
*
|
||||
* CacheMemcache::put() stores an item $buff with $key on the memcached server.
|
||||
* Parameter $valid_time is expiration time in seconds. If it's 0, the item never expires
|
||||
* (but memcached server doesn't guarantee this item to be stored all the time, it could be delete from the cache to make place for other items).
|
||||
*
|
||||
* Remember that resource variables (i.e. file and connection descriptors) cannot be stored in the cache,
|
||||
* because they can not be adequately represented in serialized state.
|
||||
*
|
||||
* @param string $key The key that will be associated with the item.
|
||||
* @param mixed $buff The variable to store. Strings and integers are stored as is, other types are stored serialized.
|
||||
* @param int $valid_time Expiration time of the item.
|
||||
* You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days).
|
||||
* If it's equal to zero, use the default valid time CacheMemcache::valid_time.
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
public function put($key, $buff, $valid_time = 0)
|
||||
{
|
||||
if($valid_time == 0)
|
||||
{
|
||||
$valid_time = $this->valid_time;
|
||||
}
|
||||
|
||||
if($this->_useExtension === 'Memcached')
|
||||
{
|
||||
return $this->_conn->set($this->getKey($key), array(time(), $buff), $valid_time);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->_conn->set($this->getKey($key), array(time(), $buff), MEMCACHE_COMPRESSED, $valid_time);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$obj = $this->_conn->get($this->getKey($key));
|
||||
if(!$obj || !is_array($obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0])
|
||||
{
|
||||
$this->delete($key);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve item from the server
|
||||
*
|
||||
* CacheMemcache::get() returns previously stored data if an item with such $key exists on the server at this moment.
|
||||
*
|
||||
* @param string $key The key to fetch
|
||||
* @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)
|
||||
{
|
||||
$obj = $this->_conn->get($this->getKey($key));
|
||||
if(!$obj || !is_array($obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0])
|
||||
{
|
||||
$this->delete($key);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $obj[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete item from the server
|
||||
*
|
||||
* CacheMemcache::delete() deletes an item with tey $key.
|
||||
*
|
||||
* @param string $key The key associated with the item to delete.
|
||||
* @return void
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return $this->_conn->delete($this->getKey($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush all existing items at the server
|
||||
*
|
||||
* CacheMemcache::truncate() immediately invalidates all existing items.
|
||||
* CacheMemcache::truncate() doesn't actually free any resources, it only marks all the items as expired,
|
||||
* so occupied memory will be overwitten by new items.
|
||||
*
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
public function truncate()
|
||||
{
|
||||
return $this->_conn->flush();
|
||||
}
|
||||
|
||||
}
|
||||
/* End of file CacheMemcache.class.php */
|
||||
/* Location: ./classes/cache/CacheMemcache.class.php */
|
||||
242
classes/cache/CacheRedis.class.php
vendored
242
classes/cache/CacheRedis.class.php
vendored
|
|
@ -1,242 +0,0 @@
|
|||
<?php
|
||||
/* Copyright (C) NAVER <http://www.navercorp.com> */
|
||||
|
||||
/**
|
||||
* Cache class for Redis
|
||||
*
|
||||
* @author NAVER (developer@xpressengine.com)
|
||||
*/
|
||||
class CacheRedis extends CacheBase
|
||||
{
|
||||
/**
|
||||
* Instance of Memcache
|
||||
*/
|
||||
protected static $_instance;
|
||||
protected $_conn;
|
||||
protected $_status;
|
||||
|
||||
/**
|
||||
* Get instance of CacheRedis
|
||||
*
|
||||
* @param string $url url of Redis
|
||||
* @return CacheRedis instance of CacheRedis
|
||||
*/
|
||||
public static function getInstance($url, $force_new_instance = false)
|
||||
{
|
||||
if(!self::$_instance || $force_new_instance)
|
||||
{
|
||||
self::$_instance = new self($url);
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* Do not use this directly. You can use getInstance() instead.
|
||||
* @param string $url url of Redis
|
||||
* @return void
|
||||
*/
|
||||
protected function __construct($url)
|
||||
{
|
||||
//$url = 'redis://localhost:6379/1';
|
||||
$url = is_array($url) ? reset($url) : $url;
|
||||
|
||||
if(!class_exists('Redis'))
|
||||
{
|
||||
$this->_status = false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$this->_conn = new Redis;
|
||||
$info = parse_url($url);
|
||||
$this->_conn->connect($info['host'], $info['port'], 0.15);
|
||||
if(isset($info['user']) || isset($info['pass']))
|
||||
{
|
||||
$this->_conn->auth(isset($info['user']) ? $info['user'] : $info['pass']);
|
||||
}
|
||||
if(isset($info['path']) && $dbnum = intval(substr($info['path'], 1)))
|
||||
{
|
||||
$this->_conn->select($dbnum);
|
||||
}
|
||||
}
|
||||
catch(RedisException $e)
|
||||
{
|
||||
$this->_status = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether support or not support cache
|
||||
*
|
||||
* @return bool Return true on support or false on not support
|
||||
*/
|
||||
public function isSupport()
|
||||
{
|
||||
if($this->_status !== null)
|
||||
{
|
||||
return $this->_status;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return $this->_conn->setex('xe', 1, 'xe');
|
||||
}
|
||||
catch(RedisException $e)
|
||||
{
|
||||
return $this->_status = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unique key of given key by path of XE
|
||||
*
|
||||
* @param string $key Cache key
|
||||
* @return string Return unique key
|
||||
*/
|
||||
protected function getKey($key)
|
||||
{
|
||||
static $prefix = null;
|
||||
if($prefix === null)
|
||||
{
|
||||
$prefix = substr(sha1(_XE_PATH_), 0, 12) . ':';
|
||||
}
|
||||
return $prefix . $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store data at the server
|
||||
*
|
||||
* CacheRedis::put() stores an item $buff with $key on the Redis server.
|
||||
* Parameter $valid_time is expiration time in seconds. If it's 0, the item never expires
|
||||
* (but Redis server doesn't guarantee this item to be stored all the time, it could be delete from the cache to make place for other items).
|
||||
*
|
||||
* Remember that resource variables (i.e. file and connection descriptors) cannot be stored in the cache,
|
||||
* because they can not be adequately represented in serialized state.
|
||||
*
|
||||
* @param string $key The key that will be associated with the item.
|
||||
* @param mixed $buff The variable to store. Strings and integers are stored as is, other types are stored serialized.
|
||||
* @param int $valid_time Expiration time of the item.
|
||||
* You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days).
|
||||
* If it's equal to zero, use the default valid time CacheRedis::valid_time.
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
public function put($key, $buff, $valid_time = 0)
|
||||
{
|
||||
if($valid_time > 60 * 60 * 24 * 30)
|
||||
{
|
||||
$valid_time = $valid_time - time();
|
||||
}
|
||||
if($valid_time <= 0)
|
||||
{
|
||||
$valid_time = $this->valid_time;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return $this->_conn->setex($this->getKey($key), $valid_time, serialize(array($_SERVER['REQUEST_TIME'], $buff)));
|
||||
}
|
||||
catch(RedisException $e)
|
||||
{
|
||||
return $this->_status = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$obj = $this->_conn->get($this->getKey($key));
|
||||
$obj = $obj ? unserialize($obj) : false;
|
||||
if(!$obj || !is_array($obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0])
|
||||
{
|
||||
$this->_conn->del($this->getKey($key));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve item from the server
|
||||
*
|
||||
* CacheRedis::get() returns previously stored data if an item with such $key exists on the server at this moment.
|
||||
*
|
||||
* @param string $key The key to fetch
|
||||
* @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)
|
||||
{
|
||||
$obj = $this->_conn->get($this->getKey($key));
|
||||
$obj = $obj ? unserialize($obj) : false;
|
||||
if(!$obj || !is_array($obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0])
|
||||
{
|
||||
$this->_conn->del($this->getKey($key));
|
||||
return false;
|
||||
}
|
||||
|
||||
return $obj[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete item from the server
|
||||
*
|
||||
* CacheRedis::delete() deletes an item with tey $key.
|
||||
*
|
||||
* @param string $key The key associated with the item to delete.
|
||||
* @return void
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
try
|
||||
{
|
||||
return $this->_conn->del($this->getKey($key));
|
||||
}
|
||||
catch(RedisException $e)
|
||||
{
|
||||
return $this->_status = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush all existing items at the server
|
||||
*
|
||||
* CacheRedis::truncate() immediately invalidates all existing items.
|
||||
* If using multiple databases, items in other databases are not affected.
|
||||
*
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
public function truncate()
|
||||
{
|
||||
try
|
||||
{
|
||||
return $this->_conn->flushDB();
|
||||
}
|
||||
catch(RedisException $e)
|
||||
{
|
||||
return $this->_status = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/* End of file CacheRedis.class.php */
|
||||
/* Location: ./classes/cache/CacheRedis.class.php */
|
||||
158
classes/cache/CacheWincache.class.php
vendored
158
classes/cache/CacheWincache.class.php
vendored
|
|
@ -1,158 +0,0 @@
|
|||
<?php
|
||||
/* Copyright (C) NAVER <http://www.navercorp.com> */
|
||||
|
||||
/**
|
||||
* Cache class for Wincache
|
||||
*
|
||||
* Wincache Handler
|
||||
*
|
||||
* @author Arnia (support@xpressengine.org)
|
||||
*/
|
||||
class CacheWincache extends CacheBase
|
||||
{
|
||||
public static $isSupport = false;
|
||||
|
||||
/**
|
||||
* Get instance of CacheWincache
|
||||
*
|
||||
* @param void $opt Not used
|
||||
* @return CacheWincache instance of CacheWincache
|
||||
*/
|
||||
function getInstance($opt = null)
|
||||
{
|
||||
if(!$GLOBALS['__CacheWincache__'])
|
||||
{
|
||||
$GLOBALS['__CacheWincache__'] = new CacheWincache();
|
||||
}
|
||||
return $GLOBALS['__CacheWincache__'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether support or not support cache
|
||||
*
|
||||
* @return bool Return true on support or false on not support
|
||||
*/
|
||||
function isSupport()
|
||||
{
|
||||
return self::$isSupport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a variable in user cache and overwrites a variable if it already exists in the cache
|
||||
*
|
||||
* @param string $key Store the variable using this $key value.
|
||||
* If a variable with same $key is already present the function will overwrite the previous value with the new one.
|
||||
* @param mixed $buff 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 CacheWincache::valid_time.
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
function put($key, $buff, $valid_time = 0)
|
||||
{
|
||||
if($valid_time == 0)
|
||||
{
|
||||
$valid_time = $this->valid_time;
|
||||
}
|
||||
return wincache_ucache_set(md5(_XE_PATH_ . $key), array($_SERVER['REQUEST_TIME'], $buff), $valid_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
function isValid($key, $modified_time = 0)
|
||||
{
|
||||
$_key = md5(_XE_PATH_ . $key);
|
||||
$obj = wincache_ucache_get($_key, $success);
|
||||
if(!$success || !is_array($obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
unset($obj[1]);
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0])
|
||||
{
|
||||
$this->_delete($_key);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a variable stored in the user cache
|
||||
*
|
||||
* @param string $key The $key that was used to store the variable in the cache.
|
||||
* @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.
|
||||
*/
|
||||
function get($key, $modified_time = 0)
|
||||
{
|
||||
$_key = md5(_XE_PATH_ . $key);
|
||||
$obj = wincache_ucache_get($_key, $success);
|
||||
if(!$success || !is_array($obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0])
|
||||
{
|
||||
$this->_delete($_key);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $obj[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete variable from the cache(private)
|
||||
*
|
||||
* @param string $_key Used to store the value.
|
||||
* @return void
|
||||
*/
|
||||
function _delete($_key)
|
||||
{
|
||||
wincache_ucache_delete($_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete variable from the cache
|
||||
*
|
||||
* @param string $key Used to store the value.
|
||||
* @return void
|
||||
*/
|
||||
function delete($key)
|
||||
{
|
||||
$_key = md5(_XE_PATH_ . $key);
|
||||
$this->_delete($_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate all existing variables at the cache
|
||||
*
|
||||
* @return bool Returns true on success or false on failure.
|
||||
*/
|
||||
function truncate()
|
||||
{
|
||||
return wincache_ucache_clear();
|
||||
}
|
||||
}
|
||||
|
||||
CacheWincache::$isSupport = function_exists('wincache_ucache_set');
|
||||
/* End of file CacheWincache.class.php */
|
||||
/* Location: ./classes/cache/CacheWincache.class.php */
|
||||
Loading…
Add table
Add a link
Reference in a new issue