网络编程 
首页 > 网络编程 > 浏览文章

YII Framework框架教程之缓存用法详解

(编辑:jimmy 日期: 2024/5/20 浏览:3 次 )

本文实例讲述了YII Framework框架缓存用法。分享给大家供大家参考,具体如下:

缓存的产生原因众所周知。于是YII作为一个高效,好用的框架,不能不支持缓存。所以YII对各种流行的缓存都提供了接口,你可以根据你的需要使用不同的缓存。

1.YII中的缓存介绍

YII中的缓存是通过组件方式定义的,具体在如下目录

/yii_dev/yii/framework/caching# tree
.
├── CApcCache.php
├── CCache.php
├── CDbCache.php
├── CDummyCache.php
├── CEAcceleratorCache.php
├── CFileCache.php
├── CMemCache.php
├── CWinCache.php
├── CXCache.php
├── CZendDataCache.php
└── dependencies
    ├── CCacheDependency.php
    ├── CChainedCacheDependency.php
    ├── CDbCacheDependency.php
    ├── CDirectoryCacheDependency.php
    ├── CExpressionDependency.php
    ├── CFileCacheDependency.php
    └── CGlobalStateCacheDependency.php

1 directory, 17 files

官方原文解释如下:

Yii 提供了不同的缓存组件,可以将缓存数据存储到不同的媒介中。例如, CMemCache 组件封装了 PHP 的 memcache 扩展并使用内存作为缓存存储媒介。 CApcCache 组件封装了 PHP APC 扩展; 而 CDbCache 组件会将缓存的数据存入数据库。下面是一个可用缓存组件的列表:

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

'cache'=>array(
  'class'=>'system.caching.CApcCache'
),

memcache的配置方式可能是

* array(
*   'components'=>array(
*     'cache'=>array(
*       'class'=>'CMemCache',
*       'servers'=>array(
*         array(
*           'host'=>'server1',
*           'port'=>11211,
*           'weight'=>60,
*         ),
*         array(
*           'host'=>'server2',
*           'port'=>11211,
*           'weight'=>40,
*         ),
*       ),
*     ),
*   ),
* )

使用方式:

yii封装了对不同缓存操作的方法,主要集中在CCache。CCache是所有Cache类的基类。所以配置好缓存后可以调用方式很简单:

<"htmlcode">
/**
 * Retrieves a value from cache with a specified key.
 * @param string $id a key identifying the cached value
 * @return mixed the value stored in cache, false if the value is not in the cache, expired or the dependency has changed.
 */
