PHP file cache technology implementation code _ PHP Tutorial

Source: Internet
Author: User
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 );
}
}


...

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.