I'll teach you smarty caching technology

Source: Internet
Author: User
Tags md5 md5 encryption php file

We should know that caching mechanism can effectively reduce the Web site's server pressure,Smarty template engine is a big bright spot for us to provide a very simple caching operation, let us learn the following.


First of all, we need to know the Smarty caching mechanism is divided into global cache, partial cache, local cache three kinds, we introduced one by one.


1, Global cache


As the name suggests, global caching is the generation of cached pages for all pages of the entire site.


First we want to manipulate the Smarty configuration file, open the cache, specify the cache file directory, and set the cache's survival time


$smarty->cache_dir = './cache/';//Set up the folder where the cached files are stored


$smarty->caching = 1;//Open Cache 0, false on behalf of off | Not 0 digits, true to open


$smarty->cache_lifetime = 3600//Unit is seconds (if-1 is never expired)


Next we have to go to the specific PHP page to set the corresponding specific cache file name


$url =md5 ($_server[' Request_uri ']);//The URL of the current page, including the All subsequent parameters) for MD5 encryption


$smarty->display (' list2.html ', $url);//Set Cache file name


It is to be noted that:


$smarty->display (' corresponding template filename ', ' Supplemental part of cached file name ') this method.


The second parameter is not required, and if not, the cached file name is the template file name for the encryption process.


But there is a tricky problem:


Http://localhost/1.10/sm/list2.php? Lan=1


Http://localhost/1.10/sm/list2.php? lan=2


Http://localhost/1.10/sm/list2.php? Lan=3


3 URLs correspond to different content, but the resulting cached file name is the result of list2.html encryption.


This results in the user wanting to query for different content and accessing the same cached file.


So it is recommended to add a secondary parameter that will access the URL (included?) All subsequent parameters) MD5 encryption is one of the more recommended by authors.


2. Partial Cache


The first thing to know about partial caching is to specify some file generation cache files, not all of the Web site's files.


Understand what needs to be done, and then we'll do the exact thing.


Before we do this, we need to emphasize a concept:


The essence of partial caching is actually partially not cached, that is, not specifying those file generation caches, but specifying that some files do not generate a cache


Suppose there are 3 files:


1.php//cache required


2.php//cache required


3.php//No caching required


In the 1.php/2.php file, still write the method of $smarty->display (' corresponding template filename ', supplemental part of cached file name).


But in 3.php we have to specifically point out that there is no need to generate a cache, specifically:


$smarty->clear_cache (' corresponding template filename ')//written in $smarty->display (' corresponding template filename ') before or after


Of course $smarty->display (' corresponding template filename ') or to write, do not want to generate the cache, the second parameter will not need.


The parameters of the $smarty->clear_cache () and $smarty->display () must be written in a consistent


3, local cache


First, let's take a look at the meaning of local caching, which is to specify some local areas under the same page to generate the cache.


Also here we have to reverse the idea.


Instead of specifying which local build cache is, instead, which parts do not generate the cache (this is similar to the partial caching operation Idea).


Say no more, first give an example


1.php


$time =time ();


$smarty->assign (' time ', $lanmuarr);


function Insert_timeget ()


{


return time ();


}


$smarty->display (' 1.html ');


1.html


{$time}


After the cache is turned on, repeat refresh this does not change


{Insert name= ' timeget '}


After the cache is turned on, repeat refresh this will change


To understand this example, let's explain the principle.


In PHP , we just have to define


A custom function named Insert_ custom supplemental name in which the value returned, without the need for assign () method delivery, can be invoked directly in the template page with the {insert Name= ' custom supplemental Name '}, without being affected by the cache, and in real time refreshing

Well, here we go. Our 3-way cache of Smarty is all explained, and interested friends can experiment with different effects.

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.