PHP template engine Smarty cache usage _ PHP Tutorial

Source: Internet
Author: User
Cache usage of the PHP template engine Smarty. 1. to enable and use the smarty cache, set caching to true in the smarty setting parameter and specify cache_dir. Set the cache_lefetime parameter to specify cache 1. Enable and use the smarty cache.

To enable the smarty cache, you only need to set caching to true in the smarty setting parameter and specify cache_dir. Set the cache_lefetime parameter to specify the cache survival time (in seconds ). To generate multiple different caches for the same page, you can add the second parameter cache_id to display or fetch, for example:

The code is as follows: $ smarty-> display ('index. tpl ', $ my_cache_id );

This feature can be used to cache different $ _ GET objects.

II. clear the smarty cache

There are several methods to clear the smarty cache:

The code is as follows:

Clear_all_cache (); // clear all caches

Clear_cache ('index. tpl '); // clear the cache of the specified template index. tpl

Clear_cache ('index. tpl ', cache_id); // clear the cache of the specified template id

III. global cache

Global cache generates cache pages for all pages of the entire website. To set the global cache, first we need to operate the smarty configuration file, enable the cache, specify the cache file directory, and set the cache survival time. the parameters are as follows:

The code is as follows:

$ Smarty-> cache_dir = './cache/'; // you can specify a folder for storing cached files.

$ Smarty-> caching = 1; // if the cache is enabled, 0 or FALSE indicate that the cache is disabled. | if the value is not 0, TRUE indicates that the cache is enabled.

$ Smarty-> cache_lifetime = 3600 // unit: Seconds (if-1 is set to never expire)

Next we will go to the specific php page to set the corresponding name of the specific cache file. the php page can be written as follows:

The code is as follows: $ smarty-> display('list2.html ', md5 ($ _ SERVER ['request _ URI']); // add the URL of the current page (including? All subsequent parameters), perform md5 encryption, and then set the cache file name.

Note that:

The code is as follows:

$ Smarty-> display ('corresponding template filename ', 'supplemental part of cache filename.

The second parameter is not required. if it is not written, the cached file name is the encrypted file of the template file name. However, this will cause a difficult problem, for example:

The code is as follows:

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

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

The two URLs correspond to different contents, but the generated cache file names are the results of article encryption. This results in the user wanting to query different contents, but accessing the same cache file. Therefore, we recommend that you add a secondary parameter to access the url (including? All parameters later) md5 encryption is a more reliable method.

IV. partial cache

Some caches specify some files to generate cache files, rather than all files on the website. The essence of partial cache is that some files are not cached. that is to say, instead of specifying which files are generated for the cache, some specific files are not generated for the cache. Assume that there are three files:

The code is as follows:

Http://www.jb51.net/index.php // need to cache

Http://www.jb51.net/cate.php // need to cache

Http://www.jb51.net/article.php // no cache required

In the php file of the first two files, you still need to write $ smarty-> display ('corresponding template filename', 'supplemental part of cache filename.

However, in the third file, we need to specify that the cache does not need to be generated. the specific method is to write the following code before the template is specified by display:

The code is as follows:

$ Smarty-> clear_cache ('corresponding template filename '); // it can be written before or after $ smarty-> display ('corresponding template filename ').

Of course, $ smarty-> display ('Template file name' corresponding to it) still needs to be written, so we didn't want to generate the cache, and the second parameter is not needed.

Note: the parameters of $ smarty-> clear_cache () and $ smarty-> display () must be consistent.

5. local cache

A local cache is used to generate a cache in some parts of the same page. In fact, it does not specify the local cache generation, but the local cache generation does not (this is similar to the operation idea of some caches ). Let's take a look at the following example:

The content of the article. php file is as follows:

The code is as follows:

$ Time = time ();

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

Function insert_timeget (){

Return time ();

}

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

The code for the template file article.html is as follows:

The code is as follows:

{$ Time}

{Insert name = 'timeget '}

Here we will explain the above example: in PHP, we only need to define a UDF named insert _ (note the underline) to customize the name of the UDF, in which the returned value, you can directly call the assign () method on the template page using {insert name = 'custom supplemental name'} without being affected by the cache, and real-time refresh.

In addition, if you feel that the Smarty cache is not good enough, you can also use the custom cache. how can you use the custom cache?

Here we need to set cache_handler_func to use a custom function to process the cache, for example:

The code is 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 the cached content

Case "write": // write cache

Case "clear": // clear

}

}

To enable the smarty cache, set caching to true in the smarty setting parameter and specify cache_dir. At the same time, set the cache_lefetime parameter to specify the cache...

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.