Home >Backend Development >PHP Tutorial >Smarty configuration and advanced caching technology_PHP tutorial
Foreword
Smarty is an excellent PHP template engine that separates logic code and user interface.
When learning and using Smarty, it is a big loss not to apply its caching technology. It can cache the HMTL file that the user finally sees into a static HTML page. When the cache attribute of Smarty is set to true, in Smarty Within the set cachetime period, the user's WEB request is directly converted into this static HTML file. This is equivalent to calling a static HTML file, which reduces a lot of burden on the backend server.
Download and configure
Official download: Smarty Download
After downloading, unzip it to the file directory of your project.
1 require('../libs/Smarty.class.php');
2
3 $smarty = new Smarty;
4
5 //$smarty->force_compile = true; //Force compilation
6 $smarty->debugging = true; //Debugging
7 $smarty->caching = true; //Enable caching
8 $smarty->cache_lifetime = 120; //Cache survival time (seconds)
$smarty->cache_dir = MY_SMARTY_DIR . '/cache/' ; //Set the cache storage path
Note: If you find that cache files change every time you browse, take a look at Smarty's force_compile , which forces Smarty to (re)compile the template every time it is called. This setting is not restricted by $compile_check. By default, it is disabled. It is very convenient for development and debugging. But it must not be used in a production environment. If caching is enabled, the cache file will be regenerated each time.
$smarty->force_compile = false; //Force compilation
Smarty caching technology
• Global Cache
• Partial caching
1. insert method
2. Dynamic block method
3. Plug-in block method
Global caching technology
As the name suggests, global caching is to generate a cache file for the entire page, specify the survival time of the cache file, and browse the entire page again within the specified time, and the cache file will be read directly.
$smarty->caching = true; //Enable caching
$smarty->cache_lifetime = 120; //Cache survival time (seconds)
Note: A template can only have one cache file. If your template has multiple pages, you must set an ID for the cache. For example, a page has multiple articles:
http://website/index.php?p=1
http://website/index.php?p=2
//$_SERVER['REQUEST_URI'] method
//Encrypt the URL of the current page (including all parameters after ?) with md5
$url=md5($_SERVER['REQUEST_URI']);
//Set cache file name
$smarty->display('index.tpl',$url);
Key point: A big reason for using caching technology is to reduce reading and writing to the database, so we need to use $smarty->isCached('index.tpl') to determine whether the cache exists. If it exists, do not operate the database again. .
if(!$smarty->isCached('index.tpl')){
echo "ACACHE NO FOUND!";
$sql = "SELECT * FROM test";
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
$smarty->assign("loaddatabase",$row[1]);
}
There is another problem here. If I change something in the database and want to update the display content, but the cache has not expired yet, what should I do?
$smarty->clearCache("index.tpl");
The clearCache above can solve this problem. As long as the data is updated, clearCache is called to clear the cache.
PS: I am using the Smarty3 version. The names of many methods have changed in this version. If it is Smarty2, “Call of unknown method ‘isCached’.” will appear. Please use $smarty->is_cached().
The Smarty3:registerPlugin() and Smarty2:register_block() that appear later are also version issues.
Let’s take a look at the speed comparison with and without caching:
1. First time browsing, no cache Total Time 0.01421
2. The second time you browse, there is cache. Total Time 0.00308
There are only a few lines of code in my index.php here. If the amount of data is large, there will be an obvious difference.
Partial caching technology
Partial cache = partial cache, that is, in the cache of a page, not all caches are generated. You can customize a certain functional module not to generate cache, and the data will be updated every time you browse;
For example, web pages display user status, web page statistics, advertising banners, etc. These data are updated very quickly and are not suitable for caching. In this way, local caching can be useful.
There are 3 methods for local caching:
1. insert method
The content contained in insert will not be cached, and the function will be re-executed every time the template is called.
How to use:
Note that the function name here must start with insert, and the name in the template corresponds to it.
index.php
//Define a time to test the difference between insert and ordinary assign
$date = date("Y-m-d H:i:s");
$smarty->assign("date", $date);
//insert
function insert_get_current_time($date){
Return date("Y-m-d H:i:s");
}
index.tpl
nocache:{insert name="get_current_time"}
cache: {$date}
Then look at the generated cache file: Come to the conclusion that insert will re-execute the function every time the template is called
nocache:
),$_smarty_tpl);?>
cache: 2012-06-04 15:46:52
This method is simple, but it is not suitable to use if the content to be displayed is a large piece.
2. Dynamic block method
Custom blocks in php
index.php
//smarty 3
// function declaration
function smarty_block_nocache ($param,$content,$smarty)
{
Return $content;
}
// register with smarty
$smarty->registerPlugin("function","nocache", "smarty_block_nocache");
As mentioned at the beginning, Smarty3 uses registerPlugin, and Smarty2 uses register_block
index.tpl
{nocache}{$date}{/nocache}
Then look at the cache file and conclude that $date
will be re-executed every time the template is called.
tpl_vars['date']->value;?>
3. Plug-in block method
This method is similar to the second one, except that the custom blocks in PHP are placed in the plugins folder in the smarty directory.
Create a file block.nocache.php in the Smarty/plugins directory with the following content:
function smarty_block_nocache($param, $content, $smarty)
{
Return $content;
}
?>
The usage in tpl template is the same as the second method
Summary
It can be concluded that Smarty caching technology can greatly improve the speed and quality of the website, and its usage is relatively simple.
The final reminder is that although the extension of the cache file generated by Smarty is php, it will not be parsed as php code.
Author: That Moment