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.