Skip to content

Commit 4f83384

Browse files
committed
V1.17
完成缓存功能
1 parent 81d703b commit 4f83384

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

backend/controllers/PostController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function behaviors()
3838
'roles' => ['?'],
3939
],
4040
[
41-
'actions' => ['view', 'index', 'create','update'],
41+
'actions' => ['view', 'index', 'create','update','delete'],
4242
'allow' => true,
4343
'roles' => ['@'],
4444
],

database/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
源码版本:V1.16 ————— 同上
3838

39+
源码版本:V1.17 ————— 同上
3940
————————————————————————————————————————
4041

4142
注:V1.5也可以在V1.4基础上执行下面这句SQL语句得到:

frontend/components/TagsCloudWidget.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function run()
3030
' <h'.$weight.' style="display:inline-block;"><span class="label label-'
3131
.$fontStyle[$weight].'">'.$tag.'</span></h'.$weight.'></a>';
3232
}
33+
sleep(3);
3334
return $tagString;
3435

3536
}

frontend/controllers/PostController.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use common\models\Tag;
1414
use common\models\Comment;
1515
use common\models\User;
16+
use yii\rest\Serializer;
1617

1718
/**
1819
* PostController implements the CRUD actions for Post model.
@@ -51,6 +52,36 @@ public function behaviors()
5152
],
5253
],
5354

55+
'pageCache'=>[
56+
'class'=>'yii\filters\PageCache',
57+
'only'=>['index'],
58+
'duration'=>600,
59+
'variations'=>[
60+
Yii::$app->request->get('page'),
61+
Yii::$app->request->get('PostSearch'),
62+
],
63+
'dependency'=>[
64+
'class'=>'yii\caching\DbDependency',
65+
'sql'=>'select count(id) from post',
66+
],
67+
],
68+
69+
'httpCache'=>[
70+
'class'=>'yii\filters\HttpCache',
71+
'only'=>['detail'],
72+
'lastModified'=>function ($action,$params){
73+
$q = new \yii\db\Query();
74+
return $q->from('post')->max('update_time');
75+
},
76+
'etagSeed'=>function ($action,$params) {
77+
$post = $this->findModel(Yii::$app->request->get('id'));
78+
return serialize([$post->title,$post->content]);
79+
},
80+
81+
'cacheControlHeader' => 'public,max-age=600',
82+
83+
],
84+
5485

5586
];
5687
}

frontend/views/post/index.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use yii\widgets\ListView;
66
use frontend\components\TagsCloudWidget;
77
use frontend\components\RctReplyWidget;
8+
use common\models\Post;
9+
use yii\caching\DbDependency;
10+
use yii\caching\yii\caching;
811

912
/* @var $this yii\web\View */
1013
/* @var $searchModel common\models\PostSearch */
@@ -43,10 +46,27 @@
4346
<div class="searchbox">
4447
<ul class="list-group">
4548
<li class="list-group-item">
46-
<span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查找文章
49+
<span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查找文章(
50+
<?php
51+
//数据缓存示例代码
52+
/*
53+
$data = Yii::$app->cache->get('postCount');
54+
$dependency = new DbDependency(['sql'=>'select count(id) from post']);
55+
56+
if ($data === false)
57+
{
58+
$data = Post::find()->count(); sleep(5);
59+
Yii::$app->cache->set('postCount',$data,600,$dependency); //设置缓存60秒后过期
60+
}
61+
62+
echo $data;
63+
*/
64+
?>
65+
<?= Post::find()->count();?>
66+
4767
</li>
4868
<li class="list-group-item">
49-
<form class="form-inline" action="index.php?r=post/index" id="w0" method="get">
69+
<form class="form-inline" action="<?= Yii::$app->urlManager->createUrl(['post/index']);?>" id="w0" method="get">
5070
<div class="form-group">
5171
<input type="text" class="form-control" name="PostSearch[title]" id="w0input" placeholder="按标题">
5272
</div>
@@ -63,7 +83,19 @@
6383
<span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 标签云
6484
</li>
6585
<li class="list-group-item">
66-
<?= TagsCloudWidget::widget(['tags'=>$tags])?>
86+
<?php
87+
//片段缓存示例代码
88+
/*
89+
$dependency = new DbDependency(['sql'=>'select count(id) from post']);
90+
91+
if ($this->beginCache('cache',['duration'=>600],['dependency'=>$dependency]))
92+
{
93+
echo TagsCloudWidget::widget(['tags'=>$tags]);
94+
$this->endCache();
95+
}
96+
*/
97+
?>
98+
<?= TagsCloudWidget::widget(['tags'=>$tags]);?>
6799
</li>
68100
</ul>
69101
</div>

0 commit comments

Comments
 (0)