CFileCache

优质
小牛编辑
124浏览
2023-12-01
所有包 | 属性 | 方法
system.caching
继承class CFileCache » CCache » CApplicationComponent » CComponent
实现ArrayAccess, ICache, IApplicationComponent
版本$Id: CFileCache.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/caching/CFileCache.php
CFileCache提供了一个基于文件的缓存机制。

CFileCache将每个缓存数据存储在独立的文件里, 文件位于cachePath目录,默认是protected/runtime/cache CFileCache会自动执行垃圾收集以删除过期缓存文件。

查看CCache操作手册以了解CFileCache支持的常用缓存操作。

公共属性

隐藏继承属性

属性类型描述定义在
behaviorsarray这个应用组件附加的行为。 这此行为将在应用组件调用init时附加在应用组件上。 请参照CModel::behaviors如何指定此属性值。CApplicationComponent
cacheFileSuffixstring缓存文件后缀,默认是.CFileCache
cachePathstring存储缓存文件的目录。默认是null,即使用 protected/runtime/cache目录。CFileCache
directoryLevelinteger存储缓存文件的子目录级别。默认是0, 即不使用子目录。若系统的缓存文件数目巨大(1万以上), 你可能需要将该值设置为1或者2以避免文件系统超过负担。 这个属性的值应不超过16(建议小于3)。CFileCache
gCProbabilityinteger缓存一块数据时,会进行垃圾 收集的概率(百万分之一计)。默认为100,意味着0.CFileCache
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
gc()删除过期缓存文件。CFileCache
get()从缓存中检索一个匹配指定键名的值。CCache
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getGCProbability()返回缓存一块数据时,会进行垃圾 收集的概率(百万分之一计)。默认为100,意味着0.01%的概率。CFileCache
getIsInitialized()检查应用组件是否已经初始化。CApplicationComponent
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
init()初始化这个应用程序组件。CFileCache
mget()从缓存中检索出多个匹配指定键名的值。CCache
offsetExists()返回指定键名的缓存条目是否存在。CCache
offsetGet()从缓存中检索一个匹配指定键名的值。CCache
offsetSet()往缓存中存储一个用键名区分的值。CCache
offsetUnset()从缓存中删除指定键名对应的值CCache
raiseEvent()发起一个事件。CComponent
set()根据一个用以甄别的键名往缓存中存储一个值。CCache
setGCProbability()设置缓存一块数据时,会进行垃圾 收集的概率(百万分之一计)。默认为100,意味着0.01%的概率。 数值必须在0到1000000之间。0意味着不会执行垃圾收集。CFileCache

受保护方法

隐藏继承方法

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

属性详细

cacheFileSuffix 属性 public string $cacheFileSuffix;

缓存文件后缀,默认是.bin。

cachePath 属性 public string $cachePath;

存储缓存文件的目录。默认是null,即使用 protected/runtime/cache目录。

directoryLevel 属性 public integer $directoryLevel;

存储缓存文件的子目录级别。默认是0, 即不使用子目录。若系统的缓存文件数目巨大(1万以上), 你可能需要将该值设置为1或者2以避免文件系统超过负担。 这个属性的值应不超过16(建议小于3)。

gCProbability 属性 public integer getGCProbability()
public void setGCProbability(integer $value)

缓存一块数据时,会进行垃圾 收集的概率(百万分之一计)。默认为100,意味着0.01%的概率。

方法详细

addValue() 方法
protected boolean addValue(string $key, string $value, integer $expire)
$keystring用以甄别缓存值的键名
$valuestring要缓存的值
$expireinteger以秒为单位的数值,表示缓存的过期时间。为0则永不过期。
{return}boolean成功存储到缓存中则返回true,否则返回false
源码: framework/caching/CFileCache.php#158 (显示) protectedfunctionaddValue($key,$value,$expire)
{
$cacheFile=$this->getCacheFile($key);
if(@filemtime($cacheFile)>time())
returnfalse;
return$this->setValue($key,$value,$expire);
}

仅仅在甄别缓存值的键名不存在的情况下,往缓存中存储值。 这是在父类中定义的方法的具体实现。

deleteValue() 方法
protected boolean deleteValue(string $key)
$keystring要删除值的键名
{return}boolean如果删除期间没有发生错误
源码: framework/caching/CFileCache.php#172 (显示) protectedfunctiondeleteValue($key)
{
$cacheFile=$this->getCacheFile($key);
return@unlink($cacheFile);
}

从缓存中删除指定键名对应的值 这是在父类中定义的方法的具体实现。

