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.inc.php); $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!