CCache

优质
小牛编辑
128浏览
2023-12-01
所有包 | 属性 | 方法
system.caching
继承abstract class CCache » CApplicationComponent » CComponent
实现IApplicationComponent, ICache, ArrayAccess
子类CApcCache, CDbCache, CEAcceleratorCache, CFileCache, CMemCache, CWinCache, CXCache, CZendDataCache
源自1.0
版本$Id: CCache.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/caching/CCache.php
CCache是用不同的缓存存储实现缓存类的基类。

可以通过调用set来往缓存中存储一个数据项,并在以后调用 get来检索。在这两个操作中,一个用来甄别数据项的键名是必须的。 在调用set时也可以指定过期时间和(或)依赖项。 如果过期时间或者依赖项改变了,则调用get不再 返回数据项。

注意,根据定义,缓存不保证值的存在,即使它 没有过期。缓存并不意味着是一个永久性存储。

CCache实现了接口ICache的如下方法:
  • get : 根据键名(如果有)从缓存中检索值
  • set : 根据键名往缓存中存储值
  • add : 仅当缓存中的没有该键名时往缓存中存储值
  • delete : 从缓存中删除指定键名的值
  • flush : 从缓存中删除所有值


子类必须实现如下方法:
  • getValue
  • setValue
  • addValue
  • deleteValue
  • flush(可选)


CCache同时继承了ArrayAccess使得可以像数组一样使用它。

公共属性

隐藏继承属性

属性类型描述定义在
behaviorsarray这个应用组件附加的行为。 这此行为将在应用组件调用init时附加在应用组件上。 请参照CModel::behaviors如何指定此属性值。CApplicationComponent
isInitializedboolean检查应用组件是否已经初始化。CApplicationComponent
keyPrefixstring加在每个缓存键名前的字符串以保证键名是唯一的。默认值是application ID。CCache

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
add()仅仅在甄别缓存值的键名不存在的情况下,往缓存中存储值。CCache
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
delete()从缓存中删除指定键名对应的值CCache
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
flush()删除所有缓存值。CCache
get()从缓存中检索一个匹配指定键名的值。CCache
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getIsInitialized()检查应用组件是否已经初始化。CApplicationComponent
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
init()初始化应用程序组件。CCache
mget()从缓存中检索出多个匹配指定键名的值。CCache
offsetExists()返回指定键名的缓存条目是否存在。CCache
offsetGet()从缓存中检索一个匹配指定键名的值。CCache
offsetSet()往缓存中存储一个用键名区分的值。CCache
offsetUnset()从缓存中删除指定键名对应的值CCache
raiseEvent()发起一个事件。CComponent
set()根据一个用以甄别的键名往缓存中存储一个值。CCache

受保护方法

隐藏继承方法

方法描述定义在
addValue()仅仅在甄别缓存值的键名不存在的情况下,往缓存中存储值。CCache
deleteValue()从缓存中删除指定键名对应的值CCache
flushValues()删除所有缓存值。CCache
generateUniqueKey()CCache
getValue()从缓存中检索一个匹配指定键名的值。CCache
getValues()从缓存中检索出多个匹配指定键名的值。CCache
setValue()往缓存中存储一个用键名区分的值。CCache

属性详细

keyPrefix 属性 public string $keyPrefix;

加在每个缓存键名前的字符串以保证键名是唯一的。默认值是application ID。

方法详细

add() 方法
public boolean add(string $id, mixed $value, integer $expire=0, ICacheDependency $dependency=NULL)
$idstring用以甄别缓存值的键名
$valuemixed要缓存的值
$expireinteger以秒为单位的数值,表示缓存的过期时间。为0则永不过期。
$dependencyICacheDependency缓存项的依赖项。若依赖项改变,缓存项会被标志为无效。
{return}boolean成功存储到缓存中则返回true,否则返回false
源码: framework/caching/CCache.php#159 (显示) publicfunctionadd($id,$value,$expire=0,$dependency=null)
{
Yii::trace('Adding"'.$id.'"tocache','system.caching.'.get_class($this));
if($dependency!==null)
$dependency->evaluateDependency();
$data=array($value,$dependency);
return$this->addValue($this->generateUniqueKey($id),serialize($data),$expire);
}

仅仅在甄别缓存值的键名不存在的情况下,往缓存中存储值。 若缓存中已有该键名则什么也不做。

addValue() 方法
protected boolean addValue(string $key, string $value, integer $expire)
$keystring用以甄别缓存值的键名
$valuestring要缓存的值
$expireinteger以秒为单位的数值,表示缓存的过期时间。为0则永不过期。
{return}boolean成功存储到缓存中则返回true,否则返回false
源码: framework/caching/CCache.php#255 (显示) protectedfunctionaddValue($key,$value,$expire)
{
thrownewCException(Yii::t('yii','{className}doesnotsupportadd()functionality.',
array('{className}'=>get_class($this))));
}

