您的位置:首页 > 编程语言 > PHP开发

php之yii缓存学习笔记

2016-08-17 00:14 246 查看
php之yii缓存学习笔记

官网给以缓存的定义:缓存是提升 Web 应用性能简便有效的方式。通过将相对静态的数据 存储到缓存并在收到请求时取回缓存,应用程序便节省了每次重新生 成这些数据所需的时间。缓存可以应用在
Web 应用程序的任何层级任何位置。在服务器端, 在较的低层面,缓存可能用于存储基础数据,例如从数据库中取 出的最新文章列表;在较高的层面,缓存可能用于存储一段或整 个 Web 页面,例如最新文章的渲染结果。在客户端,HTTP 缓存 可能用于将最近访问的页面内容存储到浏览器缓存中

其中yii支持   1.数据缓存

                     2.片段缓存

                     3.页面缓存

                     4.http缓存

1.数据缓存:数据缓存是指将一些 PHP 变量存储到缓存中,使用时再从缓存中取回。 它也是更高级缓存特性的基础,例如查询缓存 和内容缓存。

(1)读取缓存

<span style="font-size:18px;"><?php
namespace app\controllers;
use yii\web\Controller;
/**
 * qishuixian 2016.8.9
 */
class HelloController extends Controller
{
     public function actionIndex(){
    //获取缓存组件
    $cache = \YII::$app->cache;
    $cache->add('key1','hello world!');
    //读取缓存
     $data = $cache->get('key1');
     var_dump($data);
  }
}
</span>


(2)相同属性重复添加时

<span style="font-size:18px;"><?php

namespace app\controllers;

use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function actionIndex(){
//获取缓存组件
/$cache = \YII::$app->cache;
//往缓存当中写数据,缓存的方法中后来的数据不能覆盖前面的数据
$cache->add('key1','hello world!');
$cache->add('key2','hello world2!');
//读取缓存
$data = $cache->get('key1');
var_dump($data);
}
}</span>


(3)修改缓存

<span style="font-size:18px;"><?php

namespace app\controllers;

use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function actionIndex(){
//获取缓存组件
/$cache = \YII::$app->cache;
$cache->add('key1','hello world!');
//修改缓存
// $cache->set('key1','hello qishuixian');
//读取缓存
$data = $cache->get('key1');
var_dump($data);
}
}</span>


(4)删除缓存
<span style="font-size:18px;"><?php

namespace app\controllers;

use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function actionIndex(){
//获取缓存组件
/$cache = \YII::$app->cache;
//往缓存当中写数据,缓存的方法中后来的数据不能覆盖前面的数据
$cache->add('key1','hello world!');
$cache->add('key2','hello world2!');
//删除数据
$cache->delete('key1');
//把所有的缓存数据都清理掉
// $cache->flush();
//读取缓存
$data = $cache->get('key1');
var_dump($data);
}
}</span>


(5)设置、修改缓存的有效期

<span style="font-size:18px;">use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function actionIndex(){
//获取缓存组件
/$cache = \YII::$app->cache;
//设置、修改缓存的有效期
$cache->add('key1','hello world !',1);
$cache->set('key1','hello world !',15);
echo $cache->get('key1');
//读取缓存
$data = $cache->get('key1');
var_dump($data);
}
}</span>


数据缓存中依赖关系详解

   (1)文件的依赖

<span style="font-size:18px;">class HelloController extends Controller
{
public function actionIndex(){
//获取缓存组件
/$cache = \YII::$app->cache;
$dependency = new \yii\caching\FileDependency(['fileName'=>'robots.txt']);
$cache->add('file_key','hello world',3000,$dependency);
var_dump($cache->get('file_key'));
}
}</span>

(2)表达式依赖
<span style="font-size:18px;">namespace app\controllers;

use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function actionIndex(){
//获取缓存组件
/$cache = \YII::$app->cache;
//表达式的依赖
$dependency = new \yii\caching\ExpressionDependency(['expression'=>'\YII::$app->request->get("name")']);
$cache->add('expression_key','hello world',3000,$dependency);
var_dump($cache->get('expression_key'));
}
}</span>


(3)DB依赖 数据库的依赖
<span style="font-size:18px;"><?php

namespace app\controllers;

use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function actionIndex(){
//获取缓存组件
/$cache = \YII::$app->cache;
$dependency = new \yii\caching\DbDependency(['sql'=>'SELECT count(*) FROM yii.order']);
$cache->add('expression_key','hello world',3000,$dependency);
var_dump($cache->get('expression_key'));
}
}</span>


2.片段缓存:片段缓存指的是缓存页面内容中的某个片段。例如,一个页面显示了逐年销售额的摘要表格, 可以把表格缓存下来,以消除每次请求都要重新生成表格 的耗时。片段缓存是基于数据缓存实现的。
<span style="font-size:18px;">use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function actionIndex(){
//获取缓存组件
$cache = \YII::$app->cache;
//片段缓存
return $this->renderPartial('index1');
}
}</span>

<span style="font-size:18px;"><h1>1111111111111111Congratulations!</h1>
<?php $this->beginBlock('block1');?>
<h1>index</h1>
<?php $this->endBlock();?></span>

3.页面缓存:页面缓存指的是在服务器端缓存整个页面的内容。随后当同一个页面 被请求时,内容将从缓存中取出,而不是重新生成。页面缓存由 yii\filters\PageCache 类提供支持,该类是一个 过滤器。它可以像这样在控制器类中使用:
<span style="font-size:18px;"><?php

namespace app\controllers;

use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function behaviors(){
// echo '1';
return [
'class'=>'yii\filters\PageCache',
'duration'=>1000,
'dependency'=>[
'class'=>'yii\caching\FileDependency',
'fileName'=>'hw.txt'
]
];
}
public function actionIndex(){
echo "4";
}
}
</span>
页面缓存和片段缓存极其相似。它们都支持 duration,dependencies, variations 和 enabled 配置选项。它们的主要区别是页面缓存是由过滤器实现, 而片段缓存则是一个小部件。

4.http缓存:通过配置 yii\filters\HttpCache 过滤器,控制器操作渲染的内容就能 缓存在客户端。yii\filters\HttpCache 过滤器仅对 GET 和 HEAD 请求生效,它能为这些请求设置三种与缓存有关的 HTTP 头。

通过配置 yii\filters\HttpCache::lastModified 属性向客户端发送 Last-Modified 头。该属性的值应该为 PHP callable 类型,返回的是页 面修改时的 Unix 时间戳。该 callable 的参数和返回值应该如下:
<span style="font-size:18px;"><?php

namespace app\controllers;

use yii\web\Controller;
/**
* qishuixian 2016.8.9
*/
class HelloController extends Controller
{
public function behaviors(){
return [
[
'class' => 'yii\filters\HttpCache',
'only' => ['index'],
'lastModified' => function ($action, $params) {
$q = new \yii\db\Query();
return $q->from('post')->max('updated_at');
},
],
];
}
//http缓存简介
public function actionIndex(){
echo "4";
}
}
</span>


参考,学习地址:1.http://www.yiichina.com/doc/guide/2.0/caching-overview

                2.http://www.imooc.com/learn/440
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: