Yii片段缓存

片段缓存提供商网页的一个片段缓存。
第1步 - 添加一个新的 actionFragmentCaching()方法到 SiteController 控制器中。
public function actionFragmentCaching() {
   $user = new MyUser();
   $user->name = "cached user name";
   $user->email = "cacheduseremail@xuhuhu.com";
   $user->save();
   $models = MyUser::find()->all();
   return $this->render('cachedview', ['models' => $models]);
}
在上面的代码中,我们创建了一个新用户,并显示在一个 cachedview 视图文件中。
第2步 - 现在,创建一个新文件 cachedview.php 在 views/site 文件夹中。
<?php if ($this->beginCache('cachedview')) { ?>
   <?php foreach ($models as $model): ?>
      <?= $model->id; ?>
      <?= $model->name; ?>
      <?= $model->email; ?>
      <br/>
   <?php endforeach; ?>
<?php $this->endCache(); } ?>
<?php echo "Count:", \app\models\MyUser::find()->count(); ?>
我们在一对 beginCache()和 endCache()方法中包围的内容生成逻辑。
如在高速缓存中找到内容,beginCache()方法将呈现它。
第3步 - 打开 http://localhost:8080/index.php?r=site/fragment-caching 重新加载页面。将输出以下内容。
Yii片段缓存
请注意,beginCache() 和 endCache() 之前的内容已被缓存。在数据库中,示例中有共 13 用户,但只有12个被显示。

页面缓存

页面缓存提供整个网页的内容缓存。页面缓存是由 yii\filter\PageCache 类支持。
步骤1 - 在 SiteController 修改 behaviors()函数。
public function behaviors() {
   return [
      'access' => [
         'class' => AccessControl::className(),
         'only' => ['logout'],
         'rules' => [
            [
               'actions' => ['logout'],
               'allow' => true,
               'roles' => ['@'],
            ],
         ],
      ],
      'verbs' => [
         'class' => VerbFilter::className(),
         'actions' => [
            'logout' => ['post'],
         ],
      ],
      [
         'class' => 'yii\filters\PageCache',
         'only' => ['index'],
         'duration' => 60
      ],
   ];
}
上面的代码缓存索引页为 60 秒。
第2步 - 打开 http://localhost:8080/index.php?r=site/index 然后,修改索引视图文件的消息内容。

如果重新载入页面,因为页面被缓存,不会注意到有任何更改。等待一分钟,然后再重新加载页面。

HTTP缓存

Web应用程序也可以使用客户端的缓存。要使用它需要为控制器中动作配置 yii\filter\HttpCache 过滤器。
在 Last-Modified头 使用时间戳来指示该页面是否已被修改。
第1步 - 要启用发送 Last-Modified 头,配置 yii\filter\HttpCache::$lastModified 属性。
public function behaviors() {
   return [
      [
         'class' => 'yii\filters\HttpCache',
         'only' => ['index'],
         'lastModified' => function ($action, $params) {
            $q = new \yii\db\Query();
            return $q->from('news')->max('created_at');
         },
      ],
   ];
}
在上面的代码中,我们只有在 index 动作中启用了HTTP缓存。
应生成基于用户的 name 和 email 表头的 HTTP 标头。
当浏览器中首次打开 index 动作页面,则在服务器端生成内容并发送给浏览器端。
第二次,如果 name 或 email 没有改变,服务器将不能再生成页面。
ETag头提供表示页面内容的哈希值。如果页面被改变时,哈希也将被改变。
第2步 - 要启用发送ETag头,需要配置 yii\filters\HttpCache::$etagSeed 属性。
public function behaviors() {
   return [
      [
         'class' => 'yii\filters\HttpCache',
         'only' => ['index'],
         'etagSeed' => function ($action, $params) {
            $user = $this->findModel(\Yii::$app->request->get('id'));
            return serialize([$user->name, $user->email]);
         },
      ],
   ];
}
在上面的代码中,我们只有在 index 动作中启用了HTTP缓存。
应生成基于用户的 name 和 email 表头的 HTTP 标头。
当浏览器中首次打开 index 动作页面,则在服务器端生成内容并发送给浏览器端。
第二次,如果 name 或 email 没有改变,服务器将不能再生成页面。


上一篇: Yii缓存 下一篇: Yii别名(Aliases)