PHP template engine Smarty Cache usage

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

Everyone should know that proper use of the cache can effectively alleviate the server's server pressure. php Smarty is a very good php template engine. It provides us with a very simple and diverse cache operation. Let's learn about the smarty cache operation. Some tips

Here we will analyze how to open and use the smarty cache, how to clear the smarty cache and smarty's global cache, partial cache, local cache three cache mechanisms.

First, open and use smarty cache

To enable smarty's cache, simply set caching to true in the smarty setting 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 the second parameter cache_id to the display or fetch, such as:

$smarty->display('index.tpl',$my_cache_id);

This feature can be used to cache different $_GETs differently.

Second, clear the smarty cache

There are several ways to clear the smarty cache:

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 specified template specified id cache

Third, the global cache

The global cache is to generate a cache page for all pages of the entire site. Setting the global cache First, we need to operate the smarty configuration file, enable the cache, specify the cache file directory, and set the cache lifetime. The parameters are as follows:

$smarty->cache_dir='./cache/'; //Set the folder where the cache file is stored
$smarty->caching=1;//Open cache 0, FALSE means off|Non 0 digits, TRUE means open
$smarty->cache_lifetime=3600//in seconds (if you fill in -1 never expires)

Next we have to go to the specific php page to set the name of the specific cache file corresponding to it, in the php page can be written as follows:

$smarty->cache_dir='./cache/'; //Set the folder where the cache file is stored
$smarty->caching=1;//Open cache 0, FALSE means off|Non 0 digits, TRUE means open
$smarty->cache_lifetime=3600//in seconds (if you fill in -1 never expires)
have to be aware of is:
$smarty->display('Template file name corresponding to it', 'Additional part of cache file name') This method.

The second parameter is not required. If it is not written, the cache file name is the file whose name is encrypted after the template file name. But this will encounter a more difficult problem, such as:

$smarty->display('Template file name corresponding to it', 'Additional part of cache file name') This method.

These two URLs correspond to different content, but the generated cache file name is the result of the article encryption. This will cause the user to query for different content, but access the same cache file. So it is recommended to add an auxiliary parameter, will access the url (including all the parameters behind the ?) md5 encryption is a more reliable approach.

Fourth, partial cache

Partial caching is to specify some files to generate a cache file, not all files on the website. The essence of the partial cache is actually that some of the cache is not cached, that is, instead of specifying which files generate the cache, it is specified that some files do not generate a cache. Now suppose there are 3 files:

Http://www.jb51.net/index.php //requires caching
Http://www.jb51.net/cate.php //requires caching
Http://www.jb51.net/article.php //No need to cache

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

But in the third file we have to specify that there is no need to generate a cache. The specific method is to write the following code before the display specifies the template:

Http://www.jb51.net/index.php //requires caching
Http://www.jb51.net/cate.php //requires caching
Http://www.jb51.net/article.php //No need to cache
Of course, $smarty->display('the corresponding template file name') still needs to be written, and I don't want to generate the cache. The second parameter is not needed.

Note: The parameters of $smarty->clear_cache() and $smarty->display() must be written consistently.

Five, local cache

Local cache is to specify some local place generation cache under the same page. In fact, it does not specify which local generated cache, but which parts do not generate a cache (this is similar to the partial cache operation idea). Let us first look at the following example:

The contents of the article.php file are as follows:

<?php

$time=time();


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


Function insert_timeget(){


Return time();


}


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


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

<?php

$time=time();


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


Function insert_timeget(){


Return time();


}


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


Here is an explanation of the above example: in PHP we only need to define a custom function whose function name is insert_ (note underscore) custom supplement name, and the value returned in it does not need to be passed by the assign() method. Called in the template page as {insert name='custom supplemental name'}, while not being affected by the cache, but refreshed in real time.

Another added point is that if you feel that using Smarty cache is not cool enough, you can also use a custom cache, how to use a custom cache?

Here you need to set cache_handler_func to use a custom function to handle the cache, such as:



$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 cached 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.