A php Cache class with three Demo code instances. A php cache class with three Demo code instances. This article is collected from the network by the fire editor. The first thing we can see is cache. inc. php file. Save the following code as cache. inc is a PHP cache class with three Demo code instances. This article is collected on the network by the fire editor. The first thing we can see is cache. inc. php file. Save the following code as cache. inc. php.
Copy to ClipboardReference: [www.bkjia.com]
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 the cached 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 .'
';
}
}
?>
The following are three DEMO instances. Save the following code by demo. php or another file name. make sure that the file path is correct.
/*
* 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/
* Source code from: fire download http://www.bkjia.com/down
* 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:
* The fundamental difference between dynamic cache and static cache is that it is automatic. when a user accesses a page, the cache is generated, browsed, and updated without manual intervention.
* Static cache refers to the generation of static pages. operations are generally completed in the website background and must be manually performed (that is, manually generated ).
*/
/*
* Example
------------------------------------ Demo1 -------------------------------------------
Require_once ('cache. inc. php ');
$ Cachedir = './Cache/'; // sets the Cache directory.
$ Cache = new Cache ($ cachedir, 10); // if the parameter is omitted, the default setting is used. $ cache = new Cache ($ cachedir );
If ($ _ GET ['cacheac']! = 'Rewrite') // Here is a trick, through xx. Php? Cacheact = rewrite: updates the cache, and so on. you can also set some other operations.
$ Cache-> load (); // load the cache. if the cache is valid, the following page code is not executed.
// Page code starts
Echo date ('H: I: s jS f ');
// The page code ends.
$ Cache-> write (); // The first time the cache is run or expires, the cache is generated
------------------------------------ Demo2 -------------------------------------------
Require_once ('cache. inc. php ');
$ Cachedir = './Cache/'; // sets the Cache directory.
$ Cache = new Cache ($ cachedir, 10); // if the parameter is omitted, the default setting is used. $ cache = new Cache ($ cachedir );
If ($ _ GET ['cacheac']! = 'Rewrite') // Here is a trick, through xx. Php? Cacheact = rewrite: updates the cache, and so on. you can also set some other operations.
$ Cache-> load (); // load the cache. if the cache is valid, the following page code is not executed.
// Page code starts
$ Content = date ('H: I: s jS f ');
Echo $ content;
// The page code ends.
$ Cache-> write (1, $ content); // The cache is generated when the first operation or cache expires.
------------------------------------ Demo3 -------------------------------------------
Require_once ('cache. inc. php ');
Define ('cacheenable', true );
If (CACHEENABLE ){
$ Cachedir = './Cache/'; // sets the Cache directory.
$ Cache = new Cache ($ cachedir, 10); // if the parameter is omitted, the default setting is used. $ cache = new Cache ($ cachedir );
If ($ _ GET ['cacheac']! = 'Rewrite') // Here is a trick, through xx. Php? Cacheact = rewrite: updates the cache, and so on. you can also set some other operations.
$ Cache-> load (); // load the cache. if the cache is valid, the following page code is not executed.
}
// Page code starts
$ Content = date ('H: I: s jS f ');
Echo $ content;
// The page code ends.
If (CACHEENABLE)
$ Cache-> write (1, $ content); // The cache is generated when the first operation or cache expires.
*/
?>
Invalid cache. inc...