PHP template engine Smarty Cache usage

Source: Internet
Author: User
Tags php template
    1. $smarty->display (' Index.tpl ', $my _cache_id);
Copy Code

This attribute can be used to make different caches for different $_get.

Second, clear the Smarty cache clear Smarty cache is generally the following ways:

    1. Clear_all_cache ();//Clear All caches
    2. Clear_cache (' Index.tpl ');//clears the cache for the specified template Index.tpl
    3. Clear_cache (' Index.tpl ', cache_id);//clear cache for specified template specified ID
Copy Code

Global cache Global caching is the generation of cached pages for all pages of the entire site. Setting the global cache first we will operate the Smarty configuration file, open the cache, specify the cache file directory, and set the cache survival time, set the parameters as follows: $smarty->cache_dir= './cache/'; Set the folder where the cache files are stored $smarty->caching=1;//turn on cache 0, false for off | Not 0 digits, true to open $smarty->cache_lifetime=3600//units in seconds ( If you fill in 1 to never expire, then we will go to the specific PHP page to set the corresponding specific cache file name, in the PHP page can be written as follows:

    1. $smarty->display (' list2.html ', MD5 ($_server[' Request_uri '));//MD5 encrypt the URL of the current page (all parameters included) and set the cache file name
Copy Code

Note: This method is $smarty->display (' corresponding template file name ', ' Supplemental portion of cache file name ').

The second parameter is not required, and if it is not, the cached file name is the files that are encrypted after the template file name is processed. But this can come across a tricky issue, such as: http://bbs.it-home.org/article.php?id=5http://bbs.it-home.org/article.php?id= 7 The two URLs correspond to the different content that should be, but the generated cache file name is the result of article encryption. This causes the user to query for different content and access the same cache file. So it's a good idea to add an auxiliary parameter that will access the URL (all parameters that contain the following) MD5 encryption.

Part of the cache is to specify some file generation cache files, not all the Web site files. The essence of partial caching is actually partial not caching, that is, not specifying which files generate the cache, but specifying that the specific files do not generate the cache. Now suppose there are 3 files: http://bbs.it-home.org/index.php//Need cache http://bbs.it-home.org/cate.php//need cache http://bbs.it-home.org/ article.php///Do not need to cache the previous two files in the PHP file or need to write on the $smarty->display (' corresponding template filename ', ' Cache file name Supplement section ') this sentence. However, in the third file we would like to specifically indicate that there is no need to generate a cache, the specific method is to write the following code before the display specifies the template: $smarty->clear_cache (' corresponding template filename ');//actually written in $smarty-> Display (' Corresponding template file name ') before or after all can of course $smarty->display (' corresponding template file name ') or to write, originally did not want to generate a cache, the second parameter is not required. Note: The parameters of the $smarty->clear_cache () and $smarty->display () must be written in a consistent

Local cache local cache is to specify some local local generation cache under the same page. In fact, it is not specifying which local build cache, but which local does not generate the cache (this is similar to the partial caching operation Idea). Let's take a look at the following example: Article.php file part of the content:

    1. $time =time ();
    2. $smarty->assign (' time ', $lanmuarr);
    3. function Insert_timeget () {
    4. return time ();
    5. }
    6. $smarty->display (' article.html ');
Copy Code

The corresponding template file article.html part of the code:

    1. {$time}
    2. {Insert name= ' timeget '}
Copy Code

Here's an explanation of the above example: in PHP we simply define a custom function named Insert_ (note underscore) to customize the supplemental name, and the value returned in it does not need to be passed by the Assign () method, and can be directly in the template page with {insert Name= ' The custom supplemental Name '} is called in a way that is not affected by the cache and is refreshed in real time.

If you feel that using the Smarty cache is not cool enough, you can also use a custom cache, how to use the custom cache?

You need to set Cache_handler_func to handle the cache using a custom function, such as:

    1. $smarty->cache_handler_func= "Mycache";
    2. function Mycache ($action,& $smarty _obj,& $cache _content, $tpl _file=null, $cache _id=null, $compile _id=null) {
    3. Switch ($action) {
    4. Case "read"://Read Cache contents
    5. Case "Write"://write Cache
    6. Case "clear"://Empty
    7. }
    8. }
Copy Code
  • 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.