PHP and MySQL programming (version 3rd) -- 19.8 Cache

Source: Internet
Author: User
Tags processing instruction smarty template

Applications with powerful functions generally have high overhead, usually resulting from data acquisition and processing operations. For Web applications, this problem is caused by the stateless HTTP protocol. Because the HTTP protocol is stateless, the same operation must be performed repeatedly for each page request, regardless of whether the data is modified. This problem can be further deteriorated if applications are available in the world's largest network. So it's not surprising that people are always trying to make Web applications run more efficiently. There is a particularly effective solution for this, which is also one of the most reasonable solutions: Converting dynamic pages into static pages, and building them again only after the page content has been modified, or rebuild regularly. Smarty provides such a feature called page caching ). This section describes this feature and provides several examples.

Annotation "cache" is different from "compile" in two aspects. First, although "Compilation" reduces the overhead by converting the template to a PHP script, it still needs to perform the actions required to obtain data at the logic layer. "Cache" reduces the overhead at both levels, and does not need to repeatedly execute commands at the logic layer. In addition, it also converts the template content to static pages. Second, "compile" is enabled by default, and "cache" must be explicitly enabled by developers.

To use the cache, you must first set the cache attribute of Smarty to enable the cache, as shown below:

After caching is enabled, call the display () and fetch () Methods to save the content of the target template in the specified template (specified by the $ cache_dir attribute.

19.8.1 Process cache Lifecycle

The cached page is valid within the lifecycle (in seconds) specified by the $ cache_lifetime attribute. The default value is 3600 seconds, that is, 1 hour. Therefore, If You Want To modify this setting, you can set this attribute as follows:

In this object's life cycle, subsequent calls and cached templates use this life cycle.

It is possible to overwrite the previously set cache life cycle so that the cache life cycle of each template can be controlled separately. You can do this by setting the $ caching attribute to 2, as shown below:

Here, the life cycle of the news. tpl template is set to 20 minutes, which overwrites the previous global life cycle value.

19.8.2 PassIs_cached ()Eliminate processing overhead

As described earlier in this chapter, cache templates can also eliminate processing overhead. If cache is disabled (only compilation is enabled), these processing overhead will always occur. However, cache is not enabled by default. To enable caching, put the Processing Instruction in the if condition and execute the is_cached () method as follows:

In this example, the template news. tpl is verified first. If valid, the database access is skipped; otherwise, the database access is performed.

19.8.3 Create multiple caches for each template

Any specified Smarty template can be used to provide a general interface for the entire series of tutorials, news items, and blog items. Since the same template is used to generate different quantities of items, how do I cache multiple instances of a template? The answer is simpler than you think. The Smarty developer has actually solved this problem. You can use the display () method to assign a unique identifier to each instance of the cache template. For example, suppose there is a template used to generate a professional boxer biography and you want to cache each instance of this template:

Pay special attention to the following line:

This row has two functions for this script. On the one hand, it obtains boxerbio named $ _ GET ["boxerid. tpl Cache version. If the cache does not exist, use this name to cache the template instance. In this way, you can easily cache any number of instances for a specified template.

19.8.4 Conclusion about cache

Template caching greatly improves application performance. If you decide to integrate Smarty into the project, you should carefully consider caching. However, because most of the powerful Web application functions are reflected in their dynamic characteristics, on the one hand, we must consider performance improvement, and on the other hand, whether the cache page is still effective over time, we need to weigh between them. This section describes how to manage the cache life cycle of each page separately, and to execute some parts of the logic layer based on the specific cache validity. Make sure that these features are taken into account for each template.

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.