Fragment caching provides caching of a fragment of a web page.
Step1Ā ā Add a new function calledĀ actionFragmentCaching()Ā to the SiteController.
public function actionFragmentCaching() { $user = new MyUser(); $user->name = "cached user name"; $user->email = "cacheduseremail@gmail.com"; $user->save(); $models = MyUser::find()->all(); return $this->render('cachedview', ['models' => $models]); }
In the above code, we created a new user and displayed a cachedview view file.
Step2Ā ā Now, create a new file calledĀ cachedview.phpĀ in theĀ views/siteĀ folder.
<?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(); ?>
We have enclosed a content generation logic in a pair of beginCache() and endCache() methods. If the content is found in cache, the beginCache() method will render it.
Step3Ā ā Go to the URLĀ http://localhost:8080/index.php?r=site/fragment-cachingĀ and reload the page. Following will be the output.
Notice, that the content between the beginCache() and endCache() methods is cached. In the database, we have 13 users but only 12 are displayed.
Page Caching
Page caching provides caching the content of a whole web page. Page caching is supported by yii\filter\PageCache.
Step1Ā ā Modify theĀ behaviors()Ā function of the SiteController.
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 ], ]; }
The above code caches the index page for 60 seconds.
Step2Ā ā Go to the URLĀ http://localhost:8080/index.php?r=site/index. Then, modify the congratulation message of the index view file. If you reload the page, you will not notice any changes because the page is cached. Wait a minute and reload the page again.
HTTP Caching
Web applications can also use client-side caching. To use it, you may configure the yii\filter\HttpCache filter for controller actions.
The Last-Modified header uses a timestamp to indicate whether the page has been modified.
Step1Ā ā To enable sending the Last-Modified header, configure the yii\filter\HttpCache::$lastModified property.
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'); }, ], ]; }
In the above code, we enabled the HTTP caching only for the index page. When a browser opens the index page for the first time, the page is generated on the server side and sent to the browser. The second time, if no news is created, the server will not regenerate the page.
The Etag header provides a hash representing the content of the page. If the page is changed, the hash will be changed as well.
Step2Ā ā To enable sending the Etag header, configure theĀ yii\filters\HttpCache::$etagSeedĀ property.
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]); }, ], ]; }
In the above code, we enabled the HTTP caching for the index action only. It should generate the Etag HTTP header based on the name and email of the user. When a browser opens the index page for the first time, the page is generated on the server side and sent to the browser. The second time, if there are no changes to the name or email, the server will not regenerate the page.