A good introduction to the PHP Cache class

Source: Internet
Author: User
A good PHP Cache class introduces the wide application of cache in actual use, which can reduce access to the server database and improve the running speed. Currently, many CMS content management systems frequently use caching mechanisms to improve system operation efficiency. The following is a well-written cache class. you can refer to the caching mechanism and writing method.

The cache. php code is as follows:

 Cachefilevar and the file name $ this-> cachefile // different parameters on the dynamic page correspond to different Cache files, but all Cache files on each dynamic page have the same file name, only the extensions are different $ s = array (". ","/"); $ r = array (" _ "," "); $ this-> cachefilevar = str_replace ($ s, $ r, $ _ SERVER ["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 the current All Cache file groups on the 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; // judge the current Whether the page Cache 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) ;}}}?>


Class usage:

 Check () {$ template = $ cache-> read ();} else {ob_start (); ob_implicit_flush (0);?> Page content ....
 Write ($ template) ;}?>

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.