A php Cache class. Cache. inc. php :? PhpclassCache {*** $ dir: cache file storage directory * $ lifetime: cache file validity period, in seconds * $ cacheid: cache file path, including file name * $ ext: cache. inc. php:
Class Cache {
/**
* $ Dir: cache file storage directory
* $ Lifetime: cache file validity period, in seconds
* $ Cacheid: cache file path, including file name
* $ Ext: cache file extension (not required), which is used for viewing files conveniently.
*/
Private $ dir;
Private $ lifetime;
Private $ cacheid;
Private $ ext;
/**
* Destructor: checks whether the cache directory is valid. the default value is assigned.
*/
Function _ construct ($ dir =, $ lifetime = 1800 ){
If ($ this-> dir_isvalid ($ dir )){
$ This-> dir = $ dir;
$ This-> lifetime = $ lifetime;
$ This-> ext =. Php;
$ This-> cacheid = $ this-> getcacheid ();
}
}
/**
* Check whether the cache is valid
*/
Private function isvalid (){
If (! File_exists ($ this-> cacheid) return false;
If (! (@ $ Mtime = filemtime ($ this-> cacheid) return false;
If (mktime ()-$ mtime> $ this-> lifetime) return false;
Return true;
}
/**
* Write cache
* $ Mode = 0. the page content is obtained in browser cache.
* $ Mode = 1. the page content is obtained by directly assigning values (received by the $ content parameter ).
* $ Mode = 2. obtain the page content locally (fopen ile_get_contents) (it seems that this method is unnecessary)
*/
Public function write ($ mode = 0, $ content = ){
Switch ($ mode ){
Case 0:
$ Content = ob_get_contents ();
Break;
Default:
Break;
}
Ob_end_flush ();
Try {
File_put_contents ($ this-> cacheid, $ content );
}
Catch (Exception $ e ){
$ This-> error (write cache failed! Check the directory permission !);
}
}
/**
* Load cache
* Exit () stops the execution of the original page program after loading the cache. if the cache is invalid, run the original page program to generate the cache.
* Ob_start () enables browser caching to retrieve page content at the end of the page
*/
Public function load (){
If ($ this-> isvalid ()){
Echo "This is Cache .";
// Which of the following two methods is better ?????
Require_once ($ this-> cacheid );
// Echo file_get_contents ($ this-> cacheid );
Exit ();
}
Else {
Ob_start ();
}
}
/**
* Clear cache
*/
Public function clean (){
Try {
Unlink ($ this-> cacheid );
}
Catch (Exception $ e ){
$ This-> error (failed to clear cache file! Check the directory permission !);
}
}
/**
* Obtain the cache file path
*/
Private function getcacheid (){
Return $ this-> dir. md5 ($ this-> geturl (). $ this-> ext;
}
/**
* Check whether the directory exists or can be created
*/
Private function dir_isvalid ($ dir ){
If (is_dir ($ dir) return true;
Try {
Mkdir ($ dir, 0777 );
}
Catch (Exception $ e ){
$ This-> error (the specified cache directory does not exist and creation failed! Check the directory permission !);
Return false;
}
Return true;
}
/**
* Obtain the complete url of the current page.
*/
Private function geturl (){
$ Url =;
If (isset ($ _ SERVER [REQUEST_URI]) {
$ Url = $ _ SERVER [REQUEST_URI];
}
Else {
$ Url = $ _ SERVER [Php_SELF];
$ Url. = empty ($ _ SERVER [QUERY_STRING])? :?. $ _ SERVER [QUERY_STRING];
}
Return $ url;
}
/**
* Output error message
*/
Private function error ($ str ){
Echo
. $ Str.
;
}
}
?>
Demo. php:
/*
* You can freely repost and use it. please retain the copyright information. thank you for using it!
* Class Name: Cache (For Php5)
* Version: 1.0
* Description: a dynamic cache class used to control automatic page cache generation, call cache, update cache, and delete cache.
* Author: jiangjun8528@163.com, Junin
* Author Page: http://blog.csdn.Net/sdts/
* Last Modify: 2007-8-22
* Remark:
1. this version is Php5. I have not written the Php4 version yet. if you need it, please refer to it for modification. (it's easy. Don't be so lazy !).
2. this version is UTF-8 encoded. if the website uses other encoding, convert it by yourself. in Windows, use notepad to open and save it as a file, and select the corresponding encoding (generally ANSI ), in Linux, use the corresponding editing software or the iconv command line.
3. copy and paste the above 2nd entries.
* Some thoughts on cache:
* Dynamic
Why :? Php class Cache {/*** $ dir: Cache file storage directory * $ lifetime: Cache file validity period, in seconds * $ cacheid: Cache file path, including file name * $ ext: slow...