仅仅在甄别缓存值的键名不存在的情况下,往缓存中存储值。 该方法必须在子类中实现,以此往指 定缓存存储中存储数据。独立性和依 赖项已经在add()中处理了。 因此只需实现数据存储。

delete() 方法
public boolean delete(string $id)
$idstring要删除值的键名
{return}boolean如果删除期间没有发生错误
源码: framework/caching/CCache.php#173 (显示) publicfunctiondelete($id)
{
Yii::trace('Deleting"'.$id.'"fromcache','system.caching.'.get_class($this));
return$this->deleteValue($this->generateUniqueKey($id));
}

从缓存中删除指定键名对应的值

deleteValue() 方法
protected boolean deleteValue(string $key)
$keystring要删除值的键名
{return}boolean如果删除期间没有发生错误
源码: framework/caching/CCache.php#268 (显示) protectedfunctiondeleteValue($key)
{
thrownewCException(Yii::t('yii','{className}doesnotsupportdelete()functionality.',
array('{className}'=>get_class($this))));
}

从缓存中删除指定键名对应的值 该方法必须在子类中实现以从实际缓存存储中删除数据。

flush() 方法
public boolean flush()
{return}boolean如果清空操作成功执行。
源码: framework/caching/CCache.php#184 (显示) publicfunctionflush()
{
Yii::trace('Flushingcache','system.caching.'.get_class($this));
return$this->flushValues();
}

删除所有缓存值。 若缓存被多个应用程序共享,务必小心执行此操作。

flushValues() 方法 (可用自 v1.1.5)
protected boolean flushValues()
{return}boolean如果清空操作成功执行。
源码: framework/caching/CCache.php#281 (显示) protectedfunctionflushValues()
{
thrownewCException(Yii::t('yii','{className}doesnotsupportflushValues()functionality.',
array('{className}'=>get_class($this))));
}

删除所有缓存值。 子类可以实现该方法以实现清空操作。

generateUniqueKey() 方法
protected string generateUniqueKey(string $key)
$keystring用以甄别缓存值的键名
{return}string根据提供的键名生成的键名,该键名确保在整个应用程序里是唯一的。
源码: framework/caching/CCache.php#70 (显示) protectedfunctiongenerateUniqueKey($key)
{
returnmd5($this->keyPrefix.$key);
}
get() 方法
public mixed get(string $id)
$idstring用以甄别缓存值的键名
{return}mixed缓存中存储的值,若该值不在缓存中、已过期或依赖项改变时则返回false。
源码: framework/caching/CCache.php#80 (显示) publicfunctionget($id)
{
if(($value=$this->getValue($this->generateUniqueKey($id)))!==false)
{
$data=unserialize($value);
if(!is_array($data))
returnfalse;
if(!($data[1]instanceofICacheDependency)||!$data[1]->getHasChanged())
{
Yii::trace('Serving"'.$id.'"fromcache','system.caching.'.get_class($this));
return$data[0];
}
}
returnfalse;
}

从缓存中检索一个匹配指定键名的值。

getValue() 方法
protected string getValue(string $key)
$keystring用以甄别缓存值的唯一键名
{return}string缓存中存储的值,如果该值不存在或者已过期则返回false。
源码: framework/caching/CCache.php#200 (显示) protectedfunctiongetValue($key)
{
thrownewCException(Yii::t('yii','{className}doesnotsupportget()functionality.',
array('{className}'=>get_class($this))));
}

从缓存中检索一个匹配指定键名的值。 该方法必须被子类实现以从指定缓存存 储中检索数据。独立性和依赖项已经在 get()中处理了。因此只需要 实现数据检索。

getValues() 方法
protected array getValues(array $keys)
$keysarray用以甄别缓存值的键名列表
{return}array以$keys为键名的缓存值列表
源码: framework/caching/CCache.php#215 (显示) protectedfunctiongetValues($keys)
{
$results=array();
foreach($keysas$key)
$results[$key]=$this->getValue($key);
return$results;
}

从缓存中检索出多个匹配指定键名的值。 默认以多次调用getValue来依次 检索被缓存的值的方式实现。 若底层缓存存储支持多个获取(muiltiget), 该方法应该重写以利用其特性。

init() 方法
public void init()
源码: framework/caching/CCache.php#59 (显示) publicfunctioninit()
{
parent::init();
if($this->keyPrefix===null)
$this->keyPrefix=Yii::app()->getId();
}

初始化应用程序组件。 通过设置默认缓存键名前缀,该方法重写了父类方法的实现。

