Yii片段快取


片段快取提供商網頁的一個片段快取。
第1步 - 新增一個新的 actionFragmentCaching()方法到 SiteController 控制器中。
public function actionFragmentCaching() {
   $user = new MyUser();
   $user->name = "cached user name";
   $user->email = "[email protected]";
   $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 沒有改變,伺服器將不能再生成頁面。