YII Framework框架教程之缓存用法详解
|
本文实例讲述了YII Framework框架缓存用法。分享给大家供大家参考,具体如下: 缓存的产生原因众所周知。于是YII作为一个高效,好用的框架,不能不支持缓存。所以YII对各种流行的缓存都提供了接口,你可以根据你的需要使用不同的缓存。 1.YII中的缓存介绍YII中的缓存是通过组件方式定义的,具体在如下目录
1 directory,17 files 官方原文解释如下:
CMemCache: 使用 PHP memcache 扩展. CApcCache: 使用 PHP APC 扩展. CXCache: 使用 PHP XCache 扩展。注意,这个是从 1.0.1 版本开始支持的。 CEAcceleratorCache: 使用 PHP EAccelerator 扩展. CDbCache: 使用一个数据表存储缓存数据。默认情况下,它将创建并使用在 runtime 目录下的一个 SQLite3 数据库。 你也可以通过设置其 connectionID 属性指定一个给它使用的数据库。 CZendDataCache: 使用 Zend Data Cache 作为后台缓存媒介。注意,这个是从 1.0.4 版本开始支持的。 CFileCache: 使用文件存储缓存数据。这个特别适合用于存储大块数据(例如页面)。注意,这个是从 1.0.6 版本开始支持的。 CDummyCache: 目前 dummy 缓存并不实现缓存功能。此组件的目的是用于简化那些需要检查缓存可用性的代码。 例如,在开发阶段或者服务器尚未支持实际的缓存功能,我们可以使用此缓存组件。当启用了实际的缓存支持后,我们可以切换到使用相应的缓存组件。 在这两种情况中,我们可以使用同样的代码Yii::app()->cache->get($key) 获取数据片段而不需要担心 Yii::app()->cache 可能会是 null。此组件从 1.0.5 版开始支持。 提示: 由于所有的这些缓存组件均继承自同样的基类 CCache,因此无需改变使用缓存的那些代码就可以切换到使用另一种缓存方式。 缓存可以用于不同的级别。最低级别中,我们使用缓存存储单个数据片段,例如变量,我们将此称为 数据缓存(data caching)。下一个级别中,我们在缓存中存储一个由视图脚本的一部分生成的页面片段。 而在最高级别中,我们将整个页面存储在缓存中并在需要时取回。 在接下来的几个小节中,我们会详细讲解如何在这些级别中使用缓存。 注意: 按照定义,缓存是一个不稳定的存储媒介。即使没有超时,它也并不确保缓存数据一定存在。 因此,不要将缓存作为持久存储器使用。(例如,不要使用缓存存储 Session 数据)。 2.缓存的配置和调用方式yii中的缓存主要是通过组件的方式实现的,具体需要配置方式可以通过缓存的类说明进行配置。 通常是指定缓存组件的类 例如apc array( 'class'=>'system.caching.CApcCache' ),memcache的配置方式可能是 array( * 'cache'=>array( * 'class'=>'CMemCache',* 'servers'=>array( * array( * 'host'=>'server1',* 'port'=>11211,* 'weight'=>60,* ),* array( * 'host'=>'server2',* 'weight'=>40,* ),* ),* ),* )使用方式: yii封装了对不同缓存操作的方法,主要集中在CCache。CCache是所有Cache类的基类。所以配置好缓存后可以调用方式很简单:
*
根据CCache类说明可以看出,常见的缓存操作方法get,set,add,delete,flush getValue($this->generateUniqueKey($id)))!==false)
{
$data=unserialize($value);
if(!is_array($data))
return false;
if(!($data[1] instanceof ICacheDependency) || !$data[1]->getHasChanged())
{
Yii::trace('Serving "'.$id.'" from cache','system.caching.'.get_class($this));
return $data[0];
}
}
return false;
}
/**
* Retrieves multiple values from cache with the specified keys.
* Some caches (such as memcache,apc) allow retrieving multiple cached values at one time,* which may improve the performance since it reduces the communication cost.
* In case a cache doesn't support this feature natively,it will be simulated by this method.
* @param array $ids list of keys identifying the cached values
* @return array list of cached values corresponding to the specified keys. The array
* is returned in terms of (key,value) pairs.
* If a value is not cached or expired,the corresponding array value will be false.
* @since 1.0.8
*/
public function mget($ids)
{
$uniqueIDs=array();
$results=array();
foreach($ids as $id)
{
$uniqueIDs[$id]=$this->generateUniqueKey($id);
$results[$id]=false;
}
$values=$this->getValues($uniqueIDs);
foreach($uniqueIDs as $id=>$uniqueID)
{
if(!isset($values[$uniqueID]))
continue;
$data=unserialize($values[$uniqueID]);
if(is_array($data) && (!($data[1] instanceof ICacheDependency) || !$data[1]->getHasChanged()))
{
Yii::trace('Serving "'.$id.'" from cache','system.caching.'.get_class($this));
$results[$id]=$data[0];
}
}
return $results;
}
/**
* Stores a value identified by a key into cache.
* If the cache already contains such a key,the existing value and
* expiration time will be replaced with the new ones.
*
* @param string $id the key identifying 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 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
*/
public function set($id,$value,$expire=0,$dependency=null)
{
Yii::trace('Saving "'.$id.'" to cache','system.caching.'.get_class($this));
if($dependency!==null)
$dependency->evaluateDependency();
$data=array($value,$dependency);
return $this->setValue($this->generateUniqueKey($id),serialize($data),$expire);
}
/**
* 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 $id the key identifying 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 ICacheDependency $dependency dependency of the cached item. If the dependency changes,false otherwise
*/
public function add($id,$dependency=null)
{
Yii::trace('Adding "'.$id.'" to cache',$dependency);
return $this->addValue($this->generateUniqueKey($id),$expire);
}
/**
* Deletes a value with the specified key from cache
* @param string $id the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
public function delete($id)
{
Yii::trace('Deleting "'.$id.'" from cache','system.caching.'.get_class($this));
return $this->deleteValue($this->generateUniqueKey($id));
}
/**
* Deletes all values from cache.
* Be careful of performing this operation if the cache is shared by multiple applications.
* @return boolean whether the flush operation was successful.
*/
public function flush()
{
Yii::trace('Flushing cache','system.caching.'.get_class($this));
return $this->flushValues();
}
即: cache->xxxxxx对应具体的方法。 例如: cache->add($id,$value); var_dump(Yii::app()->cache->get($id));下面是yii官方给出的几种缓存方式的使用说明,这里就麻木不仁,照搬了 3.缓存的使用:数据缓存数据缓存 数据缓存即存储一些 PHP 变量到缓存中,以后再从缓存中取出来。出于此目的,缓存组件的基类 CCache 提供了两个最常用的方法: set() 和 get()。 要在缓存中存储一个变量 $value ,我们选择一个唯一 ID 并调用 set() 存储它: cache->set($id,$value);(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
