Introduction to php Cache technology

Source: Internet
Author: User
Introduction to php Cache technology

  1. Include ('adodb. inc. php'); # load code common to adodb
  2. $ ADODB_CACHE_DIR = '/usr/ADODB_cache ';
  3. $ Conn = & ADONewConnection ('mysql'); # create a connection
  4. $ Conn-> PConnect ('localhost', 'userid', '', 'agora '); # connect to MySQL, agora db
  5. $ SQL = 'SELECT mermername, CustomerID from customer ';
  6. $ Rs = $ conn-> CacheExecute (15, $ SQL );
  7. ?>

As shown above, each time data is queried, the corresponding results are serialized and saved to the file. in the future, the same query statement can be obtained from the cache file instead of directly querying the database.

Let's take a look at the page cache function provided by Smarty:

  1. Require ('smarty. class. php ');

  2. $ Smarty = new Smarty;

  3. $ Smarty-> caching = true;

  4. If (! $ Smarty-> is_cached ('index. tpl ')){

  5. // No cache available, do variable assignments here.
  6. $ Contents = get_database_contents ();
  7. $ Smarty-> assign ($ contents );
  8. }
  9. $ Smarty-> display ('index. tpl ');
  10. ?>

As shown above, each time you access the page, the system first checks whether the corresponding cache exists. if it does not exist, it connects to the database and obtains the data. after the template variable value is assigned, the page is displayed, the cache file is generated at the same time, so that the cache file will play a role the next time you access it, instead of executing the if block data query statement. Of course, there are many things to consider in actual use, such as setting the validity period and cache group. for details, refer to the related sections on cache (caching) in the Smarty manual.

The two popular PHP component caching methods have different focuses. for the Adodb cache, it caches data and for the Smarty cache, it caches pages. There are many other components that provide the caching function (for example, PEAR: Cache_Lite). the specific analysis of the specific solution used in actual programming may also be used in combination.

One obvious advantage of using the built-in caching scheme of these components is that their implementation is transparent to the client. You only need to make the necessary settings (such as the cache time and cache directory), instead of having to think too much about the cache details, the system will automatically manage the cache according to the settings. However, its disadvantages are also obvious, because every request still needs to be parsed using PHP, and the efficiency is still greatly reduced compared with pure static, which still cannot meet the requirements in the face of large PVS. in this case, static caching is required because dynamic caching is not enough.

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.