mget() 方法
public array mget(array $ids)
$idsarray用以甄别缓存值的键名列表
{return}array以指定键名列表对应的缓存值列表。 以(键名,值)对的形式返回数组。 若某值不在缓存中或已过期则数组中对应的值为false。
源码: framework/caching/CCache.php#106 (显示) publicfunctionmget($ids)
{
$uniqueIDs=array();
$results=array();
foreach($idsas$id)
{
$uniqueIDs[$id]=$this->generateUniqueKey($id);
$results[$id]=false;
}
$values=$this->getValues($uniqueIDs);
foreach($uniqueIDsas$id=>$uniqueID)
{
if(!isset($values[$uniqueID]))
continue;
$data=unserialize($values[$uniqueID]);
if(is_array($data)&&(!($data[1]instanceofICacheDependency)||!$data[1]->getHasChanged()))
{
Yii::trace('Serving"'.$id.'"fromcache','system.caching.'.get_class($this));
$results[$id]=$data[0];
}
}
return$results;
}

从缓存中检索出多个匹配指定键名的值。 某些缓存(诸如memcache,apc)允许一次性检索多个值, 这可以提高性能,因为它降低了通信成本。 在缓存本身不支持此功能的情况下,它会用这种方法模拟。

offsetExists() 方法
public boolean offsetExists(string $id)
$idstring用以甄别缓存值的键名
{return}boolean
源码: framework/caching/CCache.php#293 (显示) publicfunctionoffsetExists($id)
{
return$this->get($id)!==false;
}

返回指定键名的缓存条目是否存在。 该方法是接口ArrayAccess中要求的。

offsetGet() 方法
public mixed offsetGet(string $id)
$idstring用以甄别缓存值的键名
{return}mixed缓存中存储的值,若该值不在缓存中或者已过期则返回false。
源码: framework/caching/CCache.php#304 (显示) publicfunctionoffsetGet($id)
{
return$this->get($id);
}

从缓存中检索一个匹配指定键名的值。 该方法是接口ArrayAccess中要求的。

offsetSet() 方法
public void offsetSet(string $id, mixed $value)
$idstring用以甄别缓存值的键名
$valuemixed要缓存的值
源码: framework/caching/CCache.php#317 (显示) publicfunctionoffsetSet($id,$value)
{
$this->set($id,$value);
}

往缓存中存储一个用键名区分的值。 若缓存中已经包含该键名,则之前的缓存值 会被新的替换掉。使用set()方法来添加过期时间和依赖项。 该方法是接口ArrayAccess中要求的。

offsetUnset() 方法
public boolean offsetUnset(string $id)
$idstring要删除值的键名
{return}boolean如果删除期间没有发生错误
源码: framework/caching/CCache.php#328 (显示) publicfunctionoffsetUnset($id)
{
$this->delete($id);
}

从缓存中删除指定键名对应的值 该方法是接口ArrayAccess中要求的。

set() 方法
public boolean set(string $id, mixed $value, integer $expire=0, ICacheDependency $dependency=NULL)
$idstring用以甄别缓存值的键名
$valuemixed要缓存的值
$expireinteger以秒为单位的数值,表示缓存的过期时间。为0则永不过期。
$dependencyICacheDependency缓存项的依赖项。若依赖项改变,缓存项会被标志为无效。
{return}boolean成功存储到缓存中则返回true,否则返回false
源码: framework/caching/CCache.php#141 (显示) publicfunctionset($id,$value,$expire=0,$dependency=null)
{
Yii::trace('Saving"'.$id.'"tocache','system.caching.'.get_class($this));
if($dependency!==null)
$dependency->evaluateDependency();
$data=array($value,$dependency);
return$this->setValue($this->generateUniqueKey($id),serialize($data),$expire);
}

根据一个用以甄别的键名往缓存中存储一个值。 若缓存中已经包含该键名,则之前的缓存值 和过期时间会被新的替换掉。

setValue() 方法
protected boolean setValue(string $key, string $value, integer $expire)
$keystring用以甄别缓存值的键名
$valuestring要缓存的值
$expireinteger以秒为单位的数值,表示缓存的过期时间。为0则永不过期。
{return}boolean成功存储到缓存中则返回true,否则返回false
源码: framework/caching/CCache.php#236 (显示) protectedfunctionsetValue($key,$value,$expire)
{
thrownewCException(Yii::t('yii','{className}doesnotsupportset()functionality.',
array('{className}'=>get_class($this))));
}

往缓存中存储一个用键名区分的值。 该方法必须在子类中实现,以此往指 定缓存存储中存储数据。独立性和依 赖项已经在set()中处理了。 因此只需实现数据存储。