Home  >  Article  >  Backend Development  >  Introduction to caching technology in php

Introduction to caching technology in php

WBOY
WBOYOriginal
2016-07-25 09:03:531108browse
  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 CustomerName, CustomerID from customers';
  6. $rs = $conn->CacheExecute(15,$sql);
  7. ?>
Copy the code

as above. Every time you query the data, the corresponding results will be serialized and saved to the file. The same will happen in the future. The query statement can be obtained from the cache file without querying the database directly.

Let’s take a look at the page caching 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. ?>

Copy the code

as above, Every time you access a page, it will first check whether the corresponding cache exists. If it does not exist, connect to the database, get the data, complete the assignment of template variables, display the page, and generate a cache file at the same time, so that the cache file will be there the next time you visit. It works, and the data query statement of the if block will no longer be executed. Of course, there are many things to consider in actual use, such as the setting of the validity period, the setting of the cache group, etc. For details, you can check the relevant chapters on caching in the Smarty manual.

The emphasis of the caching methods of the above two popular PHP components is different. For Adodb's cache, it caches data, while for Smarty's cache, it caches pages. There are many other components that provide caching functions (such as: PEAR::Cache_Lite, etc.). Which solution to use in actual programming requires specific analysis of the specific situation, and may also be used comprehensively.

An obvious benefit of using the built-in caching solutions of these components is that their implementation is transparent to the client. Just make the necessary settings (such as cache time, cache directory, etc.) without thinking too much about the details of caching. The system will automatically manage the cache according to the settings. But its shortcomings are also obvious, because each request still needs to be parsed by PHP, and the efficiency is still greatly reduced compared with pure static. It still cannot meet the requirements in the face of large PV. In this case, just doing dynamic caching is not enough. Yes, static caching must be implemented.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn