首頁 php教程 PHP开发 YII Framework框架教學緩存用法詳解

YII Framework框架教學緩存用法詳解

Dec 27, 2016 pm 02:04 PM

本文實例講述了YII Framework框架快取用法。分享給大家供大家參考,具體如下:

快取的產生原因眾所周知。於是YII作為一個高效,好用的框架,不能不支援快取。所以YII對各種流行的快取都提供了接口,你可以根據你的需求使用不同的快取。

1.YII中的快取介紹

YII中的快取是透過元件方式定義的,具體在如下目錄

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

1 directory, 17 files

官方原文解釋如下:Y例如, 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類別的基底類別。所以設定好快取後可以呼叫方式很簡單:

<?php
/**
 * CCache is the base class for cache classes with different cache storage implementation.
 *
 * A data item can be stored in cache by calling {@link set} and be retrieved back
 * later by {@link get}. In both operations, a key identifying the data item is required.
 * An expiration time and/or a dependency can also be specified when calling {@link set}.
 * If the data item expires or the dependency changes, calling {@link get} will not
 * return back the data item.
 *
 * Note, by definition, cache does not ensure the existence of a value
 * even if it does not expire. Cache is not meant to be a persistent storage.
 *
 * CCache implements the interface {@link ICache} with the following methods:
 * <ul>
 * <li>{@link get} : retrieve the value with a key (if any) from cache</li>
 * <li>{@link set} : store the value with a key into cache</li>
 * <li>{@link add} : store the value only if cache does not have this key</li>
 * <li>{@link delete} : delete the value with the specified key from cache</li>
 * <li>{@link flush} : delete all values from cache</li>
 * </ul>
 *
 * Child classes must implement the following methods:
 * <ul>
 * <li>{@link getValue}</li>
 * <li>{@link setValue}</li>
 * <li>{@link addValue}</li>
 * <li>{@link deleteValue}</li>
 * <li>{@link flush} (optional)</li>
 * </ul>
 *
 * CCache also implements ArrayAccess so that it can be used like an array.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Id: CCache.php 3001 2011-02-24 16:42:44Z alexander.makarow $
 * @package system.caching
 * @since 1.0
 */
abstract class CCache extends CApplicationComponent implements ICache, ArrayAccess
{
登入後複製

根據CCache類別說明可以看出,常見的快取操作方法get,set,add,delete,flush

/**
 * 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(&#39;Serving "&#39;.$id.&#39;" from cache&#39;,&#39;system.caching.&#39;.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&#39;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(&#39;Serving "&#39;.$id.&#39;" from cache&#39;,&#39;system.caching.&#39;.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(&#39;Saving "&#39;.$id.&#39;" to cache&#39;,&#39;system.caching.&#39;.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(&#39;Adding "&#39;.$id.&#39;" to cache&#39;,&#39;system.caching.&#39;.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(&#39;Deleting "&#39;.$id.&#39;" from cache&#39;,&#39;system.caching.&#39;.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(&#39;Flushing cache&#39;,&#39;system.caching.&#39;.get_class($this));
  return $this->flushValues();
}
登入後複製

即:

Yii::app()->cache->xxx
登入後複製

xxx對應具體的方法。

例如:

$id = &#39;key1&#39;;
$value = &#39;cache value&#39;;
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[&#39;var1&#39;]=$value1; // 相当于: $cache->set(&#39;var1&#39;,$value1);
$value2=$cache[&#39;var2&#39;]; // 相当于: $value2=$cache->get(&#39;var2&#39;);
登入後複製

1. 缓存依赖

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

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

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

现在如果我们通过调用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内容...
<?php if($this->beginCache($id)) { ?>
...被缓存的内容...
<?php $this->endCache(); } ?>
...别的HTML内容...
登入後複製

如果我们不设定期限,它将默认为60 ,这意味着60秒后缓存内容将无效。

依赖(Dependency)

像data caching ,内容片段被缓存也可以有依赖。例如,文章的内容被显示取决于文章是否被修改。

要指定一个依赖,我们建立了dependency选项,可以是一个实现[ICacheDependency]的对象或可用于生成依赖对象的配置数组。下面的代码指定片段内容取决于lastModified 列的值是否变化:

...其他HTML内容...
<?php if($this->beginCache($id, array(&#39;dependency&#39;=>array(
    &#39;class&#39;=>&#39;system.caching.dependencies.CDbCacheDependency&#39;,
    &#39;sql&#39;=>&#39;SELECT MAX(lastModified) FROM Post&#39;)))) { ?>
...被缓存的内容...
<?php $this->endCache(); } ?>
...其他HTML内容...
登入後複製

变化(Variation)

缓存的内容可根据一些参数变化。例如,每个人的档案都不一样。缓存的档案内容将根据每个人ID变化。这意味着,当调用beginCache()时将用不同的ID。

COutputCache内置了这一特征,程序员不需要编写根据ID变动内容的模式。以下是摘要。

varyByRoute: 设置此选项为true ,缓存的内容将根据route变化。因此,每个控制器和行动的组合将有一个单独的缓存内容。
varyBySession: 设置此选项为true ,缓存的内容将根据session ID变化。因此,每个用户会话可能会看到由缓存提供的不同内容。
varyByParam: 设置此选项的数组里的名字,缓存的内容将根据GET参数的值变动。例如,如果一个页面显示文章的内容根据id的GET参数,我们可以指定varyByParam为array('id'),以使我们能够缓存每篇文章内容。如果没有这样的变化,我们只能能够缓存某一文章。

varyByExpression: by setting this option to a PHP expression, we can make the cached content to be variated according to the result of this PHP expression. This option has been available since version 1.0.4.
Request Types

有时候,我们希望片段缓存只对某些类型的请求启用。例如,对于某张网页上显示表单,我们只想要缓存initially requested表单(通过GET请求)。任何随后显示(通过POST请求)的表单将不被缓存,因为表单可能包含用户输入。要做到这一点,我们可以指定requestTypes 选项:

...其他HTML内容...
<?php if($this->beginCache($id, array(&#39;requestTypes&#39;=>array(&#39;GET&#39;)))) { ?>
...被缓存的内容...
<?php $this->endCache(); } ?>
...其他HTML内容...
登入後複製

2. 嵌套缓存(Nested Caching)

片段缓存可以嵌套。就是说一个缓存片段附在一个更大的片段缓存里。例如,意见缓存在内部片段缓存,而且它们一起在外部缓存中在文章内容里缓存。

...其他HTML内容...
<?php if($this->beginCache($id1)) { ?>
...外部被缓存内容...
  <?php if($this->beginCache($id2)) { ?>
  ...内部被缓存内容...
  <?php $this->endCache(); } ?>
...外部被缓存内容...
<?php $this->endCache(); } ?>
...其他HTML内容...
登入後複製

嵌套缓存可以设定不同的缓存选项。例如, 在上面的例子中内部缓存和外部缓存可以设置时间长短不同的持续值。当数据存储在外部缓存无效,内部缓存仍然可以提供有效的内部片段。 然而,反之就不行了。如果外部缓存包含有效的数据, 它会永远保持缓存副本,即使内容中的内部缓存已经过期。

5.缓存的使用:页面缓存

页面缓存

页面缓存指的是缓存整个页面的内容。页面缓存可以发生在不同的地方。 例如,通过选择适当的页面头,客户端的浏览器可能会缓存网页浏览有限时间。 Web应用程序本身也可以在缓存中存储网页内容。 在本节中,我们侧重于后一种办法。

页面缓存可以被看作是 片段缓存一个特殊情况 。 由于网页内容是往往通过应用布局来生成,如果我们只是简单的在布局中调用beginCache() 和endCache(),将无法正常工作。 这是因为布局在CController::render()方法里的加载是在页面内容产生之后。

如果想要缓存整个页面,我们应该跳过产生网页内容的动作执行。我们可以使用COutputCache作为动作 过滤器来完成这一任务。下面的代码演示如何配置缓存过滤器:

public function filters()
{
  return array(
    array(
      &#39;COutputCache&#39;,
      &#39;duration&#39;=>100,
      &#39;varyByParam&#39;=>array(&#39;id&#39;),
    ),
  );
}
登入後複製

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

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

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

动态内容(Dynamic Content)

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

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

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

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

...别的HTML内容...
<?php if($this->beginCache($id)) { ?>
...被缓存的片段内容...
  <?php $this->renderDynamic($callback); ?>
...被缓存的片段内容...
<?php $this->endCache(); } ?>
...别的HTML内容...
登入後複製

在上面的, $callback指的是有效的PHP回调。它可以是指向当前控制器类的方法或者全局函数的字符串名。它也可以是一个数组名指向一个类的方法。其他任何的参数,将传递到renderDynamic()方法中。回调将返回动态内容而不是仅仅显示它。

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

更多YII Framework框架教程之缓存用法详解相关文章请关注PHP中文网!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)