Smarty Configuration and advanced caching technology _php tutorials

Source: Internet
Author: User
Tags md5 encryption php template time 0
Objective
Smarty is an excellent PHP template engine that separates the logic code from the user interface.
Learning and using smarty, which is not applied to its cache technology is a big loss, it can be the user finally see the HMTL file cached into a static HTML page, when set Smarty the Cache property is true, In the Cachetime period set by Smarty, the user's Web request is converted directly into this static HTML file, which is equivalent to calling a static HTML file, which reduces a lot of burden to the backend server.
Download and configure
Official Download: Smarty Download
After downloading, unzip to the file directory of your project.

1 require ('.. /libs/smarty.class.php ');
2
3 $smarty = new Smarty;
4
5//$smarty->force_compile = true; Forced compilation
6 $smarty->debugging = true; Debugging
7 $smarty->caching = true; Turn on caching
8 $smarty->cache_lifetime = 120; Cache survival Time (seconds)

$smarty->cache_dir = My_smarty_dir. '/cache/'; Set the cache's storage path

Note: If you find that the cache file will change every time you browse, see Smarty's force_compile, which forces smarty to compile the template every time it is called (re). This setting is not subject to $compile_check restrictions. By default, it is not valid. It is convenient for development and commissioning. But it must not be used in the product environment. If the cache is started, the cache file will be regenerated each time.
$smarty->force_compile = false; Forced compilation

Smarty Cache Technology
• Global Cache
• Local Cache
1. Insert method
2. Dynamic Block method
3. Plug-in block method
Global Cache Technology
As the name implies, the global cache is to generate the entire page cache file, specify the cache file survival time, in a specified time to browse the entire page, the cache file will be read directly.
$smarty->caching = true; Turn on caching
$smarty->cache_lifetime = 120; Cache survival Time (seconds)

Note: A template can have only one cache file, and if your template has multiple pages, set an ID for the cache. For example, a page has multiple articles:
Http://website/index.php?p=1
http://website/index.php?p=2
$_server[' Request_uri '] method
MD5 encryption of the URL of the current page (all parameters included in the following)
$url =md5 ($_server[' Request_uri ');
Set the cache file name
$smarty->display (' Index.tpl ', $url);

Focus: Using caching technology, a big reason is to reduce the database read and write, so we have to use $smarty->iscached (' INDEX.TPL ') to determine whether the cache exists, if it exists, do not operate the database again.

if (! $smarty->iscached (' Index.tpl ')) {
echo "Acache NO found!";
$sql = "SELECT * from Test";
$query = mysql_query ($sql);
$row = Mysql_fetch_row ($query);
$smarty->assign ("Loaddatabase", $row [1]);
}


There is a problem here, if I changed a database of some content, want to update the display, but the cache is not yet extinct time, then the swelling?
$smarty->clearcache ("Index.tpl");
The above clearcache can solve this problem, as long as the data is updated, call ClearCache to clear the cache.


PS: I use the Smarty3 version, this version of the many methods of naming have changed, if it is Smarty2 will appear "Call of unknown method ' iscached '.", use $smarty->is_cached ().
The Smarty3:registerplugin (), Smarty2:register_block (), which appears later, is also the issue of the version.

Let's look at the speed comparison with cache and no cache:
1. First browse, no cache total time 0.01421

2.2nd visit, with cache total time 0.00308

Here my index.php only a few lines of code, if the amount of data is large, there are obvious differences.

Local Cache Technology
Local cache = Partial cache, is the cache of a page, not all generate cache, you can customize a function module does not generate a cache, each browsing will update data;
For example: The Web page shows the status of users, Web page statistics, advertising, and so on, these data update speed is very fast, not suitable for caching, so that the local cache will be useful.
There are 3 methods for local caching:

First, insert method
Insert contains content that is not cached, and the function is re-executed each time the template is called.
How to use:
Note that the function name here must begin with insert, and the name in the template corresponds to it.
index.php

Define a time to test the difference between insert and normal assign
$date = Date ("y-m-d h:i:s");
$smarty->assign ("date", $date);
Insert
function Insert_get_current_time ($date) {
Return Date ("y-m-d h:i:s");
}

Index.tpl
Nocache:{insert name= "Get_current_time"}

Cache: {$date}
and then look at the resulting cache file: It concludes that the insert will re-execute the function every time it calls the template
NoCache:!--? php echo insert_get_current_time (array (
), $_SMARTY_TPL);?

cache:2012-06-04 15:46:52

This method is simple, but if the content to be displayed is a large chunk, it is not suitable for use.


II. Dynamic Block method
Custom Blocks in PHP
index.php

Smarty 3
function declaration
function Smarty_block_nocache ($param, $content, $smarty)
{
return $content;
}

Register with Smarty
$smarty->registerplugin ("function", "NoCache", "Smarty_block_nocache");

Beginning to mention, Smarty3 is used Registerplugin, Smarty2 is used Register_block
Index.tpl
{NoCache} {$date} {/nocache}
Then look at the cache file and conclude that each call to the template will be re-executed in the
tpl_vars[' Date ']->value;? >


Third, plug-in block method
This method is similar to the 2nd one, just put the custom blocks in PHP into the Plugins folder in the Smarty directory.
Build a file in the Smarty/plugins directory block.nocache.php content as follows:
function Smarty_block_nocache ($param, $content, $smarty)
{
return $content;
}
?>
The use of the TPL template is the same as the second method


Summarize
Can summarize the Smarty cache technology, can greatly improve the speed and quality of the site, usage is relatively simple.
The last thing to remind you is that the Smarty generated cache file extension is PHP, but it is not parsed as PHP code.

Author: that moment

http://www.bkjia.com/PHPjc/478175.html www.bkjia.com true http://www.bkjia.com/PHPjc/478175.html techarticle Preface Smarty is an excellent PHP template engine that separates the logic code from the user interface. Learning and using smarty, no caching technology applied to it is a big loss, it ...

  • Related Article

    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.