+ -
当前位置:首页 → 问答吧 → 一个简单的cache管理类

一个简单的cache管理类

时间:2007-07-08

来源:互联网

第一次在phpchina上面发贴, 希望各位朋友多多支持, 如果各位用上了, 还请在此吼一声。 也欢迎在此建议改进。 谢谢
复制内容到剪贴板
代码:
<?

/*
  Class:   Simple cache management class.
  Version:   0.0.2
  Last update: 2007-07-07
  Author:  wimax
  URL:     [url=http://www.embedworld.com]http://www.embedworld.com[/url]
*/


  define("ERR_FOPEN", -1);
  define("ERR_READABLE", -2);
  define("ERR_FWRITE", -3);
  define("ERR_NULL", -4);
  define("INFO_OKAY", 0);

class cacheMgr {
  var $_cache_dir;
  var $_cacheFileName;
  var $_content;
  var $_cache_timespan;
  
  function cacheMgr() {
     $this->_cache_dir="./cache";
     $this->set_cache_timespan();
  }
  
  function set_cache_dir($cache_dir="") {
     $this->_cache_dir=preg_replace("#[\\\/]$#", "", $cache_dir);
  }
  
  function get_cache_dir($cache_dir="") {
     return $this->_cache_dir;
  }
  
  function set_cache_timespan($seconds=2592000) {  // 缺省cache生效的时间是一个月 (one month)
     $this->_cache_timespan=$seconds;
  }
  
  function get_cache_timespan() {
     return $this->_cache_timespan;
  }  
  
  function set_cache_file_name($params=array()) {
    $cacheFileName="";
    foreach ($params as $key => $value) {
       $cacheFileName .= "_".$key."_".$value;
    }
   
    //调试的时候用这个
    $this->_cacheFileName= $this->_cache_dir."/".$cacheFileName.".tmp";
   
    // 实际发布的时候用这个
    // $this->_cacheFileName= $this->_cache_dir."/".md5($cacheFileName).".tmp";
  }
  
  
  function read_cache_content($params=array()) {
    $this->_c;
    $this->set_cache_file_name($params);
    if (is_readable($this->_cacheFileName)) {
      $this->_content = file_get_contents($this->_cacheFileName);
    } else {
        return ERR_READABLE;
    }
   
    return $this->_content;   
  }
  
  
  /* Recommended params list:
     id
     module
     script_name
     block_name
     url_remaining_part
     
     */
     
  function write_cache_content($c, $params=array()) {
    $this->set_cache_file_name($params);
    /* dump cache content to local disk */
    if ($fp=fopen($this->_cacheFileName, "wb+")) {
       $this->_content=$content;
      if(fwrite($fp, $content)) {
        fclose($fp);
        return INFO_OKAY;
      } else {
        fclose($fp);
        return ERR_FWRITE;
      }
      
    } else {
       return ERR_FOPEN;
    }
  }

  function is_cache_exist($params=array()) {
     $this->set_cache_file_name($params);
     return file_exists($this->_cacheFileName);
  }


  function get_cache_update_time($params=array()) {
    if (!empty($params)) {
       $this->set_cache_file_name($params);
    }
    $fStat=stat($this->_cacheFileName);
   
    $fTime=$fStat["mtime"] < $fStat["ctime"] ? $fStat["mtime"] : $fStat["ctime"];
    return $fTime;
  }
  
  function is_cache_not_expired($params=array()) {
     $now=time();
     $cache_file_update_time=$this->get_cache_update_time();
     if (!empty($params)) {
       $this->set_cache_file_name($params);
     }
     return ( ($cache_file_update_time + $this->_cache_timespan) > $now);  // one month
  }


  function is_cache_health($params=array()) {
     return $this->is_cache_exist($params) &&  $this->is_cache_not_expired();
  }

}

/* 使用示例  */

if (0) {
   $cacheMgr=new cacheMgr;
   $cacheMgr->set_cache_dir("cache/");
   print "cache_dir: ". $cacheMgr->get_cache_dir()."\n";
   $pas=array("module"=>"test1", "block" => "block1");
   if ($cacheMgr->is_cache_health($pas)) {
      $content=$cacheMgr->read_cache_content($pas);
      print "haha: $content";
   } else {
      ob_start();
      print "hello world\n";
      $output=ob_get_contents();
      ob_end_flush();
      $cacheMgr->write_cache_content($output, $pas);
   }
}

?>

作者: wimax   发布时间: 2007-07-08

作者: wukeyuan   发布时间: 2007-07-08

不错

作者: fengyun   发布时间: 2007-07-09

把注释写上哈

作者: FLASH百强   发布时间: 2007-07-09

发现Discuz论坛显示的时候替换了一些字段。 譬如write_cache_content函数的第一个参数是$content, 结果替换成了$c。 read_cache_content函数的第一行应该是 $this->_content =“”;

代码中各个函数的命名已经比较清除了, 注释也就免了。
PS: 不知道怎么用Discuz的PHP语法加亮, 请知道的朋友赐教一下。 :)

作者: wimax   发布时间: 2007-07-09