Smarty template usage tutorial (5)-programmer (2) cache technology

Source: Internet
Author: User
Indicates that you want to view the news with id 10, but, news. the PHP file is loaded with news. htpl template, this template already has a cache file, so this time we can see the cache information of news with id 9. 6. use cache when our program runs for the first time, if the cache function is enabled, a cache file is generated. when the program is accessed for the second time, to directly output the cached file to the user, the cache for the template file is Demo01.tpl ------ à 2373874981asdf222187217. demo01.tpl. php user -- à demo01. tpl ------- à 2373874981asdf222187217. demo01.tpl. php Cache does not have static page speed fast 1. about the running process of the smarty cache 2. set the cache l $ smarty-> setCacheDir ($ cache_dir ); // Set the cache file directory l $ smarty-> caching = true; // enable the cache function l $ smarty-> cach_lifetime = 3600; // Set the cache time below. when the first operation is completed, a folder named cache is generated in the current path. in this folder, a cache file named demo06.htpl is generated, if the user returns the demo06.htpl template next time, smary will directly call this cache file. 3. check the cache $ smarty-> isCached ("tpl. tpl) to determine whether a template file is cached. If yes, true is returned. otherwise, false is returned.
This method is useful for using the smarty template !!! For example, if I want to query a news article, when I have read it, a cache file for this news article will be generated. now another person wants to access this news article. Should I do this? If the news is cached, the cached file is used. if there is no cache, query the database. 4. clear the cache. tpl ") clear cache files of the specified template l $ smarty-> clearAllCache () clear cache files of all templates do not enable the cache mechanism when deleting cache files; otherwise, the cache is deleted and generated. Question: What is the Cache clearing function? generally, when data in the database changes, the previous cache needs to be cleared, re-generate new cache files. 5. partial cache in some webpages. not all data needs to be cached. for example, on a news page, the cache technology should be used for news titles, news content, and release time, but information such as comments, views, and total news should not be cached in smarty, there are three ways to use the local cache function: l $ smarty-> assign ("var", "value", true) The third Assign method indicates whether to disable the cache by default: falsel {$ var nocache = true} adds nocache = true to the template variable, which indicates disabling the cache function l {nocache} {/nocache}. if a large number of template variables need to be disabled, you can consider using the {nocache} function in the template 1) 2) 3) instance: for the code, see demo11.php demo11.htpl 6. single-page multi-cache problem http://www.phpstar.cn/news.php?id=9 Indicates that you want to view the news with id 9. after viewing the news, a cache file is generated for the news. htpl template. http://www.phpstar.cn/news.php?id=10 Indicates that you want to view the news with id 10, but, news. the PHP file is loaded with news. htpl template. this template already has a cache file, so this time we can see the cache information of news with id 9 l $ smarty-> caching = true; l $ smarty-> display ("tpl", "cacheid"); specify the cache id for the current template. 7. the cache set is on some pages, and there are usually combined queries. http://www.phpstar.cn/product.php?id=8&sid=10 810
Http://www.phpstar.cn/product.php? Id = 8 & sid = 98 ^ 23872873873demo11. htpl. php 810l $ smarty-> caching = truel $ smarty-> display ("tpl", $ id1. "| ". $ id2 );

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.