返回列表 回复 发帖

一个PHP缓存类

  1. class ArrCache
  2. {
  3.         var $ob_length;//前面缓冲的数据量
  4.         var $path;//缓存文件存放路径
  5.         var $fileName;//缓存文件名
  6.         var $contents;//缓存内容
  7.         var $cached;//是否缓存
  8.         var $ext;//缓存文件扩展名
  9.        
  10.         /*
  11.          * 构造函数,设定缓存文件存放目录,是否使用gzip
  12.          * 缓存路径,“e:\localhost\cache”这样的绝对路径也可以
  13.          */
  14.         public function ArrCache($path = '/cache', $ext = 'ach', $gzip = false)
  15.         {
  16.                 if ($gzip && ereg('gzip',$_SERVER['HTTP_ACCEPT_ENCODING']))
  17.                 {
  18.                         ob_start('ob_gzhandler');
  19.                 }
  20.                 else
  21.                 {
  22.                         ob_start();
  23.                 }
  24.                 $this->ext = $ext;
  25.                 if (substr($path, 1, 1) == ':')
  26.                 {
  27.                         $this->path = $path;
  28.                 }
  29.                 else
  30.                 {
  31.                         //$this->path = $_SERVER["DOCUMENT_ROOT"].'\\'.$path;
  32.                         $this->path = $_SERVER["DOCUMENT_ROOT"].$path;
  33.                 }//echo $this->path;
  34.         }
  35.         /*
  36.          * 开始缓存
  37.          * parameter 缓存变量,必须唯一,用它来区分不同的缓存块
  38.          * time 缓冲时间,单位秒
  39.          */
  40.         public function begin($parameter = array(), $time = 60)
  41.         {
  42.                 $this->fileName = $this->fileName($parameter);
  43.                 $this->contents = ob_get_contents();
  44.                 ob_clean();
  45.                 if (file_exists($this->fileName) && time() - filemtime($this->fileName) <= $time)
  46.                 {
  47.                         $fp       = fopen($this->fileName, 'r');
  48.                         flock($fp, LOCK_SH);
  49.                         echo fread($fp, filesize($this->fileName));
  50.                         flock($fp, LOCK_UN);
  51.                         fclose($fp);
  52.                         $this->cached = true;
  53.                         return false;
  54.                 }
  55.                 else
  56.                 {
  57.                         $this->cached = false;
  58.                         return true;
  59.                 }
  60.                
  61.         }
  62.         /*
  63.          * 结束缓存
  64.          * replace 为局部不缓存
  65.          */
  66.         public function end($replace = array())
  67.         {
  68.                 $key = array();
  69.                 $value = array();
  70.                
  71.                 $contents = ob_get_contents();
  72.                
  73.                 if (!$this->cached)
  74.                 {
  75.                         $fp       = fopen($this->fileName, "w");
  76.                         flock($fp, LOCK_EX);
  77.                         fwrite($fp, $contents);
  78.                         flock($fp, LOCK_UN);
  79.                         fclose($fp);
  80.                 }
  81.                
  82.                 ob_clean();
  83.                 if (is_array($replace))
  84.                 {
  85.                         foreach($replace as $_key => $_value)
  86.                         {
  87.                                 $key[] = '';
  88.                                 $value[] = $_value;
  89.                         }
  90.                         $contents = str_replace($key, $value, $contents);
  91.                 }
  92.                 echo $this->contents.$contents;
  93.         }
  94.         public function clean($filter)
  95.         {
  96.                 $array1 = array(' ', '+', '/', '\\', ':', '"', '<', '>', '|', '=', '-');
  97.                 $array2 = array('+0', '+1', '+2', '+3', '+4', '+7', '+8', '+9', '+a', '+b', '+c');
  98.                 $filter = str_replace($array1, $array2, $filter);
  99.                
  100.                 $out = array('right' => array(), 'wrong' => array());
  101.                
  102.                 $fileNames = glob($this->path.'\\'.$filter.'.'.$this->ext);
  103.                 foreach ($fileNames as $filename)
  104.                 {
  105.                     if (unlink($filename))
  106.                     {
  107.                             $out['right'][] = $filename;
  108.                     }
  109.                     else
  110.                     {
  111.                             $out['wrong'][] = $filename;
  112.                     }
  113.                 }
  114.                 return $out;
  115.         }
  116.         private function fileName($parameter)
  117.         {
  118.                 $array  = array();
  119.                 $array1 = array(' ', '+', '/', '\\', ':', '*', '?', '"', '<', '>', '|', '=', '-');
  120.                 $array2 = array('+0', '+1', '+2', '+3', '+4', '+5', '+6', '+7', '+8', '+9', '+a', '+b', '+c');
  121.                
  122.                 if (is_array($parameter))
  123.                 {
  124.                         foreach ($parameter as $key => $value)
  125.                         {
  126.                                 $array[] = $key.'='.str_replace($array1, $array2, $value);
  127.                         }
  128.                 }
  129.                 $string = join($array, '');
  130.                 $file=end(explode('/',$_SERVER['PHP_SELF']));
  131. //                $filename = $this->path.'\\'.str_replace($array1, $array2, substr($_SERVER["SCRIPT_NAME"], 1,
  132. //strlen($_SERVER["SCRIPT_NAME"]) - 1)).$string.'.'.$this->ext;
  133. $filename = $this->path.'/'.$file.$string.'.'.$this->ext;//die($this->path);
  134.                 return $filename;
  135.         }
  136. }
  137. ?>
复制代码
您的支持,是我们最好的动力,只有一个目标,为大家提供最好的软件,尽量使系统优化,一切以站长能赚到钱为跟本出发点。

健康是人生第一财富

健康是人生第一财富:活到百岁纵贯车这是一本让你终生受益的书,它将带给你的是世上最贵重的礼物:“健康快乐100岁,每天都有好心情。”什么是好心情呢?就是好心加好情。好心是爱心、善心和真心;好情是友情、亲情和爱情。有了好心情,你会感到这个世界真夸姣,处处布满阳光,糊口布满但愿。你会全然忘却忧愁烦恼。本书用浅显、生动的语言把最新的医学知识和理念编成“一听就懂,一懂就用,一用就灵”的庶民语言,平易近人,妙趣横生,使医学保健知识走入亿万民众心中,成为泛博民众促进健康的有益指南。“实际步履的一步赛过一打纲领”。最好是从小做起,从小事做起,从现在做起,由于很多疾病发源于少年,植根于青年,发展于中年,发病于老年,预防为主,未雨绸缪,才是上策。所以要读书,有了知识,还要有感悟,最主要的是还要有步履。知,信,行,同一才有气力。




军刀购买
购买军刀
返回列表