Php tutorial File Cache instance code
Cache is widely used in actual use, which can reduce the access to the server database tutorial and improve the running speed. Currently, many cms Content Management systems frequently use caching mechanisms to improve system operation efficiency.
The cache. php code is as follows:
Php code
<?
/*
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.
*/
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 readfile ($ 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
Fopen ($ file, 'w' "> $ fp = @ fopen ($ file, 'w ');
If (! @ Fputs ($ 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 );
}
}
}
}
?>
Usage process:
Php code
<? 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 );
}
?>