Yii Framework Official Guide Series 30--cache: Fragment cache (Fragment Caching)

Source: Internet
Author: User
Tags yii



Fragment caching refers to caching a fragment of a webpage. For example, if a page shows an annual sales summary in a table, we can store this table in the cache, reducing the time that each request needs to be re-generated.

To use fragment caching, call Ccontroller::begincache () and Ccontroller::endcache () in the Controller view script. Both methods start and end of the included page content will be cached. Like data caching, we need a number to identify the cached fragment.


... Other HTML content ... <?php if ($this->begincache ($id)) {... Cached content ... <?php $this->endcache (); } ..... Other HTML content ...

In the above, if Begincache () returns false, the cached content is automatically inserted in this place; Otherwise, the if contents within the statement are executed and cached when the Endcache () is triggered.

1. Cache option (Caching options)

When calling Begincache (), you can provide an array consisting of cache options as a second parameter to customize the fragment cache. In fact, for convenience, the Begincache () and Endcache () methods are [Coutputcache]widget packaging. Therefore, all properties of Coutputcache can be initialized in the cache option.

Validity period (Duration)

Perhaps the most common option is duration, which specifies how long the content is valid in the cache. is a bit similar to the Ccache::set () expiration parameter. The following code caches the content fragment for up to one hour:


... Other HTML content ... <?php if ($this->begincache ($id, Array (' duration ' =>3600))} {... Cached content ... <?php $this->endcache (); } ..... Other HTML content ...

If we don't set the deadline, it will default to 60, which means the cached content will be invalid after 60 seconds.

Dependency (Dependency)

Like data caching, content fragments are cached and can be relied upon. For example, the content of an article is displayed depending on whether the article was modified.

To specify a dependency, we established the dependency option, which can be an object that implements Icachedependency or an array of configurations that can be used to build dependent objects. The following code specifies that the fragment content depends on lastModified whether the value of the column changes:


... Other HTML content ... <?php if ($this->begincache ($id, Array (' dependency ' =>array (        ' class ' = = ') System.caching.dependencies.CDbCacheDependency ',        ' sql ' = ' SELECT MAX (lastmodified) from Post '))) Cached content ... <?php $this->endcache (); } ..... Other HTML content ...

Change (Variation)

The contents of the cache can vary according to some parameters. For example, everyone's files are different. The cached archive content will vary according to the ID of each person. This means that when Begincache () is called, a different ID will be used.

This feature is built into the coutputcache, and programmers do not need to write patterns that change content based on their IDs. The following is a summary.

    • Varybyroute: Set this option to true, the cached content will vary according to the route. Therefore, the combination of each controller and action will have a separate cached content.

    • Varybysession: Set this option to true, the cached content will vary according to the session ID. Therefore, each user session may see different content that is provided by the cache.

    • VaryByParam: Sets the name of this option in the array, and the cached content is changed according to the value of the get parameter. For example, if a page displays the contents of the article based on id The get parameters, we can specify VaryByParam as array('id') so that we can cache each article content. Without such a change, we can only cache an article.

    • Varybyexpression:by Setting this option to a PHP expression, we can make the cached content to is variated according to T He result of this PHP expression. This option has been available since version 1.0.4.

Request type

Sometimes, we want the fragment cache to be enabled only for certain types of requests. For example, to display a form on a page, we only want to cache the initially requested form (via GET request). Any form that is subsequently displayed (via a POST request) will not be cached because the form may contain user input. To do this, we can specify the Requesttypes option:


... Other HTML content ... <?php if ($this->begincache ($id, Array (' Requesttypes ' =>array (' GET '))) {... Cached content ... <?php $this->endcache (); } ..... Other HTML content ...

2. Nested cache (Nested Caching)

Fragment caches can be nested. This means that a cache fragment is attached to a larger fragment cache. For example, comments are cached in the internal fragment cache, and they are cached in the article content together in the external cache.


... Other HTML content ... <?php if ($this->begincache ($id 1)) {... Externally cached content    ... <?php if ($this->begincache ($id 2)) {?>    ... Internal cached content    ... <?php $this->endcache (); } ..... Externally cached content ... <?php $this->endcache (); } ..... Other HTML content ...

Nested caches can set different cache options. For example, in the example above, the internal cache and the external cache can be set to a duration of different duration values. When the data store is not valid in an external cache, the internal cache can still provide valid internal fragments. However, the reverse is not. If the external cache contains valid data, it will always keep a cached copy, even if the internal cache in the content has expired.

The above is the Yii Framework Official Guide Series 30--cache: Fragment cache (Fragment Caching) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.