Php & nbsp; brief introduction to cache full-site functions & lt ;? Phpfunctioncache_page ($ refresh20) {ob_start (); // enable the buffer $ hashsha1 ($ _ SERVER [PHP_SELF]. | G |. serialize ($ _ GET ). | P |. serialize ($ _ POST); // cache file name $ f Introduction to php simple cache full-site functions
-
- Function cache_page ($ refresh = 20 ){
- Ob_start (); // enable the buffer
- $ Hash = sha1 ($ _ SERVER [PHP_SELF]. | G |. serialize ($ _ GET). | P |. serialize ($ _ POST); // cache file name
- $ File = dirname (_ FILE _)./cache/. $ hash; // cache file path
- If (! File_exists ($ file) {// The cached file does not exist
- Register_shutdown_function (cache_page_go, $ file );
- } Else {// The cached file exists
- If (time ()-filemtime ($ file)> $ refresh) {// cache timeout
- Register_shutdown_function (cache_page_go, $ file); // call a function
- }
- Else {// cache file for normal use
- $ F = file_get_contents ($ file); // retrieves the cached file content
- Echo $ f. cached content. // outputs the cached content.
- $ Output = ob_get_contents (); // retrieves the buffer content
- Ob_get_clean (); // clear the buffer
- Echo $ output; // output
- 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. newly created. // outputs the cached content.
- Exit ();
- }
- ?>