@ -47,7 +47,7 @@
* @package system.caching
* @package system.caching
* @since 1.0
* @since 1.0
*/
*/
abstract class CCache extends CApplicationComponent implements ICache, ArrayAccess
abstract class Cache extends ApplicationComponent implements \ ArrayAccess
{
{
/**
/**
* @var string a string prefixed to every cache key so that it is unique. Defaults to null which means
* @var string a string prefixed to every cache key so that it is unique. Defaults to null which means
@ -64,8 +64,8 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
* cache keys, otherwise there might be unexpected behavior.
* cache keys, otherwise there might be unexpected behavior.
* @since 1.1.11
* @since 1.1.11
**/
**/
public $hashKey=true;
public $hashKey = true;
/**
/**
* @var boolean whether to automatically serialize/unserialize the cache values. Defaults to true. Setting this property to false makes sure the cache
* @var boolean whether to automatically serialize/unserialize the cache values. Defaults to true. Setting this property to false makes sure the cache
* value will not be tampered when calling the methods {@link set()} and {@link add()}. This is useful in case you want to store data which simply
* value will not be tampered when calling the methods {@link set()} and {@link add()}. This is useful in case you want to store data which simply
@ -76,7 +76,7 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
* configure a dedicated cache component for the sole purpose of storing raw unserialized data, the main cache component should always support serialization.
* configure a dedicated cache component for the sole purpose of storing raw unserialized data, the main cache component should always support serialization.
* @since 1.1.11
* @since 1.1.11
**/
**/
public $autoSerialize=true;
public $autoSerialize = true;
/**
/**
* @var boolean wether to make use of the {@link http://pecl.php.net/package/igbinary igbinary} serializer for cache entry serialization. Defaults to false.
* @var boolean wether to make use of the {@link http://pecl.php.net/package/igbinary igbinary} serializer for cache entry serialization. Defaults to false.
@ -84,8 +84,8 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
* serializer. Since the two serialization formats are incompatible, caches should be purged before switching this on to prevent errors.
* serializer. Since the two serialization formats are incompatible, caches should be purged before switching this on to prevent errors.
* @since 1.1.11
* @since 1.1.11
*/
*/
public $useIgbinarySerializer=false;
public $useIgbinarySerializer = false;
/**
/**
* Initializes the application component.
* Initializes the application component.
* This method overrides the parent implementation by setting default cache key prefix.
* This method overrides the parent implementation by setting default cache key prefix.
@ -93,9 +93,10 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
public function init()
public function init()
{
{
parent::init();
parent::init();
if($this->keyPrefix===null)
if ($this->keyPrefix === null) {
$this->keyPrefix=Yii::app()->getId();
$this->keyPrefix = Yii::app()->getId();
$this->useIgbinarySerializer=$this->useIgbinarySerializer&&extension_loaded('igbinary');
}
$this->useIgbinarySerializer = $this->useIgbinarySerializer & & extension_loaded('igbinary');
}
}
/**
/**
@ -104,7 +105,7 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
protected function generateUniqueKey($key)
protected function generateUniqueKey($key)
{
{
return $this->hashKey ? md5($this->keyPrefix.$key) : $this->keyPrefix.$key;
return $this->hashKey ? md5($this->keyPrefix . $key) : $this->keyPrefix . $key;
}
}
/**
/**
@ -114,12 +115,10 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
public function get($id)
public function get($id)
{
{
if(($value=$this->getValue($this->generateUniqueKey($id)))!==false)
if (($value = $this->getValue($this->generateUniqueKey($id))) !== false) {
{
$data = $this->autoSerialize ? $this->unserializeValue($value) : $value;
$data=$this->autoSerialize ? $this->unserializeValue($value) : $value;
if (!$this->autoSerialize || (is_array($data) & & (!($data[1] instanceof ICacheDependency) || !$data[1]->getHasChanged()))) {
if(!$this->autoSerialize || (is_array($data) & & (!($data[1] instanceof ICacheDependency) || !$data[1]->getHasChanged())))
Yii::trace('Serving "' . $id . '" from cache', 'system.caching.' . get_class($this));
{
Yii::trace('Serving "'.$id.'" from cache','system.caching.'.get_class($this));
return $this->autoSerialize ? $data[0] : $data;
return $this->autoSerialize ? $data[0] : $data;
}
}
}
}
@ -138,23 +137,21 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
public function mget($ids)
public function mget($ids)
{
{
$uniqueIDs=array();
$uniqueIDs = array();
$results=array();
$results = array();
foreach($ids as $id)
foreach ($ids as $id) {
{
$uniqueIDs[$id] = $this->generateUniqueKey($id);
$uniqueIDs[$id]=$this->generateUniqueKey($id);
$results[$id] = false;
$results[$id]=false;
}
}
$values=$this->getValues($uniqueIDs);
$values = $this->getValues($uniqueIDs);
foreach($uniqueIDs as $id=>$uniqueID)
foreach ($uniqueIDs as $id => $uniqueID) {
{
if (!isset($values[$uniqueID])) {
if(!isset($values[$uniqueID]))
continue;
continue;
$data=$this->autoSerialize ? $this->unserializeValue($values[$uniqueID]) : $values[$uniqueID];
}
if(!$this->autoSerialize || (is_array($data) & & (!($data[1] instanceof ICacheDependency) || !$data[1]->getHasChanged())))
$data = $this->autoSerialize ? $this->unserializeValue($values[$uniqueID]) : $values[$uniqueID];
{
if (!$this->autoSerialize || (is_array($data) & & (!($data[1] instanceof ICacheDependency) || !$data[1]->getHasChanged()))) {
Yii::trace('Serving "'.$id.'" from cache','system.caching.'.get_class($this));
Yii::trace('Serving "' . $id . '" from cache', 'system.caching.' . get_class($this));
$results[$id]=$this->autoSerialize ? $data[0] : $data;
$results[$id] = $this->autoSerialize ? $data[0] : $data;
}
}
}
}
return $results;
return $results;
@ -171,13 +168,14 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
* @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
* @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
* @return boolean true if the value is successfully stored into cache, false otherwise
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
*/
public function set($id,$value,$expire=0,$dependency=null)
public function set($id, $value, $expire = 0, $dependency = null)
{
{
Yii::trace('Saving "'.$id.'" to cache','system.caching.'.get_class($this));
Yii::trace('Saving "' . $id . '" to cache', 'system.caching.' . get_class($this));
if($dependency!==null & & $this->autoSerialize)
if ($dependency !== null & & $this->autoSerialize) {
$dependency->evaluateDependency();
$dependency->evaluateDependency();
$data=$this->autoSerialize ? $this->serializeValue(array($value,$dependency)) : $value;
}
return $this->setValue($this->generateUniqueKey($id),$data,$expire);
$data = $this->autoSerialize ? $this->serializeValue(array($value, $dependency)) : $value;
return $this->setValue($this->generateUniqueKey($id), $data, $expire);
}
}
/**
/**
@ -189,13 +187,14 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
* @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
* @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
* @return boolean true if the value is successfully stored into cache, false otherwise
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
*/
public function add($id,$value,$expire=0,$dependency=null)
public function add($id, $value, $expire = 0, $dependency = null)
{
{
Yii::trace('Adding "'.$id.'" to cache','system.caching.'.get_class($this));
Yii::trace('Adding "' . $id . '" to cache', 'system.caching.' . get_class($this));
if($dependency!==null & & $this->autoSerialize)
if ($dependency !== null & & $this->autoSerialize) {
$dependency->evaluateDependency();
$dependency->evaluateDependency();
$data=$this->autoSerialize ? $this->serializeValue(array($value,$dependency)) : $value;
}
return $this->addValue($this->generateUniqueKey($id),$data,$expire);
$data = $this->autoSerialize ? $this->serializeValue(array($value, $dependency)) : $value;
return $this->addValue($this->generateUniqueKey($id), $data, $expire);
}
}
/**
/**
@ -205,7 +204,7 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
public function delete($id)
public function delete($id)
{
{
Yii::trace('Deleting "'.$id.'" from cache','system.caching.'.get_class($this));
Yii::trace('Deleting "' . $id . '" from cache', 'system.caching.' . get_class($this));
return $this->deleteValue($this->generateUniqueKey($id));
return $this->deleteValue($this->generateUniqueKey($id));
}
}
@ -216,7 +215,7 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
public function flush()
public function flush()
{
{
Yii::trace('Flushing cache','system.caching.'.get_class($this));
Yii::trace('Flushing cache', 'system.caching.' . get_class($this));
return $this->flushValues();
return $this->flushValues();
}
}
@ -232,8 +231,8 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
protected function getValue($key)
protected function getValue($key)
{
{
throw new CException(Yii::t('yii','{className} does not support get() functionality.',
throw new CException(Yii::t('yii', '{className} does not support get() functionality.',
array('{className}'=>get_class($this))));
array('{className}' => get_class($this))));
}
}
/**
/**
@ -247,9 +246,10 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
protected function getValues($keys)
protected function getValues($keys)
{
{
$results=array();
$results = array();
foreach($keys as $key)
foreach ($keys as $key) {
$results[$key]=$this->getValue($key);
$results[$key] = $this->getValue($key);
}
return $results;
return $results;
}
}
@ -266,10 +266,10 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
* @return boolean true if the value is successfully stored into cache, false otherwise
* @return boolean true if the value is successfully stored into cache, false otherwise
* @throws CException if this method is not overridden by child classes
* @throws CException if this method is not overridden by child classes
*/
*/
protected function setValue($key,$value,$expire)
protected function setValue($key, $value, $expire)
{
{
throw new CException(Yii::t('yii','{className} does not support set() functionality.',
throw new CException(Yii::t('yii', '{className} does not support set() functionality.',
array('{className}'=>get_class($this))));
array('{className}' => get_class($this))));
}
}
/**
/**
@ -285,10 +285,10 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
* @return boolean true if the value is successfully stored into cache, false otherwise
* @return boolean true if the value is successfully stored into cache, false otherwise
* @throws CException if this method is not overridden by child classes
* @throws CException if this method is not overridden by child classes
*/
*/
protected function addValue($key,$value,$expire)
protected function addValue($key, $value, $expire)
{
{
throw new CException(Yii::t('yii','{className} does not support add() functionality.',
throw new CException(Yii::t('yii', '{className} does not support add() functionality.',
array('{className}'=>get_class($this))));
array('{className}' => get_class($this))));
}
}
/**
/**
@ -300,8 +300,8 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
protected function deleteValue($key)
protected function deleteValue($key)
{
{
throw new CException(Yii::t('yii','{className} does not support delete() functionality.',
throw new CException(Yii::t('yii', '{className} does not support delete() functionality.',
array('{className}'=>get_class($this))));
array('{className}' => get_class($this))));
}
}
/**
/**
@ -313,8 +313,8 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
protected function flushValues()
protected function flushValues()
{
{
throw new CException(Yii::t('yii','{className} does not support flushValues() functionality.',
throw new CException(Yii::t('yii', '{className} does not support flushValues() functionality.',
array('{className}'=>get_class($this))));
array('{className}' => get_class($this))));
}
}
/**
/**
@ -329,11 +329,12 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
**/
**/
protected function serializeValue($value)
protected function serializeValue($value)
{
{
if($this->useIgbinarySerializer)
if ($this->useIgbinarySerializer) {
return igbinary_serialize($value);
return igbinary_serialize($value);
}
return serialize($value);
return serialize($value);
}
}
/**
/**
* Unserializes the value after it was retrieved from the actual cache backend.
* Unserializes the value after it was retrieved from the actual cache backend.
* This method will be called if {@link autoSerialize} is set to true. Child classes may override this method to change
* This method will be called if {@link autoSerialize} is set to true. Child classes may override this method to change
@ -346,8 +347,9 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
**/
**/
protected function unserializeValue($value)
protected function unserializeValue($value)
{
{
if($this->useIgbinarySerializer)
if ($this->useIgbinarySerializer) {
return igbinary_unserialize($value);
return igbinary_unserialize($value);
}
return unserialize($value);
return unserialize($value);
}
}
@ -359,7 +361,7 @@ abstract class CCache extends CApplicationComponent implements ICache, ArrayAcce
*/
*/
public function offsetExists($id)
public function offsetExists($id)
{
{
return $this->get($id)!==false;
return $this->get($id) !== false;
}
}
/**
/**