PHP简单的缓存类

PHP简单的缓存类

转载请标明出处: http://blog.csdn.net/mozillafirefox/ 本文来自 CSDN 博客。x

<?php

/*

* 缓存类 cache

* 作     者:多菜鸟

* 联系邮箱: kingerq AT msn DOT com

* 创建时间: 2006-05-05

* 来源: http://blog.csdn.net/kingerq

* 实     例:

includ "cache.php" ;

$cach = new cach 30 ;

$cache->cacheCheck ;

// 缓存内容开始

echo date "Y-m-d H:i:s" ;

// 缓存内容结束

$cache->cach ;

*/

class cach {

  // 缓存目录

  var $cacheRoot        = "./cache/";

0 为不缓存   // 缓存更新时间秒数。

  var $cacheLimitTime   = 0;

  // 缓存文件名

  var $cacheFileName    = "";

  // 缓存扩展名

  var $cacheFileExt     = "php";

  /*

   * 构造函数

   * int $cacheLimitTim 缓存更新时间

   */

  function cach $cacheLimitTim {

    if intval $cacheLimitTim

      $this->cacheLimitTim = $cacheLimitTime;

    $this->cacheFileNam = $this->getCacheFileNam ;

    ob_start ;

  }

 

  /*

   * 检查缓存文件是否在设置更新时间之内

反之则返回失败    * 返回:如果在更新时间之内则返回文件内容。

   */

  function cacheCheck {

    if file_exist $this->cacheFileNam {

      $cTime = $this->getFileCreateTim $this->cacheFileNam ;

      if $cTime + $this->cacheLimitTim > time {

        echo file_get_cont $this->cacheFileNam ;

        ob_end_flush ;

        exit;

      }

    }

    return false;

  }

  /*

   * 缓存文件或者输出静态

   * string $staticFileNam 静态文件名(含相对路径)

   */

  function cach $staticFileNam = "" {

    if $this->cacheFileNam {

      $cacheCont = ob_get_cont ;

      //echo $cacheContent;

      ob_end_flush ;

      if $staticFileNam {

$cacheCont           $this->saveFil $staticFileName.;

      }

      if $this->cacheLimitTim

$cacheCont         $this->saveFil $this->cacheFileName.;

    }

  }

 

  /*

   * 清除缓存文件

   * string $fileNam 指定文件名 ( 含函数 ) 或者 all 全部)

   * 返回:清除胜利返回 true 反之返回 false

   */

  function clearCach $fileNam = "all" {

    if $fileNam != "all" {

      $fileNam = $this->cacheRoot . strtoupper md5 $fileNam .".".$this->cacheFileExt;

      if file_exist $fileNam {

        return @unlink $fileNam ;

      }els return false;

    }

    if is_dir $this->cacheRoot {

      if $dir = @opendir $this->cacheRoot {

        while $file = @readdir $dir {

          $check = is_dir $file ;

          if !$check

          @unlink $this->cacheRoot . $file ;

        }

        @closedir $dir ;

        return true;

      }else{

        return false;

      }

    }else{

      return false;

    }

  }

  /*

   * 根据当前动态文件生成缓存文件名

   */

  function getCacheFileNam {

    return  $this->cacheRoot . strtoupper md5 $_SERVER["REQUEST_URI"] .".".$this->cacheFileExt;

  }

  /*

   * 缓存文件建立时间

   * string $fileName   缓存文件名(含相对路径)

文件不存在返回    * 返回:文件生成时间秒数。0

   */

  function getFileCreateTim $fileNam {

    if ! trim $fileNam return 0;

    if file_exist $fileNam {

      return intval filemtim $fileNam ;

    }els return 0;

  }

 

  /*

   * 保管文件

   * string $fileName  文件名(含相对路径)

   * string $text      文件内容

   * 返回:胜利返回 ture 失败返回 false

   */

$text   function saveFil $fileName.{

    if ! $fileNam || ! $text return false;

    if $this->makeDir dirnam $fileNam {

"w"       if $fp = fopen $fileName.{

$text         if @fwrite $fp.{

          fclose $fp ;

          return true;

        }els {

          fclose $fp ;

          return false;

        }

      }

    }

    return false;

  }

  /*

   * 连续建目录

   * string $dir 目录字符串

   * int $mode   权限数字

   * 返回:顺利创建或者全部已建返回 true 其它方式返回 false

   */

$mode = 0777   function makeDir $dir.{

    if ! $dir return 0;

"/",    $dir = str_replac "//". $dir ;

$mode     if ! preg_match "/^/d+$/".{

      $mode = 0777;

    }

    $mdir = "";

$dir as $val     foreach explod "/".{

      $mdir .= $val."/";

      if $val == ".." || $val == "." || trim $val == "" continue;

     

      if ! file_exist $mdir {

$mode         if !@mkdir $mdir.{

         return false;

        }

      }

    }

    return true;

  }

}

?>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值