生成缓存:

  1. <?php 
  2.     if($_GET['phphuancun']!="true") { 
  3.         define("HC_PATH",dirname(__FILE__)."/cache/"); 
  4.         define("HC_TIME",1); 
  5.         echo HC_getcache();exit
  6.     } 
  7.      
  8.     function HC_getcache($iscache='') { 
  9.         $url="http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
  10.         $cacheurl=strpos($url,"?")?$url."&phphuancun=true":$url."?phphuancun=true"
  11.         $cachename=HC_PATH.md5($url).".c"
  12.   
  13.         $cachetime=$iscache?time()+1:time()-(HC_TIME * 60*60); 
  14.         if(file_exists($cachename) && filemtime($cachename)>=$cachetime) { 
  15.             $return=file_get_contents($cachename); 
  16.             $data=function_exists(gzcompress)?@gzuncompress($return):$return
  17.             return unserialize($data); 
  18.         } else { 
  19.             $return=file_get_contents($cacheurl); 
  20.             HC_writecache($cachename,$return); 
  21.             return $return
  22.         } 
  23.     } 
  24.      
  25.     function HC_writecache($name,$array) { 
  26.         function_exists(gzcompress)?$return =gzcompress(serialize($array)):$return=serialize($array); 
  27.         @file_put_contents($name,$return); 
  28.     } 
  29. ?> 

 

 清除缓存:

  1. <?php 
  2. header("content-Type: text/html; charset=utf-8"); 
  3. //配置开始 
  4. $path="huancun/cache";//在些设置所删除的目录.为当前目录 如:删除path目录,引号里请添path; 
  5. $guolv="del.php,huancun.php,huancun";//设置需要过滤的文件或文件夹用英文状态下,号分隔 
  6. //配置结束 
  7. if($_GET['action']=="del"){ 
  8.     $file= array_values_recursive(recurdir($path,$guolv)); 
  9.     foreach($file as $k => $v){ 
  10.         remove_directory($v); 
  11.     } 
  12. }else
  13.     echo "您的配置如下<br> 
  14.     要删除的目录为: 
  15.     "; 
  16.     if($path==".")echo "当前目录";else echo $path
  17.     echo "<br>您要过滤的文件或文件夹有:".$guolv."<br> 
  18.     如果确认过滤请<a href='?action=del'>点击此处开始删除相应的目录及目录下的所有文件</a>,如果配置不正确请到文件中修改 
  19.     "; 
  20.  
  21.  
  22. //删除目录及文件 
  23. function remove_directory($dir) { 
  24.   foreach(glob($diras $fn) {  
  25.         echo " removing $fn<br>\n"
  26.         if (!is_writable($fn))@chmod($fn, 0777); 
  27.         if(is_dir($fn)){@rmdir($fn);}else{@unlink($fn);} 
  28.    }  
  29. //扫描目录 
  30. function recurdir($pathname,$guolv='del.php'
  31.     $result=array();$temp=array(); 
  32.     //检查目录是否有效和可读 
  33.     if(!is_dir($pathname) || !is_readable($pathname)) 
  34.     return null; 
  35.     //得到目录下的所有文件夹 
  36.     $allfiles=scandir($pathname); 
  37.     foreach($allfiles as $key => $filename
  38.     { 
  39.         //如果是“.”或者“..”的话则略过 
  40.         if(in_array($filename,array('.','..')))continue
  41.         if(count($guolv)>0){$lv=explode(",",$guolv);if(in_array($filename,$lv))continue;} 
  42.          
  43.         //得到文件完整名字 
  44.         $fullname =$pathname . "/" .$filename
  45.         //如果该文件是目录的话,递归调用recurdir 
  46.         $temp[]=$fullname
  47.         if(is_dir($fullname)){ 
  48.             $nowpath=explode("/",$fullname); 
  49.             if(count($guolv)>0){$lv=explode(",",$guolv);if(in_array($nowpath[count($nowpath)-1],$lv))continue;} 
  50.             $result[$filename] = recurdir($fullname);} 
  51.     }    
  52.     //最后把临时数组中的内容添加到结果数组,确保目录在前,文件在后 
  53.     foreach($temp as $f){ 
  54.         $result[]=$f
  55.     } 
  56.     return $result
  57. //获取所有文件 
  58. function array_values_recursive($ary
  59.    $lst = array(); 
  60.    foreacharray_keys($aryas $k ){ 
  61.      $v = $ary[$k]; 
  62.      if (is_array($v)) {$lst = array_merge$lst, array_values_recursive($v));}else{$lst[] = $v;} 
  63.    } 
  64.    return $lst
  65. ?>