当前位置:编程学习 > php >>

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

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

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

补充:Web开发 , php ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,