Smarty Configuration and advanced caching technology share _php templates

Source: Internet
Author: User
Tags md5 php template time 0
Preface

Smarty is an excellent PHP template engine that separates the logical code from the user interface.

Learning and using smarty, there is no cache technology applied to it is a great loss, it can be the user finally see the HMTL file into a static HTML page, when the setting Smarty cache property is True, Converting a user's Web request directly into this static HTML file during the Cachetime period of the Smarty setting is equivalent to calling a static HTML file and reducing a lot of burden to the backend server.

Download and Configuration

Official Download: Smarty Download

After downloading, unzip to the file directory of your project.

Copy Code code as follows:

Require ('.. /libs/smarty.class.php ');
$smarty = new Smarty;
$smarty->force_compile = true; Forced compilation
$smarty->debugging = true; Debugging
$smarty->caching = true; Turn on caching
$smarty->cache_lifetime = 120; Cache Survival Time (sec)

$smarty->cache_dir = My_smarty_dir. '/cache/'; To set the storage path for the cache

Note: If you find that cached files will change every time you browse, see Smarty's force_compile, which forces smarty to compile the template every time you call (re). This setting is not subject to $compile_check restrictions. By default, it is not valid. It is very convenient for development and debugging. But it must not be used in the product environment. If caching is started, the cached file will be regenerated each time.

$smarty->force_compile = false; Forced compilation

Smarty Caching Technology

Global cache
Local cache
Insert method
Dynamic Block method
Plug-in Block method

Global Caching Technology

As the name suggests, the global cache is the entire page generated cache file, specify the cache file survival time, in a specified time to browse the entire page again, will read the cached file directly.
Copy Code code as follows:

$smarty->caching = true; Turn on caching
$smarty->cache_lifetime = 120; Cache Survival Time (sec)


Note: A template can have only one cache file, and if your template has multiple pages, you should 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
Copy Code code as follows:

$_server[' Request_uri '] method
MD5 the URL of the current page (including all parameters that follow)
$url =md5 ($_server[' Request_uri '));
Set Cache file name
$smarty->display (' Index.tpl ', $url);


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

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 the content of the database, want to update the display content, but the cache has not yet to die time, that the swollen do?
$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 am using the Smarty3 version, this version of the name of many methods have changed, if it is Smarty2 words will appear "Call of the Unknown method ' iscached '.", use $smarty->is_cached ().
The following Smarty3:registerplugin (), Smarty2:register_block () is also the issue of versioning.

now let's take a look at the speed comparison between cache and no cache:
1. First browsing, total time 0.01421 not cached

2.2nd browsing, total cache total time 0.00308

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

Local caching technology

Local cache = Partial cache, is a page of the cache, not all generated cache, can be customized to set a function module does not generate a cache, each browse will update the data;

For example: The Web page displays the user's status, the webpage statistics, the advertisement article and so on, these data update speed are very fast, is not suitable the cache, thus, the local cache then has the useful.

There are 3 methods for local caching:

I. Insert method

The content contained in the insert is not cached, and the function is rerun each time the template is invoked.

How to use:

Note that the function name here must begin with insert, and the name in the template corresponds to it.

index.php
Copy Code code as follows:

Define a time to test the difference between inserts and ordinary 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
Copy Code code as follows:

Nocache:{insert name= "Get_current_time"}
Cache: {$date}
[Code]
Then look at the generated cache file: Come to the conclusion the insert will rerun the function each time the template is called

nocache:<?php Echo insert_get_current_time Array (
), $_SMARTY_TPL);? >

cache:2012-06-04 15:46:52
Copy Code code as follows:


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

Second, dynamic block method

Custom Blocks in PHP
index.php
[Code]
Smarty 3
function declaration
function Smarty_block_nocache ($param, $content, $smarty)
{
return $content;
}
Register with Smarty
$smarty->registerplugin ("function", "NoCache", "Smarty_block_nocache");

At first it was mentioned that Smarty3 is used Registerplugin, Smarty2 is used Register_block

Index.tpl

{NoCache} {$date} {/nocache}
Then look at the cached file and conclude that every time the template is invoked, the $date is executed again.
[/code]
<?php echo $_smarty_tpl->tpl_vars[' date ']->value;? >
Copy Code code as follows:

Third, plug-in block method

This method is similar to the 2nd one, just put the custom block in PHP into the Plugins folder in the Smarty directory.

Create a file in the Smarty/plugins directory block.nocache.php the contents are as follows:

<?php
function Smarty_block_nocache ($param, $content, $smarty)
{
return $content;
}
?>
[Code]
The use of the TPL template is the same as the second method

Summarize
Can be summed up smarty caching technology, can greatly improve the speed and quality of the site, the use is relatively simple.

Finally, the Smarty-generated cache file extension, though PHP, is not parsed as a PHP code.

Author: that moment

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.