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. 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/; $ADODB_CACHE_DIR='tmp'; $db=NewADOConnect('mysql'); $db->connect('localhost','root','123456','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:

<?phprequire('./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: 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.