Source Code Analysis of the Cache class

Source: Internet
Author: User
The source code of the Cache class analyzes the source code of a Cache class. For more information about how to learn the Cache, see? /* 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, in seconds, undefined or empty Source Code Analysis of the Cache class

For source code analysis of a Cache class, refer
/*
Constants that need to be defined in advance:
_ CachePath _ template cache path
_ CacheEnable _ whether the automatic Cache mechanism is enabled. if it is undefined or empty, the automatic Cache mechanism is disabled.
_ ReCacheTime _ automatic cache re-caching interval, in seconds. if it is undefined or empty, automatic cache re-caching is disabled.
_ ActionVar _ GET variable of Action

Sample:
$ Cache = new cache ();
If ($ cache-> check ()){
$ Template = $ cache-> read ();
} Else {
......
$ Cache-> write ($ template );
}
Eval ('echo $ template ;');
*/

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 dynamic page parameters correspond to different Cache files, but all the Cache files on each dynamic page have the same file name, but 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 of the current page/module
Function delete (){
// 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 or not the Cache has been cached and whether the Cache is required
Function check (){
// If the cache update interval _ ReCacheTime _ is set _
If (_ ReCacheTime _ + 0> 0 ){
// Return the last update time of the current page Cache
$ Var = @ file (_ CachePath _. "/". $ this-> cachefilevar); $ var = $ var [0];
// Delete the Cache file if the update time exceeds the update interval
If (time ()-$ var> _ ReCacheTime _){
$ This-> delete (); $ ischage = true;
}
}
// Return the Cache of the current page
$ File = _ CachePath _. "/". $ this-> cachefile;
// Determine whether the current page Cache exists and whether the Cache function is enabled
Return (file_exists ($ file) and _ CacheEnable _ and! $ Ischange );
}

// Read Cache
Function read (){
// Return the Cache of the current page
$ File = _ CachePath _. "/". $ this-> cachefile;
// Read the content of the Cache file
If (_ CacheEnable _) return file_get_contents ($ file );
Else return false;
}

// Generate Cache
Function write ($ output ){
// Return the Cache of the current page
$ 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 (! @ Fwrite ($ fp, $ output) {echo "template Cache write failed"; exit ;}
@ Fclose ($ fp );
// If the cache update interval _ ReCacheTime _ is set _
If (_ ReCacheTime _ + 0> 0 ){
// Update the last update time of the current page Cache
$ File = _ CachePath _. "/". $ this-> cachefilevar;
$ Fp = @ fopen ($ file, 'w ');
If (! @ Fwrite ($ fp, time () {echo "the Cache directory cannot be written"; exit ;}
@ Fclose ($ fp );
}
}
}

}

Related Article

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.