This paper analyzes the usage of smarty caching in detail. Share to everyone for your reference. The specific analysis is as follows:
Initially thought Smarty was just used to do some masking PHP code features, but later knew that there is a template cache this powerful feature.
What is the template cache, that is, when we take some data out of the database to the template access, every time the database access, but in fact, every time the database access is the same, if the Web site traffic is very large, this repeated access is completely unnecessary, for the database has a lot of pressure. Smarty provides caching techniques to solve this problem.
First use the Smarty cache, we have some to set:
Turn on caching
Copy Code code as follows:
Setting the cache cycle
Copy Code code as follows:
$Smarty->cache_lifetime = 30;
Set Cache Visual
Copy Code code as follows:
$Smarty->cache_dir = './cache ';
And then for the part of the database access, we first make a decision as to whether this part has been cached
Copy Code code as follows:
if (! $Smarty->iscached (' 01.html ')) {//To determine if the cache has been made, if it has been done without going here, direct output template
$conn = mysql_connect (' localhost ', ' root ', ' root ');
mysql_query (' Set names UTF8 ');
mysql_query (' use market ');
$rs = mysql_query (' Select goods_id,goods_name,shop_price,add_time from goods where goods_id = '. $goods _id, $conn);
$goods =array ();
while ($row = Mysql_fetch_assoc ($rs)) {
$goods [] = $row;
}
Echo ' walked the database ';
$Smarty->assign (' goods);
}
But there's still room for attention in the Smarty cache, that is, if you get the parameters from the address bar, then it is possible to affect the cache, a number of different parameters only cache the first generation, so here you need to use a single template for multiple caching technology, but also very simple, as long as in
Copy Code code as follows:
$Smarty->assign (' goods ', $goods);
This adds a parameter, which is obtained from the address bar, and, of course, it needs to be added to determine if it has been cached.
Copy Code code as follows:
$Smarty->iscached (' 01.html ', goods_id);
So how does this cache have to be deleted, very simple, only need to call
Copy Code code as follows:
$Smarty->clearcache (' 01.html ', $goods _id)
The second parameter is optional, if you do not fill out the template directly below all the cache.
Finally, sometimes when the debugger is not cached, we can also set this parameter to temporarily stop the cache:
Copy Code code as follows:
$Smarty->force_cache = true;
Finally, one thing to note is that the lifecycle of the cache refers to the after this time, again refresh this page will replace the old with the new cache, if not generate a new cache, then the old cache is not automatically deleted, so in the actual project development, if the cache a lot of words, in fact, also very affect the storage.
In fact, we need to cache only a very small part of the HTML, now many stations are using the memcached to cache.
I hope this article will help you with your PHP program design.