flushValues() 方法 (可用自 v1.1.5)
protected boolean flushValues()
{return}boolean如果清空操作成功执行。
源码: framework/caching/CFileCache.php#94 (显示) protectedfunctionflushValues()
{
$this->gc(false);
returntrue;
}

删除所有缓存值。 这是在父类中定义的方法的具体实现。

gc() 方法
public void gc(boolean $expiredOnly=true, string $path=NULL)
$expiredOnlyboolean是否只删除过期缓存文件。若为false,则所有在目录cachePath下的文件会被删除。
$pathstring要删除的路径。若为null,它会被设成cachePath。
源码: framework/caching/CFileCache.php#204 (显示) publicfunctiongc($expiredOnly=true,$path=null)
{
if($path===null)
$path=$this->cachePath;
if(($handle=opendir($path))===false)
return;
while(($file=readdir($handle))!==false)
{
if($file[0]==='.')
continue;
$fullPath=$path.DIRECTORY_SEPARATOR.$file;
if(is_dir($fullPath))
$this->gc($expiredOnly,$fullPath);
elseif($expiredOnly&&@filemtime($fullPath)<time()||!$expiredOnly)
@unlink($fullPath);
}
closedir($handle);
}

删除过期缓存文件。

getCacheFile() 方法
protected string getCacheFile(string $key)
$keystring缓存键
{return}string缓存文件路径
源码: framework/caching/CFileCache.php#183 (显示) protectedfunctiongetCacheFile($key)
{
if($this->directoryLevel>0)
{
$base=$this->cachePath;
for($i=0;$i<$this->directoryLevel;++$i)
{
if(($prefix=substr($key,$i+$i,2))!==false)
$base.=DIRECTORY_SEPARATOR.$prefix;
}
return$base.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
}
else
return$this->cachePath.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
}

根据键名返回缓存文件路径

getGCProbability() 方法
public integer getGCProbability()
{return}integer缓存一块数据时,会进行垃圾 收集的概率(百万分之一计)。默认为100,意味着0.01%的概率。
源码: framework/caching/CFileCache.php#68 (显示) publicfunctiongetGCProbability()
{
return$this->_gcProbability;
}
getValue() 方法
protected string getValue(string $key)
$keystring用以甄别缓存值的唯一键名
{return}string缓存中存储的值,如果该值不存在或者已过期则返回false。
源码: framework/caching/CFileCache.php#106 (显示) protectedfunctiongetValue($key)
{
$cacheFile=$this->getCacheFile($key);
if(($time=@filemtime($cacheFile))>time())
return@file_get_contents($cacheFile);
elseif($time>0)
@unlink($cacheFile);
returnfalse;
}

从缓存中检索一个匹配指定键名的值。 这是在父类中定义的方法的具体实现。

init() 方法
public void init()
源码: framework/caching/CFileCache.php#55 (显示) publicfunctioninit()
{
parent::init();
if($this->cachePath===null)
$this->cachePath=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'cache';
if(!is_dir($this->cachePath))
mkdir($this->cachePath,0777,true);
}

初始化这个应用程序组件。 该方法在接口IApplicationComponent中有要求。 它会检查memcache是否可用。

setGCProbability() 方法
public void setGCProbability(integer $value)
$valueinteger缓存一块数据时,会进行垃圾 收集的概率(百万分之一计)。默认为100,意味着0.01%的概率。 数值必须在0到1000000之间。0意味着不会执行垃圾收集。
源码: framework/caching/CFileCache.php#78 (显示) publicfunctionsetGCProbability($value)
{
$value=(int)$value;
if($value<0)
$value=0;
if($value>1000000)
$value=1000000;
$this->_gcProbability=$value;
}
setValue() 方法
protected boolean setValue(string $key, string $value, integer $expire)
$keystring用以甄别缓存值的键名
$valuestring要缓存的值
$expireinteger以秒为单位的数值,表示缓存的过期时间。为0则永不过期。
{return}boolean成功存储到缓存中则返回true,否则返回false
源码: framework/caching/CFileCache.php#125 (显示) protectedfunctionsetValue($key,$value,$expire)
{
if(!$this->_gced&&mt_rand(0,1000000)<$this->_gcProbability)
{
$this->gc();
$this->_gced=true;
}

if($expire<=0)
$expire=31536000;//1year
$expire+=time();

$cacheFile=$this->getCacheFile($key);
if($this->directoryLevel>0)
@mkdir(dirname($cacheFile),0777,true);
if(@file_put_contents($cacheFile,$value,LOCK_EX)!==false)
{
@chmod($cacheFile,0777);
return@touch($cacheFile,$expire);
}
else
returnfalse;
}

往缓存中存储一个用键名区分的值。 这是在父类中定义的方法的具体实现。