A detailed approach to the configuration of PHP Smarty advanced caching

Source: Internet
Author: User
Tags smarty template

For Web sites with large access traffic and no need for timely updating of data, full use of caching technology can effectively reduce server pressure. Powerful caching techniques are also built into the Smarty template engine, and the Smarty caching function can be used only for simple configuration. Open Smarty Advanced cache only need to set up in the Smarty configuration file cache directory, open the cache state, set the expiration of the cache, the specific use of the following methods:

The code is as follows Copy Code
/***********smarty.php***********/
<?php
Include ('./smarty/smarty.class.php ');//import Smarty Template engine class
Instantiating Smarty objects $smarty =new smarty;//
$smarty->template_dir= "Templates";//Specify Template storage directory
$smarty->compile_dir= "Templates_c";//Specify compilation file storage directory
$smarty->config_dir= "config";//Specify Configuration file storage directory
$smarty->cache_dir= "Cache";//Specify Cached storage directory
$smarty->caching=true;//to open Smarty cache
$smarty->cache_lifetime=60;//to set the cache effective time (in seconds)
$smarty->left_delimiter= "<{";//Specify left label
$smarty->right_delimiter= "}>";//Specify Right Label
?>
Smarty An example of how advanced caching is used
/***********index.php***********/
<?php
Include ("smarty.php");
Date_default_timezone_set (' PRC ');
$nowt =date (' y-m-d h:i:s ');
function Insert_buhuancun () {//locally not cached content (note!) The method name to be followed here Insert_ to the corresponding in the template file
$ntime = ' PHP well-known network-'. Date ("y-m-d h:m:s");
return $ntime;
}
$smarty->assign (' nowt ', $nowt);
$smarty->display ("index.html");
?>
/***********index.html***********/
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>php well-known network </title>
<body>
The contents will be cached for 60 seconds (that is, the time set by Cache_lifetime in the smarty.php configuration file): <font color= ' red ' ><{$nowt ></font><br/>
Insert local data is not cached: <font color= ' red ' ><{insert name= ' buhuancun '}></font><br/>
Block local blocks are not cached: <br/>
<font color= ' Red ' >
<{block name= ' Thisnocache ' nocache}>
&nbsp;&nbsp; all content contained in block blocks is not cached: <{$nowt}>
<{/block}>
</font><br/>
</body>

Using the Smarty advanced cache to cache some content in the database is also a good choice, not only to increase the speed of user access, but also to reduce the pressure of frequent access to the database

Use and purge of smarty cache

The code is as follows Copy Code

$smarty->clear_all_cache (); It is clear that the cache of other templates that you don't want to clear will also be emptied.

Now I am in the background to this page with parameters Basic.php?a=about or =contact

The things involved in the modification Update database operations. And, of course, we need to clear out this time. All caches for the BASIC.TPL template are the two caches of about and contact.

  code is as follows copy code

//Clear multiple caches of a template A
$smarty->clear_cache ("Basic.tpl", "about") that specifies the cache number;
$smarty->clear_cache ("Basic.tpl", "contact");

$smarty->display (' Cache.tpl ', cache_id);//create a cache with IDs

$smarty->clear_all_cache ();//Clear All caches
$smarty->clear_cache (' index.htm ');//clear INDEX.TPL cache
$smarty- >clear_cache (' index.htm ', cache_id); Clears the cache for the specified ID

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.