Home > Backend Development > PHP Tutorial > PHP simple caching site-wide function introduction_PHP tutorial

PHP simple caching site-wide function introduction_PHP tutorial

WBOY
Release: 2016-07-13 17:40:38
Original
1052 people have browsed it

php Simple cache site-wide function introduction

  1. function cache_page($refresh=20){
  2. ob_start();//Open 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 cache file does not exist
  6. register_shutdown_function(cache_page_go,$file);
  7. }else{// The cache file exists
  8. if( (time()-filemtime($file) )>$refresh ){//Cache timeout
  9. register_shutdown_function(cache_page_go,$file);//Call function
  10. }
  11. else{//Use cache file normally
  12. $f=file_get_contents($file);// Get cached file content
  13. echo $f. Cached;//Output cached content
  14. $output=ob_get_contents (); //Get 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(); //Get buffer contents
  23. ob_get_clean(); //Clear buffer
  24. file_put_contents($file,$output,LOCK_EX);//Write cache file
  25. echo $output. Newly created;//Output cache content
  26. exit();
  27. }
  28. ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486191.htmlTechArticlephp Simple cache full-site function introduction?php function cache_page($refresh=20){ ob_start();/ /Open buffer $hash=sha1($_SERVER[PHP_SELF].|G|.serialize($_GET).|P|.serialize($_POST));...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template