PHP Cache principle and smarty cache

Source: Internet
Author: User
I have learned the PHP buffer technology before. today I am studying the PHP Cache technology. even if it is a word poor, I can say that it is "ten miles away ".

I have learned the PHP buffer technology before. today I am studying the PHP Cache technology. even if it is a word poor, I can say that it is "ten miles away ".

I. PHP Cache principles

When we access a website, we can query or log in to the database to obtain information. Like a large portal website with millions or tens of millions of visits per day, the data traffic is huge. Therefore, the PHP Cache technology can avoid frequent and unnecessary database operations, this improves the efficiency of program execution.

Let's take a look at several PHP Cache technologies:

1. Data Cache-A common cache technology

The literal meaning is to store the data first to reduce access to the database. During the first access, the data to be used is saved to a file. when the user visits the page again, the system first checks whether the data to be queried is in the cache file. If yes, then, the database is retrieved directly without further access.

Generally, the data is saved as an array, such as the cache of ecshop.

2. page cache

Similar to the data cache, when accessing the page, it checks whether the corresponding page cache exists. if it does not exist, it queries the database to obtain the corresponding data and generates a cache page. in this way, you can directly retrieve the cache page next time without having to query the database again.

3. memory cache

Without much analysis, we are generally familiar with optimization using third-party software, such as Memcached and dbcached.

The above method can solve the problem of frequent access to the database, but the lack of timeliness, after the database changes, their default data is still modified. Therefore, it is necessary to clear the cache within the specified time to update the data. For example, if a timestamp t1 and the current timestamp t2 are set after each cache update, the cache is not updated when the difference between t1 and t2 is within T, otherwise, if the time exceeds T, you can re-query the database, clear the previous cache, and obtain a new timestamp, which repeats in sequence.

II. smarty cache

When talking about smarty, you should pay attention to two concepts: compilation and caching: smarty compilation is enabled by default, and the smarty Cache mechanism must be enabled by developers themselves. Compiling is to convert the template to be compiled by smarty into a PHP script program. next time, you can directly access the compiled PHP script program, thus saving the program execution overhead.

Brief introduction to the smarty cache technology:

1. enable cache

To use the smarty cache, first enable the cache. First create a new smarty class, and then set: $ tpl = new Smarty (); $ tpl-> caching = true; $ tpl-> cache_dir = '/cache/tp'

2. set the cache update period

If the cache is never updated, the website will lose its dynamic nature. Next we need to set a cache update cycle: $ tpl-> cache_lifetime = 60*60; (in seconds)

In addition, you need to set $ tpl-> caching:

Value: 1: force not update cache: value: 2: Set the update period value to 0 before obtaining the Template: force not cache, equivalent to false

Therefore, set caching = 2 before setting the update period cache_lifetime.

3. one template and multiple caches

When caching articles, they all use the same template, so they must be distinguished. Set $ tpl-> display ('article. php', $ art_id) based on the second parameter of display or fetch)

4. is_cashed () more rational call of cache

Some of the above cache methods do not need to update data from the database on the next visit page, but the previous $ SQL statement to query the database is still executed, which increases the processing overhead of PHP.

Before executing the $ SQL statement, use is_cached () to determine whether the cache exists. for example:

If (is_cached(article.html ))

{

$ SQL =... // execute a series of $ SQL statements

}

$ Tpl-> display('article.html ');

When this template has multiple caches, you can include the second parameter is_cached('article.html ', $ art_id)

5. local cache or no cache

We can specify a part of the cache page:

Currently, there are multiple plug-in functions under the smarty plug-in Directory: block. cacheless. php. the code is as follows:

Function smarty_block_cacheless ($ param, $ content, & $ smarty)

{
Return $ content;
}
?>

Also modify the smarty _ Compiler. class. php in the Smarty, find the following line, and change true to false:

$ This-> _ plugins ['block'] [$ tag_command] = array ($ plugin_func, null, true)

 

Then you can use this method to specify the cache on the page:

<% $ Smarty. now %> cache
<% $ Smarty. now %> no cache here <%/cacheless %>

6. clear cache

$ Tpl-> clear_all_cache (); // clear all caches

$ Tpl-> clear_cache('file.html '); // clear the specified cache

$ Tpl-> clear_cache('article.html ', $ art_id); // clear the cache number specified in the same template

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.