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.