1.控制器中CacheDemoController.php

 <?php
/**
* Created by PhpStorm.
* User: moka同学
* Date: 2016/06/29
* Time: 8:38
*/
namespace app\controllers; use yii\web\Controller; class CacheDemoController extends Controller
{
/* public function behaviors()
{
return [
[
'class'=>'yii\filters\HttpCache',
'lastModified'=>function(){
return 124234;
},
'etagSeed'=>function(){
return 'etagseed23';
}
]
];
}*/ public function actionIndex(){
//echo 'hello';die();
$cache =\Yii::$app->cache;
$data = $cache->get('key');
if(!$data){
$data = "h33e55533lo";
$cache->set('key',$data,15);//保存缓存
//$data=$cache->get('key');
}
return $this->render('index2', [
'records' => $data,
]);
} public function actionIndex3()
{
//$res = \YII::$app->response;
//Example 1 状态码
//设置状态码 :$res->statusCode = '404';
/*
//对http头部的处理
$res->headers->add('pragma','no-cache'); //添加pragma
$res->headers->set('pragma','max-age-5'); //设置pragma的值为max-age-5
$res->headers->remove('pragma'); // 移除pragma的头部*/ //跳转
//$res->headers->add('location','http://www.baidu.com');
//$res->headers->add('content-disposion','attachment'); #----------------------------------------------------------------- //Example one
//缓存技术
//Ⅰ第一步 获取缓存组件
$cache = \Yii::$app->cache; /* //Ⅱ 第二步 往缓存中写数据
$cache->add('key1','hello world!'); //Ⅳ 修改缓存数据
// $cache->set('key1','hello world2'); //Ⅴ 删除缓存数据
$cache->delete('key1'); // Ⅵ 清空数据
$cache->flush();*/ //Ⅶ 设置缓存的有效期
//方法一
//$cache->add('key1','hello world',5);//缓存保存三秒
//方法二
//$cache->set('key1','hello world',5);//缓存设置3秒 //Ⅲ 第三步 读取缓存中的数据
//$data = $cache->get('key1');
//print_r($data);
//var_dump($data); //DB依赖
/* $dependency = new \yii\caching\DbDependency(
['sql'=>'SELECT COUNT(*) FROM test']
);
$cache->add('db_key','hello world6',3,$dependency);
var_dump($cache->get('db_key'));*/ // 案例
$data = $cache->get('cache_data_key');
if ($data === false) {
//这里我们可以操作数据库获取数据,然后通过$cache->set方法进行缓存
$cacheData = 'he3443o' ;
//set方法的第一个参数是我们的数据对应的key值,方便我们获取到
//第二个参数即是我们要缓存的数据
//第三个参数是缓存时间,如果是0,意味着永久缓存。默认是0
$cache->set('cache_data_key', $cacheData, 13);
$data = $cache->get('cache_data_key');
} return $this->render('index2', [
'records' => $data,
]); }
}

2.视图view/cache-demo/index.php 后边为index2

 <div class="container">
<?php
/**
* Created by PhpStorm.
* User: moka同学
* Date: 2016/06/27
* Time: 11:00
*/
?>
<?php
if($this->beginCache('cache')){
?>
<div id="cache">
这里有缓存片段werwe
</div>
<?php
$this->endCache();
}
?>
<div id="no-cache">
这里没有缓存片段ertre
</div>
</div>

index2.php

 <div class="container">
<?php
echo $records;
?>
</div>

index3.php

 <div class="container">
<?php
echo $new;
?>
</div>

注:~

转载请注明出处。QQ1727728211

[moka同学笔记]yii2.0缓存的更多相关文章

  1. [moka同学笔记]yii2.0查询数据库

      一. [:id占位符]使用 $results = Test::findBySql($sql,array(':id'=>'1 or 1=1))->all()   二. [id=1]  选 ...

  2. [moka同学笔记]Yii2.0 modal的使用

    第一次使用,时候不明白什么原理,大概用了几次后,才模模糊糊搞清楚原来是怎么一回事,现在就把写过的代码,贴在下边. 1.在视图文件中, 第一步首先在index.php文件中 做了一个a链接的按钮 调用了 ...

  3. [moka同学笔记]yii2.0数据库操作以及分页

    1.model中models/article.php 1 <?php 2 3 namespace app\models; 4 5 use Yii; 6 7 /** 8 * This is the ...

  4. [moka同学笔记]Yii2.0验证码

    1.Model中Code.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/07/25 * Time: 10:48 ...

  5. [moka同学笔记]yii2.0表单的使用

    1.创建model   /biaodan.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * Tim ...

  6. [moka同学笔记]yii2.0小物件的简单使用(第二种方法)

    1.在widgets/TestWidget.php中 <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * T ...

  7. [moka同学笔记]yii2.0小物件的简单使用(第一种方法)

    这是第一种方法,还有另一种方法,其实都差不多. 1.在创建widgets\HelloWiget.php <?php /** * Created by PhpStorm. * User: Admi ...

  8. [moka同学笔记]Yii2.0显示页匿名函数设置$value

    匿名函数设置$value <?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ // ['cl ...

  9. [moka同学笔记]Yii2.0给一张表中增加一个属性

    1.model中建立关联 public function getUser(){ return$this->hasOne(User::className(),['id'=>'uid']) ; ...

随机推荐

  1. ios中将事件添加到系统日历

    - (void)saveEvent:(id)sender { //事件市场 EKEventStore *eventStore = [[EKEventStore alloc] init]; //6.0及 ...

  2. 程序猿每个VPN真卡手

    昨天起使用了好久好久觉得是世上最好用的poVPN貌似跑路了 刚刚续费了一年也就这样没有了,可是更多的是心疼没有好用的vpn服务. 不过查到了一个红杏chrome插件,不是vpn 如果单单上网页足够了 ...

  3. Unity 由Verlet数值积分产生的头发运动

    先发下效果图. 参考项目unitychan-crs-master与miloyip大神的博客 爱丽丝的发丝,使用Verlet数值积分,根据旧的现在位置与上一桢位置来计算现在的位置,得到新的方向,上面的运 ...

  4. 【原】 COCOS2D—LUA 获取剪贴板内容

    android下: local luaj = require ("framework.luaj")   local ok,ret  = luaj.callStaticMethod( ...

  5. android自定义viewgroup之我也玩瀑布流

    先看效果图吧, 继上一篇<android自定义viewgroup实现等分格子布局>中实现的布局效果,这里稍微有些区别,每个格子的高度不规则,就是传说的瀑布流布局,一般实现这种效果,要么用第 ...

  6. Ext JS 6 入门学习资料大全(2016-12-14)

    现在 sencha touch已经升级为 Ext JS 6 了重新整理下资料 官方网站:https://www.sencha.com/ 在线文档:http://docs.sencha.com/extj ...

  7. mono+jexus 验证码不显示:System.Drawing

    System.ArgumentException The requested FontFamily could not be found [GDI+ status: FontFamilyNotFoun ...

  8. js webapp 滑动事件

    var startX, startY, endX, endY; $(".detailImg").on("touchstart", touchStart);$(& ...

  9. Redis PHP通用类

    找到一个比较全的Redis PHP操作类库,分享给大家 <?php /**  * redis操作类  * 说明,任何为false的串,存在redis中都是空串.  * 只有在key不存在时,才会 ...

  10. Objective-C之类和对象

    http://www.cnblogs.com/kenshincui/p/3861302.html