Yii Framework official guide series 30-cache: FragmentCaching)

Source: Internet
Author: User
Fragment caching refers to caching a webpage clip. For example, if a page displays the annual sales summary in the table, we can store the table in the cache to reduce the time for each request to be re-generated. To use fragment cache, go to the controller view foot...



Fragment caching refers to caching a webpage clip. For example, if a page displays the annual sales summary in the table, we can store the table in the cache to reduce the time for each request to be re-generated.

To use the segment cache, call CController: beginCache () and CController: endCache () in the controller view script (). The page content included in the start and end of the two methods will be cached. Similar to data caching, we need a number to identify cached fragments.


Other HTML contents...
 BeginCache ($ id) {?>... Cached content...
 EndCache () ;}?>... Other HTML content...

In the preceding example, if beginCache () returns false, the cached content is automatically inserted here. otherwiseifThe content in the statement will be executed and cached when the endCache () is triggered.

1. cache Options)

When beginCache () is called, an array composed of cache options can be provided as the second parameter to customize fragment caching. In fact, for convenience, the beginCache () and endCache () methods are the packaging of [COutputCache] widgets. Therefore, all attributes of COutputCache can be initialized in the cache options.

Validity period (Duration)

Perhaps the most common option is duration, which specifies how long the content will be effective in the cache. It is similar to the CCache: set () expiration parameter. The following code caches content segments for at most one hour:


Other HTML contents...
 BeginCache ($ id, array ('duration' => 3600) {?>... Cached content...
 EndCache () ;}?>... Other HTML content...

If we do not set the period, the default value is 60, which means that the cache content will be invalid after 60 seconds.

Dependency)

Like data caching, content fragments can also be cached. For example, whether the content of an article is displayed depends on whether it is modified.

To specify a dependency, we have established the dependency option, which can be an ICacheDependency object or a configuration array that can be used to generate dependency objects. The following code specifies that the part content depends onlastModifiedWhether the column value changes:


Other HTML contents...
 BeginCache ($ id, array ('dependency '=> array ('class' => 'system. caching. dependencies. CDbCacheDependency ',' SQL '=> 'Select MAX (lastModified) FROM Post') {?>... Cached content...
 EndCache () ;}?>... Other HTML content...

Variation)

The cached content varies according to some parameters. For example, each person has different archives. The cached file content varies according to each person's ID. This means that different IDs are used when beginCache () is called.

COutputCache is built in with this feature, so programmers do not need to write a pattern that changes content based on the ID. The following 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 cache content.

  • VaryBySession: set this option to true. the cached content changes according to the session ID. Therefore, each user session may see different content provided by the cache.

  • VaryByParam: Set the name in the array of this option. the cached content changes according to the value of the GET parameter. For exampleidYou can specify varyByParamarray('id')So that we can cache the content of each article. Without such changes, we can only cache an 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.

Request type

Sometimes, we want the fragment cache to only enable some types of requests. For example, for a form displayed on a webpage, we only want to cache the initially requested form (through the GET request ). Any form subsequently displayed (via POST request) will not be cached because the form may contain user input. To do this, we can specify the requestTypes option:


Other HTML contents...
 BeginCache ($ id, array ('requesttypes '=> array ('get') {?>... Cached content...
 EndCache () ;}?>... Other HTML content...

2. Nested cache (Nested Caching)

Fragment cache can be nested. That is to say, a cache segment is attached to a larger segment cache. For example, the comments are cached in the internal segment, and they are cached in the article content together in the external cache.


Other HTML contents...
 BeginCache ($ id1) {?>... Externally cached content...
 BeginCache ($ id2) {?> Contents cached inside...
 EndCache () ;}?>... Externally cached content...
 EndCache () ;}?>... Other HTML content...

You can set different cache options for nested caching. For example, in the above example, you can set the duration of the internal cache and the external cache. When data is stored in the external cache, the internal cache can still provide valid internal segments. However, vice versa. If the external cache contains valid data, it will always keep the cached copy even if the internal cache in the content has expired.

The above is the content of the Yii Framework official guide series 30-cache: Fragment Caching. For more information, see The PHP Chinese website (www.php1.cn )!

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.