1.控制器中CacheDemoController.php
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: moka同学 5 * Date: 2016/06/29 6 * Time: 8:38 7 */ 8 namespace appcontrollers; 9 10 use yiiwebController; 11 12 class CacheDemoController extends Controller 13 { 14 /* public function behaviors() 15 { 16 return [ 17 [ 18 'class'=>'yiifiltersHttpCache', 19 'lastModified'=>function(){ 20 return 124234; 21 }, 22 'etagSeed'=>function(){ 23 return 'etagseed23'; 24 } 25 ] 26 ]; 27 }*/ 28 29 public function actionIndex(){ 30 //echo 'hello';die(); 31 $cache =Yii::$app->cache; 32 $data = $cache->get('key'); 33 if(!$data){ 34 $data = "h33e55533lo"; 35 $cache->set('key',$data,15);//保存缓存 36 //$data=$cache->get('key'); 37 } 38 return $this->render('index2', [ 39 'records' => $data, 40 ]); 41 } 42 43 public function actionIndex3() 44 { 45 //$res = YII::$app->response; 46 //Example 1 状态码 47 //设置状态码 :$res->statusCode = '404'; 48 /* 49 //对http头部的处理 50 $res->headers->add('pragma','no-cache'); //添加pragma 51 $res->headers->set('pragma','max-age-5'); //设置pragma的值为max-age-5 52 $res->headers->remove('pragma'); // 移除pragma的头部*/ 53 54 //跳转 55 //$res->headers->add('location','http://www.baidu.com'); 56 //$res->headers->add('content-disposion','attachment'); 57 58 #----------------------------------------------------------------- 59 60 //Example one 61 //缓存技术 62 //Ⅰ第一步 获取缓存组件 63 $cache = Yii::$app->cache; 64 65 /* //Ⅱ 第二步 往缓存中写数据 66 $cache->add('key1','hello world!'); 67 68 //Ⅳ 修改缓存数据 69 // $cache->set('key1','hello world2'); 70 71 //Ⅴ 删除缓存数据 72 $cache->delete('key1'); 73 74 // Ⅵ 清空数据 75 $cache->flush();*/ 76 77 //Ⅶ 设置缓存的有效期 78 //方法一 79 //$cache->add('key1','hello world',5);//缓存保存三秒 80 //方法二 81 //$cache->set('key1','hello world',5);//缓存设置3秒 82 83 84 //Ⅲ 第三步 读取缓存中的数据 85 //$data = $cache->get('key1'); 86 //print_r($data); 87 //var_dump($data); 88 89 //DB依赖 90 /* $dependency = new yiicachingDbDependency( 91 ['sql'=>'SELECT COUNT(*) FROM test'] 92 ); 93 $cache->add('db_key','hello world6',3,$dependency); 94 var_dump($cache->get('db_key'));*/ 95 96 97 // 案例 98 $data = $cache->get('cache_data_key'); 99 if ($data === false) { 100 //这里我们可以操作数据库获取数据,然后通过$cache->set方法进行缓存 101 $cacheData = 'he3443o' ; 102 //set方法的第一个参数是我们的数据对应的key值,方便我们获取到 103 //第二个参数即是我们要缓存的数据 104 //第三个参数是缓存时间,如果是0,意味着永久缓存。默认是0 105 $cache->set('cache_data_key', $cacheData, 13); 106 $data = $cache->get('cache_data_key'); 107 } 108 109 return $this->render('index2', [ 110 'records' => $data, 111 ]); 112 113 114 } 115 }
2.视图view/cache-demo/index.php 后边为index2
1 <div class="container"> 2 <?php 3 /** 4 * Created by PhpStorm. 5 * User: moka同学 6 * Date: 2016/06/27 7 * Time: 11:00 8 */ 9 ?> 10 <?php 11 if($this->beginCache('cache')){ 12 ?> 13 <div id="cache"> 14 这里有缓存片段werwe 15 </div> 16 <?php 17 $this->endCache(); 18 } 19 ?> 20 <div id="no-cache"> 21 这里没有缓存片段ertre 22 </div> 23 </div>
index2.php
1 <div class="container"> 2 <?php 3 echo $records; 4 ?> 5 </div>
index3.php
1 <div class="container"> 2 <?php 3 echo $new; 4 ?> 5 </div>
注:~
转载请注明出处。QQ1727728211