Home > Backend Development > PHP Tutorial > smarty cache usage analysis, smarty cache usage_PHP tutorial

smarty cache usage analysis, smarty cache usage_PHP tutorial

WBOY
Release: 2016-07-13 10:11:38
Original
855 people have browsed it

smarty cache usage analysis, smarty cache usage

This article analyzes the usage of smarty cache in detail. Share it with everyone for your reference. The specific analysis is as follows:

At first I thought smarty was just used to cover up PHP code functions, but later I learned that it also has the powerful function of template caching.

What is template caching? When we take out some data from the database and output it to the template, we access the database every time. But in fact, every database access is the same. If the traffic of the website Very large. This kind of repeated access is completely unnecessary and puts a lot of pressure on the database. smarty provides caching technology to solve this problem.

First use smarty’s cache, we have a few things to set up:

Enable caching

Copy code The code is as follows:
$Smarty->caching = true;

Set cache period

Copy code The code is as follows:
$Smarty->cache_lifetime = 30;

Set up cache visual inspection

Copy code The code is as follows:
$Smarty->cache_dir = './cache';

Then for the part of database access, we first make a judgment whether this part has been cached

Copy code The code is as follows:
if(!$Smarty->isCached('01.html')){//Judge whether it has been done After caching, if it has already been done, don’t go here and output the template directly
$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 'Leave the database';
$Smarty->assign('goods);
}

But there is something to note in smarty's cache, that is, if you get the parameters from the address bar, it may affect the cache. Multiple different parameters only cache the first generated ones, so you need to use It is actually very simple to use single template and multiple cache technology, as long as
Copy code The code is as follows:
$Smarty->assign('goods',$goods);
A parameter is added here, This parameter is obtained from the address bar. Of course, when judging, you also need to add the parameter to determine whether it has been cached
Copy the code The code is as follows:
$Smarty->isCached('01.html',goods_id);

So how to delete this cache? It's very simple. You just need to call

Copy code The code is as follows:
$Smarty->clearCache( '01.html',$goods_id)
The second parameter is optional. If not filled in, all caches under this template will be deleted directly.
Finally, sometimes when debugging the program without caching, we can also set this parameter to temporarily stop caching:
Copy the code The code is as follows:
$Smarty->force_cache = true;

Finally, one thing to note is that the cache life cycle means that after this time, refreshing the page again will replace the old cache with the new cache. If a new cache is not generated, the old cache will not automatically Deleted, so in actual project development, if there are a lot of caches, it will actually affect the storage.

In fact, what we need to cache is only a small part of the HTML. Now many websites use memcached for caching.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/928225.htmlTechArticlesmarty cache usage analysis, smarty cache usage This article analyzes the usage of smarty cache in detail. Share it with everyone for your reference. The specific analysis is as follows: At first I thought smarty was just for...
Related labels:
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