This article mainly introduces the simple and practical website PHP Cache class, which is of great benefit for everyone to learn and understand the cache mechanism and operating principle, if you need it, you can refer to the cache technology which is widely used in actual use. it can effectively reduce the access pressure on the server database and improve the running speed. Currently, many CMS content management systems frequently use caching mechanisms to improve system operation efficiency. This article takes a simple and practical cache class as an example to help you refer to the cache mechanism and writing method.
The cache. php code of the cached file is as follows:
<? Php/* constants to be defined in advance: _ CachePath _ template cache path _ CacheEnable _ whether the automatic Cache mechanism is enabled, undefined or empty, indicates the automatic cache mechanism _ ReCacheTime _ automatic cache re-interval. the unit is seconds. if it is undefined or empty, automatic cache re-caching is disabled */class cache {var $ cachefile; var $ cachefilevar; function cache () {// Generate the name of the Cache group for the current page $ this-> cachefilevar and the name $ this-> cachefile // different parameters on the dynamic page correspond to different Cache files, however, all the Cache files on each dynamic page have the same file name, but the extension is different $ s = array (". ","/"); $ r = array (" _ "," "); $ this-> cachefilevar = str_replace ($ s, $ r, $ _ SERVE R ["SCRIPT_NAME"]). "_". $ _ GET [_ ActionVar _]; $ this-> cachefile = $ this-> cachefilevar. ". ". md5 ($ _ SERVER ["REQUEST_URI"]);} // delete the cache function delete () of the current page/module () {// delete the cache of the current page $ d = dir (_ CachePath _); $ strlen = strlen ($ this-> cachefilevar ); // return all Cache file groups of Taiyuan 264 Hospital on the current page while (false! ==( $ Entry = $ d-> read () {if (substr ($ entry, 0, $ strlen) ==$ this-> cachefilevar) {if (! Unlink (_ CachePath _. "/". $ entry) {echo "the Cache directory cannot be written"; exit ;}}// determine whether the Cache has been cached and whether the Cache function check () is required () {// if the cache update interval _ ReCacheTime _ if (_ ReCacheTime _ + 0> 0) is set) {// return the last update time of the current page Cache $ var = @ file (_ CachePath _. "/". $ this-> cachefilevar); $ var = $ var [0]; // if the update time exceeds the update interval, delete the Cache file if (time () -$ var >_recachetime _) {$ this-> delete (); $ ischage = true ;}// return the Cache $ file = _ CachePath _ of the current page _. "/". $ this-> cachefile; // determines when Whether the Cache on the previous page exists and whether the Cache function enables return (file_exists ($ file) and _ CacheEnable _ and! $ Ischange);} // read the Cache function read () {// return the current page's Cache $ file = _ CachePath _. "/". $ this-> cachefile; // read the content of the Cache file if (_ CacheEnable _) return readfile ($ file); else return false ;} // Generate Cache function write ($ output) {// return the current page's Cache $ file = _ CachePath _. "/". $ this-> cachefile; // if the Cache function is enabled if (_ CacheEnable _) {// write the output content to the Cache file $ fp = @ fopen ($ file, 'W'); if (! @ Fputs ($ fp, $ output) {echo "template Cache write failed"; exit ;}@ fclose ($ fp ); // if the Cache update interval _ ReCacheTime _ if (_ ReCacheTime _ + 0> 0) is set {// The last update time of the Cache on the current page $ file = _ CachePath _. "/". $ this-> cachefilevar; $ fp = @ fopen ($ file, 'w'); if (! @ Fwrite ($ fp, time () {echo "the Cache directory cannot be written to"; exit ;}@ fclose ($ fp) ;}}}?>
Usage of cache classes:
<? Php define ("_ CachePath _",". /cache/"); define (" _ CacheEnable _ "," 1 "); define (" _ ReCacheTime _ "," 43200 "); include ('cache. php '); $ cache = new cache (); if ($ cache-> check () {$ template = $ cache-> read ();} else {ob_start (); ob_implicit_flush (0);?> Here is the page content .... <? Php $ template = ob_get_contents (); $ cache-> write ($ template);}?>