issue 2119. supporting php 5.4.

git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12684 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
flyskyko 2013-02-04 08:37:09 +00:00
parent 854e45afaa
commit bd1fa3651d
5 changed files with 173 additions and 75 deletions

View file

@ -1,11 +1,13 @@
<?php
/**
* Cache class for APC
*
* @author NHN (developer@xpressengine.com)
**/
* */
class CacheApc extends CacheBase
{
/**
* Default valid time
* @var int
@ -18,7 +20,7 @@ class CacheApc extends CacheBase
* @param void $opt Not used
* @return CacheApc instance of CacheApc
*/
function getInstance($opt=null)
function getInstance($opt = null)
{
if(!$GLOBALS['__CacheApc__'])
{
@ -34,6 +36,7 @@ class CacheApc extends CacheBase
*/
function CacheApc()
{
}
/**
@ -52,14 +55,18 @@ class CacheApc extends CacheBase
* @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.
* 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(time(), $buff), $valid_time);
if($valid_time == 0)
{
$valid_time = $this->valid_time;
}
return apc_store(md5(_XE_PATH_ . $key), array(time(), $buff), $valid_time);
}
/**
@ -67,14 +74,17 @@ class CacheApc extends CacheBase
*
* @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.
* 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);
$_key = md5(_XE_PATH_ . $key);
$obj = apc_fetch($_key, $success);
if(!$success || !is_array($obj)) return false;
if(!$success || !is_array($obj))
{
return false;
}
unset($obj[1]);
if($modified_time > 0 && $modified_time > $obj[0])
@ -91,14 +101,17 @@ class CacheApc extends CacheBase
*
* @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.
* 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);
$_key = md5(_XE_PATH_ . $key);
$obj = apc_fetch($_key, $success);
if(!$success || !is_array($obj)) return false;
if(!$success || !is_array($obj))
{
return false;
}
if($modified_time > 0 && $modified_time > $obj[0])
{
@ -117,7 +130,7 @@ class CacheApc extends CacheBase
*/
function _delete($_key)
{
$this->put($_key,null,1);
$this->put($_key, null, 1);
}
/**
@ -140,7 +153,7 @@ class CacheApc extends CacheBase
{
return apc_clear_cache('user');
}
}
}
/* End of file CacheApc.class.php */
/* Location: ./classes/cache/CacheApc.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* Cache class for file
*
@ -8,6 +9,7 @@
*/
class CacheFile extends CacheBase
{
/**
* Default valid time
* @var int
@ -27,7 +29,8 @@ class CacheFile extends CacheBase
*/
function getInstance()
{
if(!$GLOBALS['__CacheFile__']) {
if(!$GLOBALS['__CacheFile__'])
{
$GLOBALS['__CacheFile__'] = new CacheFile();
}
return $GLOBALS['__CacheFile__'];
@ -41,7 +44,10 @@ class CacheFile extends CacheBase
function CacheFile()
{
$this->cache_dir = _XE_PATH_ . $this->cache_dir;
if(!is_dir($this->cache_dir)) FileHandler::makeDir($this->cache_dir);
if(!is_dir($this->cache_dir))
{
FileHandler::makeDir($this->cache_dir);
}
}
/**
@ -75,7 +81,7 @@ class CacheFile extends CacheBase
*/
function put($key, $obj, $valid_time = 0)
{
$cache_file = $this->getCacheFileName($key);
$cache_file = $this->getCacheFileName($key);
$text = serialize($obj);
FileHandler::writeFile($cache_file, $text);
}
@ -90,7 +96,10 @@ class CacheFile extends CacheBase
function isValid($key, $modified_time = 0)
{
$cache_file = $this->getCacheFileName($key);
if(file_exists($cache_file)) return true;
if(file_exists($cache_file))
{
return true;
}
return false;
}
@ -106,7 +115,10 @@ class CacheFile extends CacheBase
{
$cache_file = $this->getCacheFileName($key);
$content = FileHandler::readFile($cache_file);
if(!$content) return false;
if(!$content)
{
return false;
}
return unserialize($content);
}
@ -143,7 +155,7 @@ class CacheFile extends CacheBase
{
FileHandler::removeFilesInDir($this->cache_dir);
}
}
}
/* End of file CacheFile.class.php */
/* Location: ./classes/cache/CacheFile.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* CacheHandler
*
@ -6,6 +7,7 @@
*/
class CacheHandler extends Handler
{
/**
* instance of cache handler
* @var CacheBase
@ -49,37 +51,59 @@ class CacheHandler extends Handler
*/
function CacheHandler($target, $info = null, $always_use_file = false)
{
if(!$info) $info = Context::getDBInfo();
if(!$info)
{
$info = Context::getDBInfo();
}
if($info)
{
if($target == 'object')
{
if($info->use_object_cache =='apc') $type = 'apc';
else if(substr($info->use_object_cache,0,8)=='memcache')
if($info->use_object_cache == 'apc')
{
$type = 'apc';
}
else if(substr($info->use_object_cache, 0, 8) == 'memcache')
{
$type = 'memcache';
$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($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')
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($info->use_template_cache == 'wincache') $type = 'wincache';
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->handler = call_user_func(array($class, 'getInstance'), $url);
$this->keyGroupVersions = $this->handler->get('key_group_versions', 0);
if(!$this->keyGroupVersions)
{
@ -97,7 +121,10 @@ class CacheHandler extends Handler
*/
function isSupport()
{
if($this->handler && $this->handler->isSupport()) return true;
if($this->handler && $this->handler->isSupport())
{
return true;
}
return false;
}
@ -106,12 +133,15 @@ class CacheHandler extends Handler
*
* @param string $key Cache key
* @param int $modified_time Unix time of data modified.
* 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.
*/
function get($key, $modified_time = 0)
{
if(!$this->handler) return false;
if(!$this->handler)
{
return false;
}
return $this->handler->get($key, $modified_time);
}
@ -121,13 +151,16 @@ class CacheHandler extends Handler
* @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.
* 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.
*/
function put($key, $obj, $valid_time = 0)
{
if(!$this->handler) return false;
if(!$this->handler)
{
return false;
}
return $this->handler->put($key, $obj, $valid_time);
}
@ -139,7 +172,10 @@ class CacheHandler extends Handler
*/
function delete($key)
{
if(!$this->handler) return false;
if(!$this->handler)
{
return false;
}
return $this->handler->delete($key);
}
@ -148,12 +184,15 @@ class CacheHandler extends Handler
*
* @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.
* 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)
{
if(!$this->handler) return false;
if(!$this->handler)
{
return false;
}
return $this->handler->isValid($key, $modified_time);
}
@ -164,7 +203,10 @@ class CacheHandler extends Handler
*/
function truncate()
{
if(!$this->handler) return false;
if(!$this->handler)
{
return false;
}
return $this->handler->truncate();
}
@ -206,6 +248,7 @@ class CacheHandler extends Handler
$this->keyGroupVersions[$keyGroupName]++;
$this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
}
}
/**
@ -215,12 +258,13 @@ class CacheHandler extends Handler
*/
class CacheBase
{
/**
* 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.
* 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)
@ -234,8 +278,8 @@ class CacheBase
* @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.
* 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.
*/
function put($key, $obj, $valid_time = 0)
@ -248,7 +292,7 @@ class CacheBase
*
* @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.
* 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)
@ -275,7 +319,7 @@ class CacheBase
{
return false;
}
}
}
/* End of file CacheHandler.class.php */
/* Location: ./classes/cache/CacheHandler.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* Cache class for memcache
*
@ -6,6 +7,7 @@
*/
class CacheMemcache extends CacheBase
{
/**
* Default valid time
* @var int
@ -26,7 +28,8 @@ class CacheMemcache extends CacheBase
*/
function getInstance($url)
{
if(!$GLOBALS['__CacheMemcache__']) {
if(!$GLOBALS['__CacheMemcache__'])
{
$GLOBALS['__CacheMemcache__'] = new CacheMemcache($url);
}
return $GLOBALS['__CacheMemcache__'];
@ -42,7 +45,7 @@ class CacheMemcache extends CacheBase
function CacheMemcache($url)
{
//$config['url'] = array('memcache://localhost:11211');
$config['url'] = is_array($url)?$url:array($url);
$config['url'] = is_array($url) ? $url : array($url);
$this->Memcache = new Memcache;
foreach($config['url'] as $url)
@ -59,8 +62,11 @@ class CacheMemcache extends CacheBase
*/
function isSupport()
{
if($GLOBALS['XE_MEMCACHE_SUPPORT']) return true;
if($this->Memcache->set('xe', 'xe', MEMCACHE_COMPRESSED, 1))
if($GLOBALS['XE_MEMCACHE_SUPPORT'])
{
return true;
}
if($this->Memcache->set('xe', 'xe', MEMCACHE_COMPRESSED, 1))
{
$GLOBALS['XE_MEMCACHE_SUPPORT'] = true;
}
@ -79,7 +85,7 @@ class CacheMemcache extends CacheBase
*/
function getKey($key)
{
return md5(_XE_PATH_.$key);
return md5(_XE_PATH_ . $key);
}
/**
@ -95,13 +101,16 @@ class CacheMemcache extends CacheBase
* @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.
* 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.
*/
function put($key, $buff, $valid_time = 0)
{
if($valid_time == 0) $valid_time = $this->valid_time;
if($valid_time == 0)
{
$valid_time = $this->valid_time;
}
return $this->Memcache->set($this->getKey($key), array(time(), $buff), MEMCACHE_COMPRESSED, $valid_time);
}
@ -111,7 +120,7 @@ class CacheMemcache extends CacheBase
*
* @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.
* 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)
@ -119,7 +128,10 @@ class CacheMemcache extends CacheBase
$_key = $this->getKey($key);
$obj = $this->Memcache->get($_key);
if(!$obj || !is_array($obj)) return false;
if(!$obj || !is_array($obj))
{
return false;
}
unset($obj[1]);
if($modified_time > 0 && $modified_time > $obj[0])
@ -138,14 +150,17 @@ class CacheMemcache extends CacheBase
*
* @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.
* 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 = $this->getKey($key);
$obj = $this->Memcache->get($_key);
if(!$obj || !is_array($obj)) return false;
if(!$obj || !is_array($obj))
{
return false;
}
if($modified_time > 0 && $modified_time > $obj[0])
{
@ -197,7 +212,7 @@ class CacheMemcache extends CacheBase
{
return $this->Memcache->flush();
}
}
}
/* End of file CacheMemcache.class.php */
/* Location: ./classes/cache/CacheMemcache.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* Cache class for Wincache
*
@ -6,7 +7,9 @@
*
* @author Arnia (support@xpressengine.org)
*/
class CacheWincache extends CacheBase {
class CacheWincache extends CacheBase
{
/**
* Default valid time
* @var int
@ -19,9 +22,10 @@ class CacheWincache extends CacheBase {
* @param void $opt Not used
* @return CacheWincache instance of CacheWincache
*/
function getInstance($opt=null)
function getInstance($opt = null)
{
if(!$GLOBALS['__CacheWincache__']) {
if(!$GLOBALS['__CacheWincache__'])
{
$GLOBALS['__CacheWincache__'] = new CacheWincache();
}
return $GLOBALS['__CacheWincache__'];
@ -34,6 +38,7 @@ class CacheWincache extends CacheBase {
*/
function CacheWincache()
{
}
/**
@ -50,17 +55,20 @@ class CacheWincache extends CacheBase {
* 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.
* 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.
* 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(time(), $buff), $valid_time);
if($valid_time == 0)
{
$valid_time = $this->valid_time;
}
return wincache_ucache_set(md5(_XE_PATH_ . $key), array(time(), $buff), $valid_time);
}
/**
@ -68,14 +76,17 @@ class CacheWincache extends CacheBase {
*
* @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.
* 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);
$_key = md5(_XE_PATH_ . $key);
$obj = wincache_ucache_get($_key, $success);
if(!$success || !is_array($obj)) return false;
if(!$success || !is_array($obj))
{
return false;
}
unset($obj[1]);
if($modified_time > 0 && $modified_time > $obj[0])
@ -83,7 +94,7 @@ class CacheWincache extends CacheBase {
$this->_delete($_key);
return false;
}
return true;
}
@ -92,14 +103,17 @@ class CacheWincache extends CacheBase {
*
* @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.
* 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);
$_key = md5(_XE_PATH_ . $key);
$obj = wincache_ucache_get($_key, $success);
if(!$success || !is_array($obj)) return false;
if(!$success || !is_array($obj))
{
return false;
}
if($modified_time > 0 && $modified_time > $obj[0])
{
@ -129,7 +143,7 @@ class CacheWincache extends CacheBase {
*/
function delete($key)
{
$_key = md5(_XE_PATH_.$key);
$_key = md5(_XE_PATH_ . $key);
$this->_delete($_key);
}
@ -142,7 +156,7 @@ class CacheWincache extends CacheBase {
{
return wincache_ucache_clear();
}
}
}
/* End of file CacheWincache.class.php */
/* Location: ./classes/cache/CacheWincache.class.php */