|
|
@ -9,8 +9,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace yii\caching; |
|
|
|
namespace yii\caching; |
|
|
|
|
|
|
|
|
|
|
|
use yii\base\ApplicationComponent; |
|
|
|
use yii\base\Component; |
|
|
|
use yii\base\InvalidCallException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Cache is the base class for cache classes supporting different cache storage implementation. |
|
|
|
* Cache is the base class for cache classes supporting different cache storage implementation. |
|
|
@ -21,13 +20,16 @@ use yii\base\InvalidCallException; |
|
|
|
* can also be specified when calling [[set()]]. If the data item expires or the dependency |
|
|
|
* can also be specified when calling [[set()]]. If the data item expires or the dependency |
|
|
|
* changes at the time of calling [[get()]], the cache will return no data. |
|
|
|
* changes at the time of calling [[get()]], the cache will return no data. |
|
|
|
* |
|
|
|
* |
|
|
|
* Derived classes should implement the following methods: |
|
|
|
* A typical usage pattern of cache is like the following: |
|
|
|
* |
|
|
|
* |
|
|
|
* - [[getValue]]: retrieve the value with a key (if any) from cache |
|
|
|
* ~~~ |
|
|
|
* - [[setValue]]: store the value with a key into cache |
|
|
|
* $key = 'demo'; |
|
|
|
* - [[addValue]]: store the value only if the cache does not have this key before |
|
|
|
* $data = $cache->get($key); |
|
|
|
* - [[deleteValue]]: delete the value with the specified key from cache |
|
|
|
* if ($data === false) { |
|
|
|
* - [[flushValues]]: delete all values from cache |
|
|
|
* // ...generate $data here... |
|
|
|
|
|
|
|
* $cache->set($key, $data, $expire, $dependency); |
|
|
|
|
|
|
|
* } |
|
|
|
|
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* Because Cache implements the ArrayAccess interface, it can be used like an array. For example, |
|
|
|
* Because Cache implements the ArrayAccess interface, it can be used like an array. For example, |
|
|
|
* |
|
|
|
* |
|
|
@ -36,10 +38,19 @@ use yii\base\InvalidCallException; |
|
|
|
* echo $cache['foo']; |
|
|
|
* echo $cache['foo']; |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* Derived classes should implement the following methods: |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* - [[getValue()]]: retrieve the value with a key (if any) from cache |
|
|
|
|
|
|
|
* - [[setValue()]]: store the value with a key into cache |
|
|
|
|
|
|
|
* - [[addValue()]]: store the value only if the cache does not have this key before |
|
|
|
|
|
|
|
* - [[deleteValue()]]: delete the value with the specified key from cache |
|
|
|
|
|
|
|
* - [[flushValues()]]: delete all values from cache |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @since 2.0 |
|
|
|
* @since 2.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
abstract class Cache extends Component implements \ArrayAccess |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string a string prefixed to every cache key so that it is unique. Defaults to null, meaning using |
|
|
|
* @var string a string prefixed to every cache key so that it is unique. Defaults to null, meaning using |
|
|
@ -59,64 +70,45 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $serializer; |
|
|
|
public $serializer; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Initializes the application component. |
|
|
|
|
|
|
|
* This method overrides the parent implementation by setting default cache key prefix. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function init() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
parent::init(); |
|
|
|
|
|
|
|
if ($this->keyPrefix === null) { |
|
|
|
|
|
|
|
$this->keyPrefix = \Yii::$application->id; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Generates a cache key from one or multiple parameters. |
|
|
|
* Builds a normalized cache key from one or multiple parameters. |
|
|
|
* The cache key generated is safe to be used to access cache via methods such as [[get()]], [[set()]]. |
|
|
|
* |
|
|
|
* For example: |
|
|
|
* The generated key contains letters and digits only, and its length is no more than 32. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* If only one parameter is given and it is already a normalized key, then |
|
|
|
|
|
|
|
* it will be returned back without change. Otherwise, a normalized key |
|
|
|
|
|
|
|
* is generated by serializing all given parameters and applying MD5 hashing. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* The following example builds a cache key using three parameters: |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* $key = $cache->buildKey($className, $method, $id); |
|
|
|
* $key = $cache->buildKey($className, $method, $id); |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $id the |
|
|
|
* @param string $key the first parameter |
|
|
|
* @return string the cache key |
|
|
|
* @return string the generated cache key |
|
|
|
* @throws InvalidCallException if the method receives no parameter |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function generateKey($id) |
|
|
|
public function buildKey($key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$n = func_num_args(); |
|
|
|
if (func_num_args() === 1 && ctype_alnum($key) && strlen($key) <= 32) { |
|
|
|
if ($n === 1 && is_string($id) && ctype_alnum($id) && strlen($id) <= 32) { |
|
|
|
return (string)$key; |
|
|
|
return $this->keyPrefix . $id; |
|
|
|
|
|
|
|
} elseif ($n < 1) { |
|
|
|
|
|
|
|
throw new InvalidCallException(__METHOD__ . ' requires at least one parameter.'); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$params = func_get_args(); |
|
|
|
$params = func_get_args(); |
|
|
|
return $this->keyPrefix . md5(serialize($params)); |
|
|
|
return md5(serialize($params)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Generates a normalized key from a given key. |
|
|
|
|
|
|
|
* The normalized key is obtained by hashing the given key via MD5 and then prefixing it with [[keyPrefix]]. |
|
|
|
|
|
|
|
* @param string $key a key identifying a value to be cached |
|
|
|
|
|
|
|
* @return string a key generated from the provided key which ensures the uniqueness across applications |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected function generateKey($key) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->keyPrefix . md5($key); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Retrieves a value from cache with a specified key. |
|
|
|
* Retrieves a value from cache with a specified key. |
|
|
|
* @param string $id a key identifying the cached value |
|
|
|
* @param string $key a key identifying the cached value |
|
|
|
* @return mixed the value stored in cache, false if the value is not in the cache, expired, |
|
|
|
* @return mixed the value stored in cache, false if the value is not in the cache, expired, |
|
|
|
* or the dependency associated with the cached data has changed. |
|
|
|
* or the dependency associated with the cached data has changed. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function get($id) |
|
|
|
public function get($key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$value = $this->getValue($this->generateKey($id)); |
|
|
|
$key = $this->keyPrefix . $this->buildKey($key); |
|
|
|
|
|
|
|
$value = $this->getValue($key); |
|
|
|
if ($value === false || $this->serializer === false) { |
|
|
|
if ($value === false || $this->serializer === false) { |
|
|
|
return $value; |
|
|
|
return $value; |
|
|
|
} elseif ($this->serializer === null) { |
|
|
|
} elseif ($this->serializer === null) { |
|
|
@ -124,7 +116,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$value = call_user_func($this->serializer[1], $value); |
|
|
|
$value = call_user_func($this->serializer[1], $value); |
|
|
|
} |
|
|
|
} |
|
|
|
if (is_array($value) && ($value[1] instanceof Dependency) || !$value[1]->getHasChanged()) { |
|
|
|
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged())) { |
|
|
|
return $value[0]; |
|
|
|
return $value[0]; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
return false; |
|
|
@ -136,30 +128,30 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
* Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time, |
|
|
|
* Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time, |
|
|
|
* which may improve the performance. In case a cache does not support this feature natively, |
|
|
|
* which may improve the performance. In case a cache does not support this feature natively, |
|
|
|
* this method will try to simulate it. |
|
|
|
* this method will try to simulate it. |
|
|
|
* @param array $ids list of keys identifying the cached values |
|
|
|
* @param array $keys list of keys identifying the cached values |
|
|
|
* @return array list of cached values corresponding to the specified keys. The array |
|
|
|
* @return array list of cached values corresponding to the specified keys. The array |
|
|
|
* is returned in terms of (key,value) pairs. |
|
|
|
* is returned in terms of (key,value) pairs. |
|
|
|
* If a value is not cached or expired, the corresponding array value will be false. |
|
|
|
* If a value is not cached or expired, the corresponding array value will be false. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function mget($ids) |
|
|
|
public function mget($keys) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$uids = array(); |
|
|
|
$keyMap = array(); |
|
|
|
foreach ($ids as $id) { |
|
|
|
foreach ($keys as $key) { |
|
|
|
$uids[$id] = $this->generateKey($id); |
|
|
|
$keyMap[$key] = $this->keyPrefix . $this->buildKey($key); |
|
|
|
} |
|
|
|
} |
|
|
|
$values = $this->getValues($uids); |
|
|
|
$values = $this->getValues(array_values($keyMap)); |
|
|
|
$results = array(); |
|
|
|
$results = array(); |
|
|
|
if ($this->serializer === false) { |
|
|
|
foreach ($keyMap as $key => $newKey) { |
|
|
|
foreach ($uids as $id => $uid) { |
|
|
|
$results[$key] = false; |
|
|
|
$results[$id] = isset($values[$uid]) ? $values[$uid] : false; |
|
|
|
if (isset($values[$newKey])) { |
|
|
|
} |
|
|
|
if ($this->serializer === false) { |
|
|
|
} else { |
|
|
|
$results[$key] = $values[$newKey]; |
|
|
|
foreach ($uids as $id => $uid) { |
|
|
|
} else { |
|
|
|
$results[$id] = false; |
|
|
|
$value = $this->serializer === null ? unserialize($values[$newKey]) |
|
|
|
if (isset($values[$uid])) { |
|
|
|
: call_user_func($this->serializer[1], $values[$newKey]); |
|
|
|
$value = $this->serializer === null ? unserialize($values[$uid]) : call_user_func($this->serializer[1], $values[$uid]); |
|
|
|
|
|
|
|
if (is_array($value) && (!($value[1] instanceof Dependency) || !$value[1]->getHasChanged())) { |
|
|
|
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged())) { |
|
|
|
$results[$id] = $value[0]; |
|
|
|
$results[$key] = $value[0]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -172,14 +164,15 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
* If the cache already contains such a key, the existing value and |
|
|
|
* If the cache already contains such a key, the existing value and |
|
|
|
* expiration time will be replaced with the new ones, respectively. |
|
|
|
* expiration time will be replaced with the new ones, respectively. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $id the key identifying the value to be cached |
|
|
|
* @param string $key the key identifying the value to be cached |
|
|
|
* @param mixed $value the value to be cached |
|
|
|
* @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 $expire 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, |
|
|
|
* @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()]]. |
|
|
|
* 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 |
|
|
|
* @return boolean whether the value is successfully stored into cache |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function set($id, $value, $expire = 0, $dependency = null) |
|
|
|
public function set($key, $value, $expire = 0, $dependency = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($dependency !== null && $this->serializer !== false) { |
|
|
|
if ($dependency !== null && $this->serializer !== false) { |
|
|
|
$dependency->evaluateDependency(); |
|
|
|
$dependency->evaluateDependency(); |
|
|
@ -189,20 +182,22 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
} elseif ($this->serializer !== false) { |
|
|
|
} elseif ($this->serializer !== false) { |
|
|
|
$value = call_user_func($this->serializer[0], array($value, $dependency)); |
|
|
|
$value = call_user_func($this->serializer[0], array($value, $dependency)); |
|
|
|
} |
|
|
|
} |
|
|
|
return $this->setValue($this->generateKey($id), $value, $expire); |
|
|
|
$key = $this->keyPrefix . $this->buildKey($key); |
|
|
|
|
|
|
|
return $this->setValue($key, $value, $expire); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Stores a value identified by a key into cache if the cache does not contain this key. |
|
|
|
* Stores a value identified by a key into cache if the cache does not contain this key. |
|
|
|
* Nothing will be done if the cache already contains the key. |
|
|
|
* Nothing will be done if the cache already contains the key. |
|
|
|
* @param string $id the key identifying the value to be cached |
|
|
|
* @param string $key the key identifying the value to be cached |
|
|
|
* @param mixed $value the value to be cached |
|
|
|
* @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 $expire 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, |
|
|
|
* @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()]]. |
|
|
|
* 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 |
|
|
|
* @return boolean whether the value is successfully stored into cache |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function add($id, $value, $expire = 0, $dependency = null) |
|
|
|
public function add($key, $value, $expire = 0, $dependency = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($dependency !== null && $this->serializer !== false) { |
|
|
|
if ($dependency !== null && $this->serializer !== false) { |
|
|
|
$dependency->evaluateDependency(); |
|
|
|
$dependency->evaluateDependency(); |
|
|
@ -212,17 +207,19 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
} elseif ($this->serializer !== false) { |
|
|
|
} elseif ($this->serializer !== false) { |
|
|
|
$value = call_user_func($this->serializer[0], array($value, $dependency)); |
|
|
|
$value = call_user_func($this->serializer[0], array($value, $dependency)); |
|
|
|
} |
|
|
|
} |
|
|
|
return $this->addValue($this->generateKey($id), $value, $expire); |
|
|
|
$key = $this->keyPrefix . $this->buildKey($key); |
|
|
|
|
|
|
|
return $this->addValue($key, $value, $expire); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Deletes a value with the specified key from cache |
|
|
|
* Deletes a value with the specified key from cache |
|
|
|
* @param string $id the key of the value to be deleted |
|
|
|
* @param string $key the key of the value to be deleted |
|
|
|
* @return boolean if no error happens during deletion |
|
|
|
* @return boolean if no error happens during deletion |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function delete($id) |
|
|
|
public function delete($key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->deleteValue($this->generateKey($id)); |
|
|
|
$key = $this->keyPrefix . $this->buildKey($key); |
|
|
|
|
|
|
|
return $this->deleteValue($key); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -301,23 +298,23 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns whether there is a cache entry with a specified key. |
|
|
|
* Returns whether there is a cache entry with a specified key. |
|
|
|
* This method is required by the interface ArrayAccess. |
|
|
|
* This method is required by the interface ArrayAccess. |
|
|
|
* @param string $id a key identifying the cached value |
|
|
|
* @param string $key a key identifying the cached value |
|
|
|
* @return boolean |
|
|
|
* @return boolean |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function offsetExists($id) |
|
|
|
public function offsetExists($key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->get($id) !== false; |
|
|
|
return $this->get($key) !== false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Retrieves the value from cache with a specified key. |
|
|
|
* Retrieves the value from cache with a specified key. |
|
|
|
* This method is required by the interface ArrayAccess. |
|
|
|
* This method is required by the interface ArrayAccess. |
|
|
|
* @param string $id a key identifying the cached value |
|
|
|
* @param string $key a key identifying the cached value |
|
|
|
* @return mixed the value stored in cache, false if the value is not in the cache or expired. |
|
|
|
* @return mixed the value stored in cache, false if the value is not in the cache or expired. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function offsetGet($id) |
|
|
|
public function offsetGet($key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->get($id); |
|
|
|
return $this->get($key); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -325,21 +322,21 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess |
|
|
|
* If the cache already contains such a key, the existing value will be |
|
|
|
* If the cache already contains such a key, the existing value will be |
|
|
|
* replaced with the new ones. To add expiration and dependencies, use the [[set()]] method. |
|
|
|
* replaced with the new ones. To add expiration and dependencies, use the [[set()]] method. |
|
|
|
* This method is required by the interface ArrayAccess. |
|
|
|
* This method is required by the interface ArrayAccess. |
|
|
|
* @param string $id the key identifying the value to be cached |
|
|
|
* @param string $key the key identifying the value to be cached |
|
|
|
* @param mixed $value the value to be cached |
|
|
|
* @param mixed $value the value to be cached |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function offsetSet($id, $value) |
|
|
|
public function offsetSet($key, $value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->set($id, $value); |
|
|
|
$this->set($key, $value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Deletes the value with the specified key from cache |
|
|
|
* Deletes the value with the specified key from cache |
|
|
|
* This method is required by the interface ArrayAccess. |
|
|
|
* This method is required by the interface ArrayAccess. |
|
|
|
* @param string $id the key of the value to be deleted |
|
|
|
* @param string $key the key of the value to be deleted |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function offsetUnset($id) |
|
|
|
public function offsetUnset($key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->delete($id); |
|
|
|
$this->delete($key); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |