Google News
logo
Yii framework - Interview Questions
What is Fragment Caching in Yii framework?
Fragment caching refers to caching a fragment of a Web page. For example, if a page displays a summary of yearly sale in a table, you can store this table in cache to eliminate the time needed to generate this table for each request. Fragment caching is built on top of data caching.
 
To use fragment caching, use the following construct in a view :
if ($this->beginCache($id)) {

    // ... generate content here ...

    $this->endCache();
}
That is, enclose content generation logic in a pair of beginCache() and endCache() calls. If the content is found in the cache, beginCache() will render the cached content and return false, thus skip the content generation logic. Otherwise, your content generation logic will be called, and when endCache() is called, the generated content will be captured and stored in the cache.
Advertisement