public function get($id)
{
  if(($value=$this->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, the item is labeled invalid.
 * @return boolean true if the value is successfully stored into cache, false otherwise
 */
public function add($id,$value,$expire=0,$dependency=null)
{
  Yii::trace('Adding "'.$id.'" to cache','system.caching.'.get_class($this));
  if($dependency!==null)
    $dependency->evaluateDependency();
  $data=array($value,$dependency);
  return $this->addValue($this->generateUniqueKey($id),serialize($data),$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();
}

即:

Yii::app()->cache->xxx

xxx对应具体的方法。

例如:

$id = 'key1';
$value = 'cache value';
Yii::app()->cache->add($id, $value);
var_dump(Yii::app()->cache->get($id));

下面是yii官方给出的几种缓存方式的使用说明,这里就麻木不仁,照搬了

3.缓存的使用:数据缓存

数据缓存

数据缓存即存储一些 PHP 变量到缓存中,以后再从缓存中取出来。出于此目的,缓存组件的基类 CCache 提供了两个最常用的方法: set() 和 get()。

要在缓存中存储一个变量 $value ,我们选择一个唯一 ID 并调用 set() 存储它:

Yii::app()->cache->set($id, $value);

缓存的数据将一直留在缓存中,除非它由于某些缓存策略(例如缓存空间已满,旧的数据被删除)而被清除。 要改变这种行为,我们可以在调用 set() 的同时提供一个过期参数,这样在设定的时间段之后,缓存数据将被清除:

// 值$value 在缓存中最多保留30秒
Yii::app()->cache->set($id, $value, 30);

稍后当我们需要访问此变量时(在同一个或不同的 Web 请求中),就可以通过 ID 调用 get() 从缓存中将其取回。 如果返回的是 false,表示此值在缓存中不可用,我们应该重新生成它。

$value=Yii::app()->cache->get($id);
if($value===false)
{
  // 因为在缓存中没找到 $value ,重新生成它 ,
  // 并将它存入缓存以备以后使用:
  // Yii::app()->cache->set($id,$value);
}

为要存入缓存的变量选择 ID 时,要确保此 ID 对应用中所有其他存入缓存的变量是唯一的。 而在不同的应用之间,这个 ID 不需要是唯一的。缓存组件具有足够的智慧区分不同应用中的 ID。

一些缓存存储器,例如 MemCache, APC, 支持以批量模式获取多个缓存值。这可以减少获取缓存数据时带来的开销。 从版本 1.0.8 起,Yii 提供了一个新的名为 mget() 的方法。它可以利用此功能。如果底层缓存存储器不支持此功能,mget() 依然可以模拟实现它。

要从缓存中清除一个缓存值,调用 delete(); 要清楚缓存中的所有数据,调用 flush()。 当调用 flush() 时一定要小心,因为它会同时清除其他应用中的缓存。

提示: 由于 CCache 实现了 ArrayAccess,缓存组件也可以像一个数组一样使用。下面是几个例子:

$cache=Yii::app()->cache;
$cache['var1']=$value1; // 相当于: $cache->set('var1',$value1);
$value2=$cache['var2']; // 相当于: $value2=$cache->get('var2');

1. 缓存依赖

除了过期设置,缓存数据也可能会因为依赖条件发生变化而失效。例如,如果我们缓存了某些文件的内容,而这些文件发生了改变,我们就应该让缓存的数据失效, 并从文件中读取最新内容而不是从缓存中读取。

我们将一个依赖关系表现为一个 CCacheDependency 或其子类的实例。 当调用 set() 时,我们连同要缓存的数据将其一同传入。

// 此值将在30秒后失效
// 也可能因依赖的文件发生了变化而更快失效
Yii::app()->cache->set($id, $value, 30, new CFileCacheDependency('FileName'));

现在如果我们通过调用get() 从缓存中获取 $value ,依赖关系将被检查,如果发生改变,我们将会得到一个 false 值,表示数据需要被重新生成。

如下是可用的缓存依赖的简要说明:

CFileCacheDependency: 如果文件的最后修改时间发生改变,则依赖改变。
CDirectoryCacheDependency: 如果目录和其子目录中的文件发生改变,则依赖改变。
CDbCacheDependency: 如果指定 SQL 语句的查询结果发生改变,则依赖改变。
CGlobalStateCacheDependency: 如果指定的全局状态发生改变,则依赖改变。全局状态是应用中的一个跨请求,跨会话的变量。它是通过 CApplication::setGlobalState() 定义的。
CChainedCacheDependency: 如果链中的任何依赖发生改变,则此依赖改变。
CExpressionDependency: 如果指定的 PHP 表达式的结果发生改变,则依赖改变。此类从版本 1.0.4 起可用。

4.缓存的使用:片段缓存

片段缓存(Fragment Caching)

片段缓存指缓存网页某片段。例如,如果一个页面在表中显示每年的销售摘要,我们可以存储此表在缓存中,减少每次请求需要重新产生的时间。

要使用片段缓存,在控制器视图脚本中调用CController::beginCache() 和CController::endCache() 。这两种方法开始和结束包括的页面内容将被缓存。类似data caching ,我们需要一个编号,识别被缓存的片段。

...别的HTML内容...
<"htmlcode">
...其他HTML内容...
<"htmlcode">
...其他HTML内容...
<"htmlcode">
...其他HTML内容...
<"htmlcode">
...其他HTML内容...
<"htmlcode">
public function filters()
{
  return array(
    array(
      'COutputCache',
      'duration'=>100,
      'varyByParam'=>array('id'),
    ),
  );
}

上述过滤器配置会使过滤器适用于控制器中的所有行动。 我们可能会限制它在一个或几个行动通过使用插件操作器。 更多的细节中可以看过滤器。

Tip: 我们可以使用COutputCache作为一个过滤器,因为它从CFilterWidget继承过来, 这意味着它是一个工具(widget)和一个过滤器。事实上,widget的工作方式和过滤器非常相似: 工具widget (过滤器filter)是在action动作里的内容执行前执行,在执行后结束。

6.缓存的使用:动态内容

动态内容(Dynamic Content)

当使用fragment caching或page caching,我们常常遇到的这样的情况 整个部分的输出除了个别地方都是静态的。例如,帮助页可能会显示静态的帮助 信息,而用户名称显示的是当前用户的。

解决这个问题,我们可以根据用户名匹配缓存内容,但是这将是我们宝贵空间一个巨大的浪费,因为缓存除了用户名其他大部分内容是相同的。我们还可以把网页切成几个片段并分别缓存,但这种情况会使页面和代码变得非常复杂。更好的方法是使用由[ CController ]提供的动态内容dynamic content功能 。

动态内容是指片段输出即使是在片段缓存包括的内容中也不会被缓存。即使是包括的内容是从缓存中取出,为了使动态内容在所有时间是动态的,每次都得重新生成。出于这个原因,我们要求 动态内容通过一些方法或函数生成。

调用CController::renderDynamic()在你想的地方插入动态内容。

...别的HTML内容...
<"_blank" href="https://www.jb51.net/Special/386.htm">Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

上一篇:YII Framework框架教程之国际化实现方法
下一篇:YII Framework框架教程之安全方案详解