Record a practical PHP cache class

Source: Internet
Author: User

Cache is widely used 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:

<? /* 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, $ _ SERVER [" S CRIPT_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 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; // 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:

<? 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);?> Page content .... <? Php $ template = ob_get_contents (); $ cache-> 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.