首頁  >  文章  >  後端開發  >  php 简单的缓存全站函数介绍_PHP教程

php 简单的缓存全站函数介绍_PHP教程

WBOY
WBOY原創
2016-07-13 17:40:38990瀏覽

php  简单的缓存全站函数介绍

  1. function cache_page($refresh=20){
  2. ob_start();//开启缓冲区
  3. $hash=sha1($_SERVER[PHP_SELF].|G|.serialize($_GET).|P|.serialize($_POST)); //缓存文件名字
  4. $file=dirname(__FILE__)./cache/.$hash;//缓存文件路径
  5. if(!file_exists($file)) {//缓存文件不存在
  6.    register_shutdown_function(cache_page_go,$file);
  7. }else{// 缓存文件存在
  8.    if( (time()-filemtime($file))>$refresh ){//缓存超时
  9.     register_shutdown_function(cache_page_go,$file);// 调用函数
  10.    }
  11.    else{//正常使用缓存文件
  12.     $f=file_get_contents($file);// 取出缓存文件内容
  13.     echo $f.缓存的哦;//输出缓存内容
  14.     $output=ob_get_contents(); //取出缓冲区内容
  15.     ob_get_clean();    //清空缓冲区
  16.     echo $output;      //输出
  17.     exit();
  18.    }
  19. }
  20. }
  21. function cache_page_go($file){
  22. $output=ob_get_contents();//获取缓冲区内容
  23.    ob_get_clean();           //清空缓冲区
  24.    file_put_contents($file,$output,LOCK_EX);//写入缓存文件
  25.    echo $output.新建的哦;//输出缓存内容
  26.    exit();
  27. }
  28. ?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486191.htmlTechArticlephp 简单的缓存全站函数介绍 ?php function cache_page($refresh=20){ ob_start();//开启缓冲区 $hash=sha1($_SERVER[PHP_SELF].|G|.serialize($_GET).|P|.serialize($_POST));...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn