Introduction to php's simple cache full-site functions

Source: Internet
Author: User

Introduction to php's simple cache full-site functions

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

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.