|
|
|
@ -95,16 +95,10 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
* then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key |
|
|
|
|
* is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]]. |
|
|
|
|
* |
|
|
|
|
* The following example builds a cache key using three parameters: |
|
|
|
|
* |
|
|
|
|
* ~~~ |
|
|
|
|
* $key = $cache->buildKey(array($className, $method, $id)); |
|
|
|
|
* ~~~ |
|
|
|
|
* |
|
|
|
|
* @param mixed $key the key to be normalized |
|
|
|
|
* @return string the generated cache key |
|
|
|
|
*/ |
|
|
|
|
public function buildKey($key) |
|
|
|
|
protected function buildKey($key) |
|
|
|
|
{ |
|
|
|
|
if (is_string($key)) { |
|
|
|
|
$key = ctype_alnum($key) && StringHelper::strlen($key) <= 32 ? $key : md5($key); |
|
|
|
@ -116,7 +110,8 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Retrieves a value from cache with a specified key. |
|
|
|
|
* @param string $key a key identifying the cached value |
|
|
|
|
* @param mixed $key a key identifying the cached value. This can be a simple string or |
|
|
|
|
* a complex data structure consisting of factors representing the key. |
|
|
|
|
* @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. |
|
|
|
|
*/ |
|
|
|
@ -131,7 +126,7 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
} else { |
|
|
|
|
$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($this))) { |
|
|
|
|
return $value[0]; |
|
|
|
|
} else { |
|
|
|
|
return false; |
|
|
|
@ -165,7 +160,7 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
$value = $this->serializer === null ? unserialize($values[$newKey]) |
|
|
|
|
: call_user_func($this->serializer[1], $values[$newKey]); |
|
|
|
|
|
|
|
|
|
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged())) { |
|
|
|
|
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged($this))) { |
|
|
|
|
$results[$key] = $value[0]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -179,7 +174,8 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
* If the cache already contains such a key, the existing value and |
|
|
|
|
* expiration time will be replaced with the new ones, respectively. |
|
|
|
|
* |
|
|
|
|
* @param string $key the key identifying the value to be cached |
|
|
|
|
* @param mixed $key a key identifying the value to be cached value. 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 Dependency $dependency dependency of the cached item. If the dependency changes, |
|
|
|
@ -190,7 +186,7 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
public function set($key, $value, $expire = 0, $dependency = null) |
|
|
|
|
{ |
|
|
|
|
if ($dependency !== null && $this->serializer !== false) { |
|
|
|
|
$dependency->evaluateDependency(); |
|
|
|
|
$dependency->evaluateDependency($this); |
|
|
|
|
} |
|
|
|
|
if ($this->serializer === null) { |
|
|
|
|
$value = serialize(array($value, $dependency)); |
|
|
|
@ -204,7 +200,8 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
/** |
|
|
|
|
* 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. |
|
|
|
|
* @param string $key the key identifying the value to be cached |
|
|
|
|
* @param mixed $key a key identifying the value to be cached value. 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 Dependency $dependency dependency of the cached item. If the dependency changes, |
|
|
|
@ -215,7 +212,7 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
public function add($key, $value, $expire = 0, $dependency = null) |
|
|
|
|
{ |
|
|
|
|
if ($dependency !== null && $this->serializer !== false) { |
|
|
|
|
$dependency->evaluateDependency(); |
|
|
|
|
$dependency->evaluateDependency($this); |
|
|
|
|
} |
|
|
|
|
if ($this->serializer === null) { |
|
|
|
|
$value = serialize(array($value, $dependency)); |
|
|
|
@ -228,7 +225,8 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Deletes a value with the specified key from cache |
|
|
|
|
* @param string $key the key of the value to be deleted |
|
|
|
|
* @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or |
|
|
|
|
* a complex data structure consisting of factors representing the key. |
|
|
|
|
* @return boolean if no error happens during deletion |
|
|
|
|
*/ |
|
|
|
|
public function delete($key) |
|
|
|
|