Introduction to php's simple cache full-site functions

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

  1. Function cache_page ($ refresh = 20 ){
  2. Ob_start (); // enable the buffer
  3. $ Hash = sha1 ($ _ SERVER [PHP_SELF]. | G |. serialize ($ _ GET). | P |. serialize ($ _ POST); // cache file name
  4. $ File = dirname (_ FILE _)./cache/. $ hash; // cache file path
  5. If (! File_exists ($ file) {// The cached file does not exist
  6. Register_shutdown_function (cache_page_go, $ file );
  7. } Else {// The cached file exists
  8. If (time ()-filemtime ($ file)> $ refresh) {// cache timeout
  9. Register_shutdown_function (cache_page_go, $ file); // call a function
  10. }
  11. Else {// cache file for normal use
  12. $ F = file_get_contents ($ file); // retrieves the cached file content
  13. Echo $ f. cached content. // outputs the cached content.
  14. $ Output = ob_get_contents (); // retrieves the buffer content
  15. Ob_get_clean (); // clear the buffer
  16. Echo $ output; // output
  17. Exit ();
  18. }
  19. }
  20. }
  21. Function cache_page_go ($ file ){
  22. $ Output = ob_get_contents (); // Obtain the buffer content
  23. Ob_get_clean (); // clear the buffer
  24. File_put_contents ($ file, $ output, LOCK_EX); // write the cached file
  25. Echo $ output. newly created. // outputs the cached content.
  26. Exit ();
  27. }
  28. ?>

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.