Smarty cache usage analysis

Source: Internet
Author: User
This article mainly introduces the usage of the smarty cache and analyzes the features and usage skills of the smarty cache in the form of instances, which is of great practical value, for more information about the usage of the smarty cache, see this document. Share it with you for your reference. The specific analysis is as follows:

At first, I thought that smarty was only used to conceal some php code functions, but later I realized that there was a powerful template cache function.

What is template cache? when we extract some data from the database and output it to the template access, we access the database every time, but in fact, every database access is the same. if the traffic on the website is large, such repeated access is completely unnecessary, which puts a lot of pressure on the database. Smarty provides the cache technology to solve this problem.

First, use the smarty cache. we have some settings:

Enable cache

The code is as follows:

$ Smarty-> caching = true;

Set cache period

The code is as follows:

$ Smarty-> cache_lifetime = 30;

Set cache visual testing

The code is as follows:

$ Smarty-> cache_dir = './cache ';

Then, we first make a judgment on the part of the database access to determine whether the part has been cached.

The code is as follows:

If (! $ Smarty-> isCached('01.html ') {// Determine whether the cache has been performed. if the cache has been performed, do not go here and directly output the 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 'database gone ';
$ Smarty-> assign ('goods );
}


However, in the smarty cache, note that if you get parameters from the address bar, the cache may be affected. multiple different parameters are only cached for the first generation, so here we need to use the single-template multi-cache technology, in fact, it is also very simple, as long as

The code is as follows:

$ Smarty-> assign ('Goods ', $ goods );

A parameter is added here, which is obtained from the address bar. of course, you also need to add the parameter to determine whether it has been cached.

The code is as follows:

$ Smarty-> isCached('01.html ', goods_id );

So how to delete the cache? it's very easy to call.

The code is as follows:

$ Smarty-> abstrache('01.html ', $ goods_id)

The second parameter is optional. If this parameter is left blank, all the caches under this template will be deleted directly.
Finally, sometimes the program is not cached during debugging. you can also set this parameter to temporarily stop caching:

The code is as follows:

$ Smarty-> force_cache = true;

Note that the cache lifecycle means that after this time, refresh the page again and use a new cache instead of the old one. If no new cache is generated, in this case, the old cache will not be automatically deleted. Therefore, in actual project development, if there are many caches, the storage will be greatly affected.

In fact, we only need to cache a small part of HTML, and many websites now use memcached for caching.

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.