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. For more information, see
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. For more information, see
The cache technology is widely used in actual use, which 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 to disable the automatic cache mechanism _ ReCacheTime _ automatic cache re-interval, in seconds. If it is undefined or empty, it indicates to disable automatic cache re-cache */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, $ _ SERVER [" SCRI PT_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 Cachefunction check () is required () {// If the cache update interval is set to _ ReCacheTime_if (_ ReCacheTime _ + 0> 0) {// 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 whether the current page Cache exists and whether the Cache Function Can return (file_exists ($ file) and _ CacheEnable _ and be enabled! $ Ischange);} // read Cachefunction read () {// return the Cache $ file = _ CachePath _ of the current page _. "/". $ this-> cachefile; // read the content of the Cache file if (_ CacheEnable _) return readfile ($ file); else return false;} // generate Cachefunction write ($ output) {// return the Cache $ file = _ CachePath _ of the current page _. "/". $ 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:
<? Phpdefine ("_ 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);}?>