Commit fcf2d0a3 by Qiang Xue

Fixes #2823.

parent 6e8c4602
......@@ -64,23 +64,23 @@ class ApcCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire)
protected function setValue($key, $value, $duration)
{
return apc_store($key, $value, $expire);
return apc_store($key, $value, $duration);
}
/**
* Stores multiple key-value pairs in cache.
* @param array $data array where key corresponds to cache key while value
* @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function setValues($data, $expire)
protected function setValues($data, $duration)
{
return array_keys(apc_store($data, null, $expire));
return array_keys(apc_store($data, null, $duration));
}
/**
......@@ -88,23 +88,23 @@ class ApcCache extends Cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire)
protected function addValue($key, $value, $duration)
{
return apc_add($key, $value, $expire);
return apc_add($key, $value, $duration);
}
/**
* Adds multiple key-value pairs to cache.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function addValues($data, $expire)
protected function addValues($data, $duration)
{
return array_keys(apc_add($data, null, $expire));
return array_keys(apc_add($data, null, $duration));
}
/**
......
......@@ -27,7 +27,7 @@ use yii\helpers\StringHelper;
* $data = $cache->get($key);
* if ($data === false) {
* // ...generate $data here...
* $cache->set($key, $data, $expire, $dependency);
* $cache->set($key, $data, $duration, $dependency);
* }
* ~~~
*
......@@ -194,13 +194,13 @@ abstract class Cache extends Component implements \ArrayAccess
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return boolean whether the value is successfully stored into cache
*/
public function set($key, $value, $expire = 0, $dependency = null)
public function set($key, $value, $duration = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
......@@ -212,7 +212,7 @@ abstract class Cache extends Component implements \ArrayAccess
}
$key = $this->buildKey($key);
return $this->setValue($key, $value, $expire);
return $this->setValue($key, $value, $duration);
}
/**
......@@ -221,13 +221,13 @@ abstract class Cache extends Component implements \ArrayAccess
* expiration time will be replaced with the new ones, respectively.
*
* @param array $items the items to be cached, as key-value pairs.
* @param integer $expire default number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration default number of seconds in which the cached values will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached items. If the dependency changes,
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return boolean whether the items are successfully stored into cache
*/
public function mset($items, $expire = 0, $dependency = null)
public function mset($items, $duration = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
......@@ -245,7 +245,7 @@ abstract class Cache extends Component implements \ArrayAccess
$data[$key] = $value;
}
return $this->setValues($data, $expire);
return $this->setValues($data, $duration);
}
/**
......@@ -253,13 +253,13 @@ abstract class Cache extends Component implements \ArrayAccess
* If the cache already contains such a key, the existing value and expiration time will be preserved.
*
* @param array $items the items to be cached, as key-value pairs.
* @param integer $expire default number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration default number of seconds in which the cached values will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached items. If the dependency changes,
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return boolean whether the items are successfully stored into cache
*/
public function madd($items, $expire = 0, $dependency = null)
public function madd($items, $duration = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
......@@ -277,7 +277,7 @@ abstract class Cache extends Component implements \ArrayAccess
$data[$key] = $value;
}
return $this->addValues($data, $expire);
return $this->addValues($data, $duration);
}
/**
......@@ -286,13 +286,13 @@ abstract class Cache extends Component implements \ArrayAccess
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return boolean whether the value is successfully stored into cache
*/
public function add($key, $value, $expire = 0, $dependency = null)
public function add($key, $value, $duration = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
......@@ -304,7 +304,7 @@ abstract class Cache extends Component implements \ArrayAccess
}
$key = $this->buildKey($key);
return $this->addValue($key, $value, $expire);
return $this->addValue($key, $value, $duration);
}
/**
......@@ -345,10 +345,10 @@ abstract class Cache extends Component implements \ArrayAccess
* in specific cache storage.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
abstract protected function setValue($key, $value, $expire);
abstract protected function setValue($key, $value, $duration);
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
......@@ -356,10 +356,10 @@ abstract class Cache extends Component implements \ArrayAccess
* in specific cache storage.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
abstract protected function addValue($key, $value, $expire);
abstract protected function addValue($key, $value, $duration);
/**
* Deletes a value with the specified key from cache
......@@ -399,14 +399,14 @@ abstract class Cache extends Component implements \ArrayAccess
* The default implementation calls [[setValue()]] multiple times store values one by one. If the underlying cache
* storage supports multi-set, this method should be overridden to exploit that feature.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function setValues($data, $expire)
protected function setValues($data, $duration)
{
$failedKeys = [];
foreach ($data as $key => $value) {
if ($this->setValue($key, $value, $expire) === false) {
if ($this->setValue($key, $value, $duration) === false) {
$failedKeys[] = $key;
}
}
......@@ -419,14 +419,14 @@ abstract class Cache extends Component implements \ArrayAccess
* The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache
* storage supports multi-add, this method should be overridden to exploit that feature.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function addValues($data, $expire)
protected function addValues($data, $duration)
{
$failedKeys = [];
foreach ($data as $key => $value) {
if ($this->addValue($key, $value, $expire) === false) {
if ($this->addValue($key, $value, $duration) === false) {
$failedKeys[] = $key;
}
}
......
......@@ -178,14 +178,14 @@ class DbCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire)
protected function setValue($key, $value, $duration)
{
$command = $this->db->createCommand()
->update($this->cacheTable, [
'expire' => $expire > 0 ? $expire + time() : 0,
'expire' => $duration > 0 ? $duration + time() : 0,
'data' => [$value, \PDO::PARAM_LOB],
], ['id' => $key]);
......@@ -194,7 +194,7 @@ class DbCache extends Cache
return true;
} else {
return $this->addValue($key, $value, $expire);
return $this->addValue($key, $value, $duration);
}
}
......@@ -204,24 +204,18 @@ class DbCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire)
protected function addValue($key, $value, $duration)
{
$this->gc();
if ($expire > 0) {
$expire += time();
} else {
$expire = 0;
}
try {
$this->db->createCommand()
->insert($this->cacheTable, [
'id' => $key,
'expire' => $expire,
'expire' => $duration > 0 ? $duration + time() : 0,
'data' => [$value, \PDO::PARAM_LOB],
])->execute();
......
......@@ -37,10 +37,10 @@ class DummyCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire)
protected function setValue($key, $value, $duration)
{
return true;
}
......@@ -50,10 +50,10 @@ class DummyCache extends Cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire)
protected function addValue($key, $value, $duration)
{
return true;
}
......
......@@ -119,16 +119,11 @@ class FileCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire)
protected function setValue($key, $value, $duration)
{
if ($expire <= 0) {
$expire = 31536000; // 1 year
}
$expire += time();
$cacheFile = $this->getCacheFile($key);
if ($this->directoryLevel > 0) {
@FileHelper::createDirectory(dirname($cacheFile), $this->dirMode, true);
......@@ -137,8 +132,10 @@ class FileCache extends Cache
if ($this->fileMode !== null) {
@chmod($cacheFile, $this->fileMode);
}
return @touch($cacheFile, $expire);
if ($duration <= 0) {
$duration = 31536000; // 1 year
}
return @touch($cacheFile, $duration + time());
} else {
return false;
}
......@@ -150,17 +147,17 @@ class FileCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire)
protected function addValue($key, $value, $duration)
{
$cacheFile = $this->getCacheFile($key);
if (@filemtime($cacheFile) > time()) {
return false;
}
return $this->setValue($key, $value, $expire);
return $this->setValue($key, $value, $duration);
}
/**
......@@ -227,10 +224,10 @@ class FileCache extends Cache
* Recursively removing expired cache files under a directory.
* This method is mainly used by [[gc()]].
* @param string $path the directory under which expired cache files are removed.
* @param boolean $expiredOnly whether to only remove expired cache files. If false, all files
* @param boolean $durationdOnly whether to only remove expired cache files. If false, all files
* under `$path` will be removed.
*/
protected function gcRecursive($path, $expiredOnly)
protected function gcRecursive($path, $durationdOnly)
{
if (($handle = opendir($path)) !== false) {
while (($file = readdir($handle)) !== false) {
......@@ -239,11 +236,11 @@ class FileCache extends Cache
}
$fullPath = $path . DIRECTORY_SEPARATOR . $file;
if (is_dir($fullPath)) {
$this->gcRecursive($fullPath, $expiredOnly);
if (!$expiredOnly) {
$this->gcRecursive($fullPath, $durationdOnly);
if (!$durationdOnly) {
@rmdir($fullPath);
}
} elseif (!$expiredOnly || $expiredOnly && @filemtime($fullPath) < time()) {
} elseif (!$durationdOnly || $durationdOnly && @filemtime($fullPath) < time()) {
@unlink($fullPath);
}
}
......
......@@ -188,39 +188,28 @@ class MemCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire)
protected function setValue($key, $value, $duration)
{
if ($expire > 0) {
$expire += time();
} else {
$expire = 0;
}
$expire = $duration > 0 ? $duration + time() : 0;
return $this->useMemcached ? $this->_cache->set($key, $value, $expire) : $this->_cache->set($key, $value, 0, $expire);
}
/**
* Stores multiple key-value pairs in cache.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys. Always empty in case of using memcached.
*/
protected function setValues($data, $expire)
protected function setValues($data, $duration)
{
if ($this->useMemcached) {
if ($expire > 0) {
$expire += time();
} else {
$expire = 0;
}
$this->_cache->setMulti($data, $expire);
$this->_cache->setMulti($data, $duration > 0 ? $duration + time() : 0);
return [];
} else {
return parent::setValues($data, $expire);
return parent::setValues($data, $duration);
}
}
......@@ -230,17 +219,12 @@ class MemCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire)
protected function addValue($key, $value, $duration)
{
if ($expire > 0) {
$expire += time();
} else {
$expire = 0;
}
$expire = $duration > 0 ? $duration + time() : 0;
return $this->useMemcached ? $this->_cache->add($key, $value, $expire) : $this->_cache->add($key, $value, 0, $expire);
}
......
......@@ -64,23 +64,23 @@ class WinCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire)
protected function setValue($key, $value, $duration)
{
return wincache_ucache_set($key, $value, $expire);
return wincache_ucache_set($key, $value, $duration);
}
/**
* Stores multiple key-value pairs in cache.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function setValues($data, $expire)
protected function setValues($data, $duration)
{
return wincache_ucache_set($data, null, $expire);
return wincache_ucache_set($data, null, $duration);
}
/**
......@@ -89,12 +89,12 @@ class WinCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire)
protected function addValue($key, $value, $duration)
{
return wincache_ucache_add($key, $value, $expire);
return wincache_ucache_add($key, $value, $duration);
}
/**
......@@ -102,12 +102,12 @@ class WinCache extends Cache
* The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache
* storage supports multiadd, this method should be overridden to exploit that feature.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function addValues($data, $expire)
protected function addValues($data, $duration)
{
return wincache_ucache_add($data, null, $expire);
return wincache_ucache_add($data, null, $duration);
}
/**
......
......@@ -55,12 +55,12 @@ class XCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire)
protected function setValue($key, $value, $duration)
{
return xcache_set($key, $value, $expire);
return xcache_set($key, $value, $duration);
}
/**
......@@ -69,12 +69,12 @@ class XCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire)
protected function addValue($key, $value, $duration)
{
return !xcache_isset($key) ? $this->setValue($key, $value, $expire) : false;
return !xcache_isset($key) ? $this->setValue($key, $value, $duration) : false;
}
/**
......
......@@ -39,12 +39,12 @@ class ZendDataCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire)
protected function setValue($key, $value, $duration)
{
return zend_shm_cache_store($key, $value, $expire);
return zend_shm_cache_store($key, $value, $duration);
}
/**
......@@ -53,12 +53,12 @@ class ZendDataCache extends Cache
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire)
protected function addValue($key, $value, $duration)
{
return zend_shm_cache_fetch($key) === null ? $this->setValue($key, $value, $expire) : false;
return zend_shm_cache_fetch($key) === null ? $this->setValue($key, $value, $duration) : false;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment