Skip to content

Commit

Permalink
V1.17
Browse files Browse the repository at this point in the history
完成缓存功能
  • Loading branch information
michaelweixi committed Dec 28, 2016
1 parent 81d703b commit 4f83384
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function behaviors()
'roles' => ['?'],
],
[
'actions' => ['view', 'index', 'create','update'],
'actions' => ['view', 'index', 'create','update','delete'],
'allow' => true,
'roles' => ['@'],
],
Expand Down
1 change: 1 addition & 0 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

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

源码版本:V1.17 ————— 同上
————————————————————————————————————————

注:V1.5也可以在V1.4基础上执行下面这句SQL语句得到:
Expand Down
1 change: 1 addition & 0 deletions frontend/components/TagsCloudWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function run()
' <h'.$weight.' style="display:inline-block;"><span class="label label-'
.$fontStyle[$weight].'">'.$tag.'</span></h'.$weight.'></a>';
}
sleep(3);
return $tagString;

}
Expand Down
31 changes: 31 additions & 0 deletions frontend/controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use common\models\Tag;
use common\models\Comment;
use common\models\User;
use yii\rest\Serializer;

/**
* PostController implements the CRUD actions for Post model.
Expand Down Expand Up @@ -51,6 +52,36 @@ public function behaviors()
],
],

'pageCache'=>[
'class'=>'yii\filters\PageCache',
'only'=>['index'],
'duration'=>600,
'variations'=>[
Yii::$app->request->get('page'),
Yii::$app->request->get('PostSearch'),
],
'dependency'=>[
'class'=>'yii\caching\DbDependency',
'sql'=>'select count(id) from post',
],
],

'httpCache'=>[
'class'=>'yii\filters\HttpCache',
'only'=>['detail'],
'lastModified'=>function ($action,$params){
$q = new \yii\db\Query();
return $q->from('post')->max('update_time');
},
'etagSeed'=>function ($action,$params) {
$post = $this->findModel(Yii::$app->request->get('id'));
return serialize([$post->title,$post->content]);
},

'cacheControlHeader' => 'public,max-age=600',

],


];
}
Expand Down
38 changes: 35 additions & 3 deletions frontend/views/post/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use yii\widgets\ListView;
use frontend\components\TagsCloudWidget;
use frontend\components\RctReplyWidget;
use common\models\Post;
use yii\caching\DbDependency;
use yii\caching\yii\caching;

/* @var $this yii\web\View */
/* @var $searchModel common\models\PostSearch */
Expand Down Expand Up @@ -43,10 +46,27 @@
<div class="searchbox">
<ul class="list-group">
<li class="list-group-item">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查找文章
<span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查找文章(
<?php
//数据缓存示例代码
/*
$data = Yii::$app->cache->get('postCount');
$dependency = new DbDependency(['sql'=>'select count(id) from post']);
if ($data === false)
{
$data = Post::find()->count(); sleep(5);
Yii::$app->cache->set('postCount',$data,600,$dependency); //设置缓存60秒后过期
}
echo $data;
*/
?>
<?= Post::find()->count();?>
</li>
<li class="list-group-item">
<form class="form-inline" action="index.php?r=post/index" id="w0" method="get">
<form class="form-inline" action="<?= Yii::$app->urlManager->createUrl(['post/index']);?>" id="w0" method="get">
<div class="form-group">
<input type="text" class="form-control" name="PostSearch[title]" id="w0input" placeholder="按标题">
</div>
Expand All @@ -63,7 +83,19 @@
<span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 标签云
</li>
<li class="list-group-item">
<?= TagsCloudWidget::widget(['tags'=>$tags])?>
<?php
//片段缓存示例代码
/*
$dependency = new DbDependency(['sql'=>'select count(id) from post']);
if ($this->beginCache('cache',['duration'=>600],['dependency'=>$dependency]))
{
echo TagsCloudWidget::widget(['tags'=>$tags]);
$this->endCache();
}
*/
?>
<?= TagsCloudWidget::widget(['tags'=>$tags]);?>
</li>
</ul>
</div>
Expand Down

0 comments on commit 4f83384

Please sign in to comment.