Home > Article > Backend Development > Yii Framework Official Guide Series 30 - Caching: Fragment Caching
Fragment caching refers to caching a certain fragment of a web page. For example, if a page displays annual sales summaries in a table, we can store this table in the cache, reducing the time it needs to be regenerated for each request.
To use fragment cache, call CController::beginCache() and CController::endCache() in the controller view script. Both methods start and end including the page content that will be cached. Similar to data caching, we need a number to identify the cached fragment.
...别的HTML内容... <?php if($this->beginCache($id)) { ?> ...被缓存的内容... <?php $this->endCache(); } ?> ...别的HTML内容...
In the above, if beginCache() returns false, the cached content is automatically inserted in this place; otherwise, The content within the if
statement will be executed and cached when endCache() is triggered.
When calling beginCache(), you can provide an array consisting of caching options as the second parameter to customize the fragment cache. In fact, for convenience, the beginCache() and endCache() methods are wrappers of the [COutputCache] widget. Therefore all properties of COutputCache can be initialized in cache options.
Perhaps the most common option is duration, which specifies how long the content will be valid in the cache. It is somewhat similar to CCache::set() expiration parameter. The code below caches content snippets for up to one hour:
...其他HTML内容... <?php if($this->beginCache($id, array('duration'=>3600))) { ?> ...被缓存的内容... <?php $this->endCache(); } ?> ...其他HTML内容...
If we don’t set a period, it will default to 60 , which means that cached content will be invalid after 60 seconds.
Like data caching, content fragments can also have dependencies when they are cached. For example, the content of an article is displayed depending on whether the article has been modified.
To specify a dependency, we create the dependency option, which can be an object that implements ICacheDependency or a configuration array that can be used to generate dependency objects. The following code specifies that the fragment content depends on whether the value of the lastModified
column changes:
...其他HTML内容... <?php if($this->beginCache($id, array('dependency'=>array( 'class'=>'system.caching.dependencies.CDbCacheDependency', 'sql'=>'SELECT MAX(lastModified) FROM Post')))) { ?> ...被缓存的内容... <?php $this->endCache(); } ?> ...其他HTML内容...
The cached content can vary according to some parameters. For example, everyone’s profile is different. The cached profile content will change based on each person ID. This means that a different ID will be used when beginCache() is called.
COutputCache has this feature built in, and programmers do not need to write a pattern that changes content based on ID. Below is a summary.
varyByRoute: Set this option to true, and the cached content will change according to the route. Therefore, each controller and action combination will have a separate cached content.
varyBySession: Set this option to true and the cached content will change based on the session ID. Therefore, each user session may see different content served by the cache.
varyByParam: Set the name in the array of this option. The cached content will change according to the value of the GET parameter. For example, if a page displays article content based on the id
GET parameter, we can specify varyByParam as array('id')
to enable us to cache each article content. Without such changes, we would only be able to cache a certain article.
varyByExpression: by setting this option to a PHP expression, we can make the cached content to be variated according to the result of this PHP expression. This option has been available since version 1.0. 4.
Sometimes, we want fragment caching to be enabled only for certain types of requests. For example, for a form to be displayed on a certain web page, we only want to cache the initially requested form (via a GET request). Any form that is subsequently displayed (via a POST request) will not be cached, as the form may contain user input. To do this, we can specify the requestTypes option:
...其他HTML内容... <?php if($this->beginCache($id, array('requestTypes'=>array('GET')))) { ?> ...被缓存的内容... <?php $this->endCache(); } ?> ...其他HTML内容...
Fragment caches can be nested. That is, a cache fragment is attached to a larger fragment cache. For example, comments are cached in the internal snippet cache, and they are cached together in the external cache with article content.
...其他HTML内容... <?php if($this->beginCache($id1)) { ?> ...外部被缓存内容... <?php if($this->beginCache($id2)) { ?> ...内部被缓存内容... <?php $this->endCache(); } ?> ...外部被缓存内容... <?php $this->endCache(); } ?> ...其他HTML内容...
Nested caching can set different caching options. For example, in the example above, the internal cache and the external cache can be set to different duration values. When data stored in the external cache is invalidated, the internal cache can still serve valid internal fragments. However, the reverse is not possible. If the external cache contains valid data, it will keep a cached copy forever, even if the content in the internal cache has expired.
The above is the content of Yii Framework Official Guide Series 30 - Caching: Fragment Caching. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!