php专区

 首页 > php专区 > PHP应用 > php类库 > php文件缓存类文件 - php类库

php文件缓存类文件 - php类库

分享到:
【字体:
导读:
          本人给大家推一个不错php文件缓存类文件,从各方面来看本缓存类很合理并且适用于大型网站使用,php文件缓存类文件代码如下:?phpclassCache{/**缓存目录**/var$CacheDir=#39;./c#39;;/**缓存的文件...

php文件缓存类文件

本人给大家推一个不错php文件缓存类文件,从各方面来看本缓存类很合理并且适用于大型网站使用,php文件缓存类文件代码如下:

  1. class Cache { 
  2.  /** 缓存目录 **/ 
  3.  var $CacheDir        = './c'
  4.  /** 缓存的文件 **/ 
  5.  var $CacheFile        = ''
  6.  /** 文件缓存时间(分钟) **/ 
  7.  var $CacheTime        = 0; 
  8.  /** 文件是否已缓存 **/ 
  9.  var $CacheFound        = False; 
  10.  /** 错误及调试信息 **/ 
  11.  var $DebugMsg        = NULL; 
  12.  
  13.  function Cache($CacheTime = 0) { 
  14.   $this->CacheTime    = $CacheTime
  15.  } 
  16.  
  17.  private function Run() { 
  18.   /** 缓存时间大于0,检测缓存文件的修改时间,在缓存时间内为缓存文件名,超过缓存时间为False, 
  19.                 小于等于0,返回false,并清理已缓存的文件 
  20.          **/ 
  21.   Return $this->CacheTime ? $this->CheckCacheFile() : $this->CleanCacheFile(); 
  22.  } 
  23.  function GetCache($VistUrl,$CacheFileType = 'html'
  24.  { 
  25.   $this->SetCacheFile($VistUrl,$CacheFileType); 
  26.  
  27.   $fileName=$this->CheckCacheFile(); 
  28.   if($fileName
  29.   { 
  30.    $fp = fopen($fileName,"r"); 
  31.    $content_fread($fpfilesize($fileName)); 
  32.    fclose($fp); 
  33.    return $content_
  34.   } 
  35.   else 
  36.   { 
  37.    return false; 
  38.   } 
  39.  } 
  40.  private function SetCacheFile($VistUrl,$CacheFileType = 'html') { 
  41.   if(emptyempty($VistUrl)) { 
  42.    /** 默认为index.html **/ 
  43.    $this->CacheFile = 'index'
  44.   }else { 
  45.    /** 传递参数为$_POST时 **/ 
  46.    $this->CacheFile = is_array($VistUrl) ? implode('.',$VistUrl) : $VistUrl
  47.   } 
  48.   $this->CacheFile = $this->CacheDir.'/'.md5($this->CacheFile); 
  49.   $this->CacheFile.= '.'.$CacheFileType
  50.  } 
  51.  
  52.  function SetCacheTime($t = 60) { 
  53.   $this->CacheTime = $t
  54.  } 
  55.  
  56.  private function CheckCacheFile() { 
  57.   if(!$this->CacheTime || !file_exists($this->CacheFile)) {Return False;} 
  58.   /** 比较文件的建立/修改日期和当前日期的时间差 **/ 
  59.   $GetTime=(Time()-Filemtime($this->CacheFile))/(60*1); 
  60.   /** Filemtime函数有缓存,注意清理 **/ 
  61.   Clearstatcache(); 
  62.   $this->Debug('Time Limit '.($GetTime*60).'/'.($this->CacheTime*60).''); 
  63.   $this->CacheFound = $GetTime <= $this->CacheTime ? $this->CacheFile : False; 
  64.   Return $this->CacheFound; 
  65.  } 
  66.  
  67.  function SaveToCacheFile($VistUrl,$Content,$CacheFileType = 'html') { 
  68.   $this->SetCacheFile($VistUrl,$CacheFileType); 
  69.   if(!$this->CacheTime) { 
  70.    Return False; 
  71.   } 
  72.   /** 检测缓存目录是否存在 **/ 
  73.   if(true === $this->CheckCacheDir()) { 
  74.    $CacheFile = $this->CacheFile; 
  75.    $CacheFile = str_replace('//','/',$CacheFile); 
  76.    $fp = @fopen($CacheFile,"wb"); 
  77.    if(!$fp) { 
  78.     $this->Debug('Open File '.$CacheFile.' Fail'); 
  79.    }else { 
  80.     if(@!fwrite($fp,$Content)){ 
  81.      $this->Debug('Write '.$CacheFile.' Fail'); 
  82.     }else { 
  83.      $this->Debug('Cached File'); 
  84.     }; 
  85.     @fclose($fp); 
  86.    } 
  87.   }else { 
  88.    /** 缓存目录不存在,或不能建立目录 **/ 
  89.    $this->Debug('Cache Folder '.$this->CacheDir.' Not Found'); 
  90.   } 
  91.  } 
  92.  
  93.  private function CheckCacheDir() { 
  94.   if(file_exists($this->CacheDir)) { Return true; } 
  95.   /** 保存当前工作目录 **/ 
  96.   $Location = getcwd(); 
  97.   /** 把路径划分成单个目录 **/ 
  98.   $Dir = split("/"$this->CacheDir); 
  99.   /** 循环建立目录 **/ 
  100.   $CatchErr = True; 
  101.   for ($i=0; $i<count($Dir); $i++){ 
  102.    if (!file_exists($Dir[$i])){ 
  103.     /** 建立目录失败会返回False 返回建立最后一个目录的返回值 **/ 
  104.     $CatchErr = @mkdir($Dir[$i],0777); 
  105.    } 
  106.    @chdir($Dir[$i]); 
  107.   } 
  108.   /** 建立完成后要切换到原目录 **/ 
  109.   chdir($Location); 
  110.   if(!$CatchErr) { 
  111.    $this->Debug('Create Folder '.$this->CacheDir.' Fail'); 
  112.   } 
  113.   Return $CatchErr
  114.  } 
  115.  
  116.  private function CleanCacheFile() { 
  117.   if(file_exists($this->CacheFile)) { 
  118.    @chmod($this->CacheFile,777); 
  119.    @unlink($this->CacheFile); 
  120.   } 
  121.   /** 置没有缓存文件 **/ 
  122.   $this->CacheFound = False; 
  123.   Return $this->CacheFound; 
  124.  } 
  125.  
  126.  function Debug($msg='') { 
  127.   if(DEBUG) { 
  128.    $this->DebugMsg[] = '[Cache]'.$msg
  129.   } 
  130.  } 
  131.  
  132.  function GetError() { 
  133.   Return emptyempty($this->DebugMsg) ? '' : "
    n"
    .implode("
    n"
    ,$this->DebugMsg);//开源代码phpfensi.com 
  134.  } 
  135. }/* end of class */ 
  136. ?>
分享到:
PHP ZIP压缩类代码 - php类库
PHP ZIP压缩类代码 本程序可以快速的实现把我们的文件利用php压缩类压缩成我们想的zip,或者rar 的压缩包,后缀名可以自定义,压缩算法是来自国外一个网站抄的. 首先实例化,然后传参,两个参数,第一个关于你文件地址的一个Array,第二个是要你要保存的压缩包文件的绝对地址. For example,代码如...
把php生成静态(html)页面程序代码 - php...
把php生成静态(html)页面程序代码 生成静态页面一般是把动态页面生成html页面,这样可以减少服务器负载也是现在各大网站常用的优化方法,下面我来分享一个把php生成静态(html)页面类.  
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……