Teach you Smarty cache technology

Source: Internet
Author: User
Tags website server
You should know that the cache mechanism can effectively reduce the pressure on the website server. one of the highlights of the Smarty template engine is that it provides us with very simple cache operations. let's take a look at the following. First we need to know the Smarty Cache mechanism is divided into global cache, part of the cache, bureau... "> <LINKhref =" http://www.php100.com//statics/style/h

You should know that the cache mechanism can effectively reduce the pressure on the website server. one of the highlights of the Smarty template engine is that it provides us with very simple cache operations. let's take a look at the following.

First, we need to know that the Smarty Cache mechanism is divided into global cache, partial cache, and partial cache. we will introduce them one by one.

1. global cache

As its name implies, global cache generates cache pages for all pages of the entire website.

First, we need to operate the smarty configuration file, enable the cache, specify the Directory of the cached file, and set the cache survival time.

$ Smarty-> cache_dir = './cache/'; // you can specify a folder for storing cached files.

$ Smarty-> caching = 1; // if the cache is enabled, 0 or FALSE indicate that the cache is disabled. | if the value is not 0, TRUE indicates that the cache is enabled.

$ Smarty-> cache_lifetime = 3600 // unit: Seconds (if-1 is set to never expire)

Next we will go to the specific php page to set the name of the specific cache file.

$ Url = md5 ($ _ SERVER ['request _ URI ']); // add the URL of the current page (including? All parameters ).

$ Smarty-> display('list2.html ', $ url); // Set the cache file name

Note that:

$ Smarty-> display ('corresponding template filename ', 'supplemental part of cache filename.

The second parameter is not required. if it is not written, the cached file name is the template file name that has been encrypted.

However, this will cause a difficult 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 names of the generated stored files are encrypted by list2.html.

This results in the user wanting to query different contents, but accessing the same cache file.

Therefore, we recommend that you add a secondary parameter to access the url (including? All the following parameters) md5 encryption is recommended by the author.

2. partial cache

First, you need to know what part of the cache is, in fact, specifying some files to generate cache files, rather than all the files on the website.

After learning about the expected results, let's take a look at the specific operations.

Before the operation, we must emphasize the following concept:

The essence of partial cache is that some files are not cached. that is to say, instead of specifying those files to generate the cache, some specific files are not cached.

Assume there are three files:

1. php // cache required

2. php // cache required

3. php // no cache required

In the 1.php/2. php file, write $ smarty-> display ('corresponding template filename ', 'Add part of cache filename.

However, in php 3, we need to specify that the cache does not need to be generated. the specific method is:

$ Smarty-> clear_cache ('corresponding template filename ') // it can be written before or after $ smarty-> display ('corresponding template filename ').

Of course, $ smarty-> display ('Template file name' corresponding to it) still needs to be written, so we didn't want to generate the cache, and the second parameter is not needed.

The parameters of $ smarty-> clear_cache () and $ smarty-> display () must be consistent.

3. local cache

First, let us understand the meaning of local cache, that is, to specify some local locations under the same page to generate a cache.

Here we also need to reverse our thinking.

In fact, it is not to specify which local caches are generated, but which ones are not generated (this is similar to the operation idea of some caches ).

Let's just say, let's take an instance first.

1. php

$ Time = time ();

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

Function insert_timeget ()

{

Return time ();

}

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

  

1. html

{$ Time}

// Refresh the cache repeatedly.

{Insert name = 'timeget '}

// When the cache is enabled, refresh it again.

After understanding the example, let's explain the principle.

In PHP, we only need to define

The value returned by the UDF named insert _ custom supplementary name is not transmitted using the assign () method, you can directly use {insert name = 'custom supplemental name'} on the template page without being affected by the cache, and refresh the page in real time.

Now, we have explained all the cache methods in smarty 3. if you are interested, you can try different effects on your own.

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.