Home > Backend Development > PHP Tutorial > Teach you step by step Smarty caching technology_PHP tutorial

Teach you step by step Smarty caching technology_PHP tutorial

WBOY
Release: 2016-07-21 14:51:41
Original
806 people have browsed it

Everyone should know that the caching mechanism can effectively reduce the server pressure of the website. SmartyOne of the highlights of the template engine is that it provides us with very simple caching operations. , let us learn about it below.


First of all, we need to know that the Smarty cache mechanism is divided into three types: global cache, partial cache, and local cache. We will introduce them one by one.


1. Global cache


As the name suggests, global caching generates cached pages for all pages of the entire website.


First we need to operate the smarty configuration file, enable caching, specify the cache file directory, and set the cache survival time


$smarty->cache_dir = ‘./cache/’; //Set the folder to store cache files


$smarty->caching = 1; //Turn on caching 0, FALSE means off | Non-0 number, TRUE means on


$smarty->cache_lifetime = 3600//The unit is seconds (if you fill in -1, it will never expire)


Next we have to go to the specific php page to set the name of the specific cache file corresponding to it


$url=md5($_SERVER['REQUEST_URI']); //Encrypt the URL of the current page (including all parameters after ?) with md5


$smarty->display(‘list2.html’, $url); //Set cache file name


Things to note:


$smarty->display('corresponding template file name', 'supplementary part of the cache file name') this method.


The second parameter is not required. If not written, the cache file name will be the name of the encrypted template file.


But this will encounter a more difficult problem:


http://localhost/1.10/sm/list2.php? lan=1


http://localhost/1.10/sm/list2.php? lan=2


http://localhost/1.10/sm/list2.php? lan=3


The three URLs correspond to different contents, but the generated cache file names are all the encrypted results of list2.html.


This will cause the user to query different content but access the same cache file.


Therefore, it is recommended to add an auxiliary parameter to encrypt the access URL (including all parameters after?) md5, which is recommended by the author.


2. Partial cache


First of all, understand what partial caching is. It actually means specifying some files to generate cache files, not all files on the website.


Now that we understand the effect we need to achieve, let’s do the specific operations


Before operating, we must first emphasize a concept:


The essence of partial caching is actually partial non-caching, which means that instead of specifying those files to generate cache, it specifies specific files not to generate cache


Assume there are 3 files:


1.php //needs caching


2.php //needs caching


3.php //No caching required


In the 1.php/2.php file, still write the $smarty->display('corresponding template file name', 'supplementary part of the cache file name') method.


But in 3.php we have to specifically specify that there is no need to generate a cache. The specific method is:


$smarty->clear_cache(‘the corresponding template file name’)//Written before or after $smarty->display(‘the corresponding template file name’)


Of course, $smarty->display('corresponding template file name') still needs to be written. We don't want to generate a cache, so the second parameter is not needed.


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


3. Local cache


First, let us understand the meaning of local caching, which is to specify certain local places under the same page to generate cache.


Similarly, we have to reverse our thinking here.


In fact, it is not to specify which parts generate cache, but which parts do not generate cache (this is similar to the operation idea of ​​partial cache).


Without further ado, let me give you an example


1.php


$time=time();


$smarty->assign(‘time’, $lanmuarr);


function insert_timeget()


{


return time();


}


$smarty->display(’1.html’);


1.html


{$time}


//After turning on the cache, this will not change if you refresh it repeatedly


{insert name=’timeget’}


//After turning on the cache, repeatedly refreshing this will change


After understanding this example, let’s explain the principle


In PHP we only need to define


The function name is insert_custom supplementary name. The value returned in it does not need to be passed by the assign() method. It can be called directly in the template page as {insert name='custom supplementary name'}. , and will not be affected by caching and refresh in real time


Okay, now we have finished explaining all the three caching methods of smarty. Friends who are interested can try different effects by themselves.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371728.htmlTechArticleEveryone should know that the caching mechanism can effectively reduce the server pressure of the website. One of the highlights of the Smarty template engine is that it We provide a very simple caching operation, let me...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template