Php file cache technology implementation code. The following is a php Cache file implementation class. Based on my experience, the cache file is compared with the file generation day and current time based on the time set by the user, then determine whether the following is a php Cache file implementation class. Based on my experience, the cache file is compared with the file generation time and the current time based on the time set by the user, then determine whether to regenerate the cache file. */
The following is a php Tutorial cache file implementation class. Based on my experience, the cache file is compared with the file generation time and the current time based on the time set by the user, then determine whether to regenerate the cache file. */
Class pagecache {
/**
* @ Var string $ file cache file address
* @ Access public
*/
Public $ file;
/**
* @ Var int $ cachetime cache time
* @ Access public
*/
Public $ cachetime = 3600;
/**
* Constructor
* @ Param string $ file cache file address
* @ Param int $ cachetime cache time
*/
Function _ construct ($ file, $ cachetime = 3600 ){
$ This-> file = $ file;
$ This-> cachetime = $ cachetime;
}
/**
* Cache content retrieval
* @ Param bool: whether to directly output data. if it is set to true, it is directly transferred to the cache page. if it is set to false, the cached content is returned.
* @ Return mixed
*/
Public function get ($ output = true ){
If (is_file ($ this-> file) & (time ()-filemtime ($ this-> file) <= $ this-> cachetime &&! $ _ Get ['nocache']) {
If ($ output ){
Header ('Location: '. $ this-> file );
Exit;
} Else {
Return file_get_contents ($ this-> file );
}
} Else {
Return false;
}
}
/**
* Set cache content
* @ Param $ content: html string
*/
Public function set ($ content ){
$ Fp = fopen ($ this-> file, 'w ');
Fwrite ($ fp, $ content );
Fclose ($ fp );
}
}
...