yii2的数据缓存,片段缓存,页面缓存、HTTP缓存和文件缓存

             

一、数据缓存

<?php
$count = Yii::$app->cache->get('postCount');//从缓存读取数据
$dependency = new \yii\caching\DbDe pendency(['sql' => 'select count(id) from post']);//缓存依赖
if($count === false)
{
    $count = \common\models\Post::find()->count();
    Yii::$app->cache->set('postCount', $count, 60, $dependency);//设置60s过期时间
}
echo $count;
?>

二、片段缓存

//片段缓存
$dependency = new DbDependency(['sql' => 'select count(id) from post']);//缓存依赖
if($this->beginCache('cache', ['duration' => 600],['dependency' => $dependency]))
{
    echo TagsCloudWidget::widget(['tags' => $tags]);
    $this->endCache();
}

三、页面缓存(controller中的behaviors编写)

//页面缓存    该方法会在其他方法之前执行
public function behaviors()
{
    // 声明缓存配置,需要注意的这里是二维数组
    return
    [
        'pageCache' => [
            'class' => 'yii\filters\PageCache',  //缓存控件,过滤器
            'only' => ['index'],                 //执行控制器
            'duration' => 600,                   //过期时间
            'variations' => [                    //url参数
                Yii::$app->request->get('page'),
                Yii::$app->request->get('PostFrontendSearch'),
            ],
            'dependency' => [                    //缓存依赖
                'class' => 'yii\caching\DbDependency',
                'sql' => 'select count(id) from post',
            ]
         ]
    ]
}

四、HTTP缓存(controller中的behaviors编写)

//HTTP缓存
'httpCache' => [
    'class' => 'yii\filters\HttpCache', //缓存控件,过滤器
    'only' => ['detail'], //执行控制器
    'lastModified' => function($action, $params) //最后修改时间
    {
        $q = new Query();
        return $q->from('post')->max('update_time'); //返回Unix时间戳
    },
    'etagSeed' => function ($action, $params) //etag
    {
        $post = $this->findModel(Yii::$app->request->get('id'));
        return serialize([$post->title, $post->content]); //返回序列号,标题和内容缓存
    },
    'cacheControlHeader' => 'public, mag-age=600',
]

五、文件缓存

1、配置缓存组件

在应用主体配置文件@app/config/web.php中的 $config 数组下的components数组中配置cache组件

2、缓存操作

$cache = \Yii::$app->cache;
<span class="redactor-invisible-space">
添加缓存<span class="redactor-invisible-space"></span></span>
$cache->add('name','value');//添加一个缓存<span class="redactor-invisible-space">
<span class="redactor-invisible-space">$cache->madd(['nameone'=>'valueone','nametwo'=>'valuetwo']);<span class="redactor-invisible-space">//添加多个缓存<span class="redactor-invisible-space">
<span class="redactor-invisible-space">
<span class="redactor-invisible-space">删除缓存<span class="redactor-invisible-space">
$cache->delete('name');<span class="redactor-invisible-space">//删除一个缓存<span class="redactor-invisible-space"></span>
<span class="redactor-invisible-space">$cache->flush();<span class="redactor-invisible-space">//删除全部缓存<span class="redactor-invisible-space">
<span class="redactor-invisible-space">
<span class="redactor-invisible-space">读取缓存<span class="redactor-invisible-space">
$cache->get('name');<span class="redactor-invisible-space">//读取一个缓存<span class="redactor-invisible-space">
$cache->mget(['nameone','nametwo']);<span class="redactor-invisible-space">//读取多个缓存<span class="redactor-invisible-space"></span></span></span></span></span></span>
<span class="redactor-invisible-space">
判断缓存是否存在<span class="redactor-invisible-space"></span>
$cache->exists('name');<span class="redactor-invisible-space"></span></span></span></span></span></span></span></span></span></span></span></span></span></span>


发表评论 请登录再评论
  •   文章分类
回到顶部