Yii Fragment Caching - Yii

What is Yii Fragment Caching?

The caching of a fragment of a web page is provided by Fragment Caching.

Step 1 − A new function actionFragmentCaching() is added to the SiteController.

A new user is created and a view file cachedview is displayed.

Step 2 − A new file cachedview.php is created in the views/site folder.

A content generation logic is enclosed in a pair of beginCache() and endCache() methods. The beginCache() method will renderthe content that is found in cache.

Step 3 – Visit the URL http://localhost:8080/index.php?r=site/fragment-caching and reload the page. The output appears as:

Fragment Caching

Caching is done for the content between beginCache() and endCache() methods. 13 users are there in database, but only 12 are displayed.

What is Yii Page Caching?

Yii Page caching facilitates in caching the content of the complete web page. It is supported by yiifilterPageCache.

Step 1 − The behaviors() function is modified in the SiteController

The above code caches the index page for 60 seconds.

Step 2 – Visit the URL http://localhost:8080/index.php?r=site/index. Then, modify the congratulation message of the index view file. If the page is reloaded, no changes are noticed as the page is cached for 60 seconds. Wait for 60 seconds and reload the page.

Page Caching

What is Yii HTTP Caching?

The client-side caching can also be used by the web applications. For using the client-side caching, yiifilterHttpCache filter need to be configured for controller actions.

The Last-Modified header uses a timestamp to indicate whether the page has been modified.

Step 1 − To enable sending the Last-Modified header, configure the yiifilterHttpCache::$lastModified property.

The HTTP caching is enabled for the index page. When the index page is opened for the first time, it is generated on server side and then sent to the browser. If no news is created, then second time 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.

Step 2 − To enable sending the Etag header, configure the yiifiltersHttpCache::$etagSeed property.

By the above code, the HTTP caching for the index action is generated. It should generate the Etag HTTP header based on the name and email of the user. When a index page is opened for the first time by the browser, the page is generated on server side and sent to the browser. The second time, if no changes to either name or email, the server will not regenerate the page.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Yii Topics