PHP template engine Smarty Cache usage Summary _php template

Source: Internet
Author: User
Tags md5 md5 encryption php template

Here we will analyze how to open and use the Smarty cache, how to clear the Smarty cache and Smarty Global cache, partial caching, local caching three kinds of caching mechanism.

First, open and use the Smarty cache

To turn on the Smarty cache, simply set the caching to true in the Smarty settings parameter and specify Cache_dir. Also set the Cache_lefetime parameter to specify the cache lifetime (in seconds). If you want to generate multiple different caches for the same page, you can add a second parameter cache_id to display or fetch, such as:

Copy Code code as follows:
$smarty->display (' Index.tpl ', $my _cache_id);

This attribute can be used for different caching of different $_get.

Second, clear the Smarty cache

There are several ways to clear the Smarty cache:

Copy Code code as follows:

Clear_all_cache ()//Clear All caches
Clear_cache (' index.tpl ')//clear cache for specified template Index.tpl
Clear_cache (' Index.tpl ', cache_id)//clear cache of specified ID for specified template

Third, global cache

Global caching is the generation of cached pages for all pages of the entire Web site. Set global cache First we want to manipulate the Smarty configuration file, open the cache, specify the cache file directory, and set the cache's survival time, set the parameters as follows:

Copy Code code as follows:

$smarty->cache_dir= './cache/'; Set up a folder where cached files are stored
$smarty->caching=1;//turn on cache 0, false on behalf of off | Not 0 digits, true to open
$smarty->cache_lifetime=3600//Unit is seconds (if 1 is never expired)

Next we have to go to the specific PHP page settings corresponding to the specific cache file name, in the PHP page can be written as follows:

Copy Code code as follows:
$smarty->display (' list2.html ', MD5 ($_server[' Request_uri '));//To MD5 encrypt the URL of the current page (including all parameters that follow) and then set the cached file name

It is to be noted that:

Copy Code code as follows:

$smarty->display (' corresponding template filename ', ' Supplemental part of cached file name ') this method.

The second parameter is not required, and if not, the cached filename is the file that the template file name is encrypted with. But there is a tricky problem, such as:

Copy Code code as follows:

Http://www.jb51.net/article.php?id=5
Http://www.jb51.net/article.php?id=7

The two URLs correspond to different content, but the resulting cached file name is the result of article encryption. This results in the user wanting to query for different content and accessing the same cached file. So it is recommended that you add a secondary parameter that will access the URL (including all subsequent parameters) MD5 encryption is a more reliable approach.

Four, partial cache

Partial caching Specifies some file generation cache files, not all of the Web site's files. The real part of the cache is actually partially not cached, that is, not specifying which files generate the cache, but rather specifying that some files do not generate a cache. Now suppose there are 3 files:

Copy Code code as follows:

http://www.jb51.net/index.php//cache required
http://www.jb51.net/cate.php//cache required
http://www.jb51.net/article.php//No caching required

The previous two files in the PHP file still need to write $smarty->display (' corresponding template file name ', ' the supplemental part of the cache file name ') this sentence.

But in the third file, we specifically point out that there is no need to generate a cache, by writing the following code before the display specifies the template:

Copy Code code as follows:

$smarty->clear_cache (' corresponding template filename ');//In fact, before or after $smarty->display (' corresponding template filename ')

Of course $smarty->display (' corresponding template filename ') or to write, do not want to generate the cache, the second parameter will not need.

Note: The parameters of the $smarty->clear_cache () and $smarty->display () must be written in a consistent

Five, local cache

Local caching is the designation of certain local build caches under the same page. Instead of specifying which local build cache it is, it does not generate the cache (which is similar to the partial caching operation Idea). Let's take a look at one of the following examples:

The article.php file section reads as follows:

Copy Code code as follows:

<?php

$time =time ();

$smarty->assign (' time ', $lanmuarr);

function Insert_timeget () {

return time ();

}

$smarty->display (' article.html ');

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

Copy Code code as follows:

<body>

{$time}<!--turn on caching, the value will not change-->

{Insert Name= ' timeget '}< The value will change after the cache is turned on!---->

</body>

Here is an explanation of the above example: in PHP, we just define a custom function named Insert_ (note underline) custom supplemental name, in which the value returned, do not need assign () method pass, 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.

Plus, if you feel it's not cool to use the Smarty cache, you can also use custom caching, how do you use custom caching?

Here you need to set up Cache_handler_func to use a custom function to process the cache, such as:

Copy Code code as follows:

$smarty->cache_handler_func= "Mycache";

function Mycache ($action,& $smarty _obj,& $cache _content, $tpl _file=null, $cache _id=null, $compile _id=null) {

Switch ($action) {

Case "read"://Read cache content

Case "Write"://write Cache

Case "clear"://Empty

}

}


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.