PHP Cache page method

Source: Internet
Author: User
PHP Cache page functions. **************************************** * ************************ -- Function name: cache_page (including cache_page_go) -- function: easily and quickly cache the entire site -- Parameter: cache time (unit: Seconds) -- Return value: Output Content -- instance: cache_page (300 ); the function uses the PHP Cache page function at the top of the page.

/*************************************** **************************

-- Function name: cache_page (including cache_page_go)
-- For use: easily and quickly cache the entire site
-- Parameter value: cache time (unit: Seconds)
-- Return value: Output Content
-- Example: cache_page (300); the function is used at the top of the page.
**************************************** ***************************/

Function cache_page ($ refresh = 20 ){
Ob_start (); // enable the buffer
$ Temp = sha1 ($ _ SERVER ['php _ SELF ']. '| G | '. serialize ($ _ GET ). '| P | '. serialize ($ _ POST); // cache file name
$ Temp = dirname (_ FILE _). '/cache/'. $ temp; // cache FILE path
If (! File_exists ($ temp) {// The cached file does not exist
Register_shutdown_function ('cache _ page_go ', $ temp );
} Else {// The cached file exists
If (time ()-filemtime ($ temp)> $ refresh) {// cache timeout
Register_shutdown_function ('cache _ page_go ', $ temp); // call the function
} Else {// cache file for normal use
$ Temp = file_get_contents ($ temp); // retrieves the cached file content
Echo $ temp; // outputs the cached content
$ Temp = ob_get_contents (); // retrieves the buffer content
Ob_get_clean (); // clear the buffer
Echo $ temp; // output
Unset ($ temp, $ refresh);/* deregister the variable */
Exit ();
}
}

}


Function cache_page_go ($ file ){
$ Output = ob_get_contents (); // Obtain the buffer content
Ob_get_clean (); // clear the buffer
File_put_contents ($ file, $ output, LOCK_EX); // write the cached file
Echo $ output; // outputs the cached content.
Unset ($ output, $ file);/* deregister variable */
Exit ();

}

We recommend that you place the function at the beginning of the page.

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.