A good PHP file cache class file _ PHP Tutorial

Source: Internet
Author: User
A good php file cache file. I recommend a good php file cache file for you. from all aspects, this cache class is reasonable and suitable for large websites. if you need it, please refer to it. The code is as follows: I will give you a good php file cache file. from all aspects, this cache class is reasonable and suitable for large websites. if you need it, please refer to it.
The code is as follows:

Class Cache {
/** Cache directory **/
Var $ CacheDir = './C ';
/** Cached files **/
Var $ CacheFile = '';
/** File cache time (minutes )**/
Var $ CacheTime = 0;
/** Whether the file is cached **/
Var $ CacheFound = False;
/** Error and debugging information **/
Var $ DebugMsg = NULL;

Function Cache ($ CacheTime = 0 ){
$ This-> CacheTime = $ CacheTime;
}

Private function Run (){
/** The cache time is greater than 0. it detects the modification time of the cached file. it is the cache file name within the cache time. if the cache time exceeds the cache time, it is False,
If the value is less than or equal to 0, false is returned and cached files are cleared.
**/
Return $ this-> CacheTime? $ This-> CheckCacheFile (): $ this-> CleanCacheFile ();
}
Function GetCache ($ VistUrl, $ CacheFileType = 'html ')
{
$ This-> SetCacheFile ($ VistUrl, $ CacheFileType );

$ FileName = $ this-> CheckCacheFile ();
If ($ fileName)
{
$ Fp = fopen ($ fileName, "r ");
$ Content _ = fread ($ fp, filesize ($ fileName ));
Fclose ($ fp );
Return $ content _;
}
Else
{
Return false;
}
}
Private function SetCacheFile ($ VistUrl, $ CacheFileType = 'html '){
If (empty ($ VistUrl )){
/** Index.html **/
$ This-> CacheFile = 'index ';
} Else {
/** When the parameter is $ _ POST **/
$ This-> CacheFile = is_array ($ VistUrl )? Implode ('.', $ VistUrl): $ VistUrl;
}
$ This-> CacheFile = $ this-> CacheDir. '/'. md5 ($ this-> CacheFile );
$ This-> CacheFile. = '.'. $ CacheFileType;
}

Function SetCacheTime ($ t = 60 ){
$ This-> CacheTime = $ t;
}

Private function CheckCacheFile (){
If (! $ This-> CacheTime |! File_exists ($ this-> CacheFile) {Return False ;}
/** Compare the time difference between the file creation/modification date and the current date **/
$ GetTime = (Time ()-Filemtime ($ this-> CacheFile)/(60*1 );
/** The Filemtime function has a cache. Be sure to clean it **/
Clearstatcache ();
$ This-> Debug ('Time Limit '. ($ GetTime * 60).'/'. ($ this-> CacheTime * 60 ).'');
$ This-> CacheFound = $ GetTime <= $ this-> CacheTime? $ This-> CacheFile: False;
Return $ this-> CacheFound;
}

Function SaveToCacheFile ($ VistUrl, $ Content, $ CacheFileType = 'html '){
$ This-> SetCacheFile ($ VistUrl, $ CacheFileType );
If (! $ This-> CacheTime ){
Return False;
}
/** Check whether the cache directory exists **/
If (true ===$ this-> CheckCacheDir ()){
$ CacheFile = $ this-> CacheFile;
$ CacheFile = str_replace ('//', '/', $ CacheFile );
$ Fp = @ fopen ($ CacheFile, "wb ");
If (! $ Fp ){
$ This-> Debug ('open file'. $ CacheFile. 'fail ');
} Else {
If (@! Fwrite ($ fp, $ Content )){
$ This-> Debug ('write'. $ CacheFile. 'fail ');
} Else {
$ This-> Debug ('cached file ');
};
@ Fclose ($ fp );
}
} Else {
/** The cache directory does not exist or cannot be created **/
$ This-> Debug ('cache folder'. $ this-> CacheDir. 'not found ');
}
}

Private function CheckCacheDir (){
If (file_exists ($ this-> CacheDir) {Return true ;}
/** Save the current working directory **/
$ Location = getcwd ();
/** Divide the path into a single directory **/
$ Dir = split ("/", $ this-> CacheDir );
/** Create a directory cyclically **/
$ CatchErr = True;
For ($ I = 0; $ I If (! File_exists ($ Dir [$ I]) {
/** If a directory fails to be created, False is returned, and the return value of creating the last Directory is returned **/
$ CatchErr = @ mkdir ($ Dir [$ I], 0777 );
}
@ Chdir ($ Dir [$ I]);
}
/** Switch to the original directory after creation **/
Chdir ($ Location );
If (! $ CatchErr ){
$ This-> Debug ('create folder'. $ this-> CacheDir. 'fail ');
}
Return $ CatchErr;
}

Private function CleanCacheFile (){
If (file_exists ($ this-> CacheFile )){
@ Chmod ($ this-> CacheFile, 777 );
@ Unlink ($ this-> CacheFile );
}
/** Set no cached file **/
$ This-> CacheFound = False;
Return $ this-> CacheFound;
}

Function Debug ($ msg = ''){
If (DEBUG ){
$ This-> DebugMsg [] = '[Cache]'. $ msg;
}
}

Function GetError (){
Return empty ($ this-> DebugMsg )? '':"
N ". implode ("
N ", $ this-> DebugMsg );
}
}/* End of class */


?>


Bytes. The code is as follows...

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.