PHP Cache details

Source: Internet
Author: User
What is cache! What can we do! In fact, the cache is equivalent to memory. Save for a while! Cache means that we do not need to execute the database when executing something. It is okay to directly execute our Cache. In general, the purpose of the cache is to put data in one place to make access faster. there is no doubt that the memory is the fastest,, can hundreds of MB of data be stored in the memory? This is unrealistic. of course, sometimes it is temporarily stored as a server cache. for example, if the ob_start () cache page is enabled, you will ask what cache is before sending the file header! What can we do! In fact, the cache is equivalent to memory. Save for a while!

Cache means that we do not need to execute the database when executing something. Execute our cache directly.

In general, the purpose of caching is to put data in one place to make access faster. there is no doubt that the memory is the fastest, but can hundreds of MB of data be stored in the memory? This is not realistic. of course, sometimes it is temporarily stored as a server cache, for example, if the ob_start () cache page is enabled, the page content will be cached in the memory before the file header is sent, knowing that the page output will be clear automatically or wait for the ob_get_contents to return, or be cleared by the ob_end_clean display, which can be used well in the generation of static pages and can be well reflected in the template, I have discussed this article in depth: it is a temporary way to generate static pages in php, but it is not a good solution to our problem.

It can be said that cache is generally divided into page cache and data cache. The ADODB cache is the data cache. The smarty is the page cache. The adodb cache is

<? Php}
Include (./adodb. inc. php );
$ ADODB_CACHE_DIR = 'tmp ';
$ Db = NewADOConnect ('mysql ');
$ Db-> connect ('localhost', 'root', '123', 'mysql ');
$ SQL = "select * from user ";
$ Db-> cacheexecute (300, $ SQL );
?>

In this way, the cache is generated under the TMP directory! (Cached files are serialized data .) Next time, we will read data from the cache directly. SMARTY cache:

<
? Php
Require ('./smarty/Smarty. class. php ');
$ Smarty = new Smarty; Z)
$ Smarty-> caching = true;
If (! $ Smarty-> is_cached ('index. tpl '))
// No cache available, do variable assignments here .)
$ Contents = get_database_contents ();
$ Smarty-> assign ($ contents );
}
$ Smarty-> display ('index. tpl ');)
?>

First, determine whether the cached file exists! No direct link to the database! If yes! Execute DISPLAY. Read cache. We can see the two examples above! I have a good understanding of cache.

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.