php文件缓存技术实现代码

下面是一个php缓存文件实现类,根据我的经验,缓存文件是根据用户设置的时间与文件生成的日间以及当前时间进行比较,然后再判断是否需要重新生成缓存文件。*/

class pagecache {
 /**
  * @var string $file 缓存文件地址
  * @access public
  */
 public $file;
 
 /**
  * @var int $cachetime 缓存时间
  * @access public
  */
 public $cachetime = 3600;
 
 /**
  * 构造函数
  * @param string $file 缓存文件地址
  * @param int $cachetime 缓存时间
     */
 function __construct($file, $cachetime = 3600) {
  $this->file = $file;
  $this->cachetime = $cachetime;
 }
 
 /**
  * 取缓存内容
  * @param bool 是否直接输出,true直接转到缓存页,false返回缓存内容
  * @return mixed
     */
 public function get($output = true) {
  if (is_file($this->file) && (time()-filemtime($this->file))<=$this->cachetime && !$_get['nocache']) {
   if ($output) {
    header('location:' . $this->file);
    exit;
   } else {
    return file_get_contents($this->file);
   }
  } else {
   return false;
  }
 }
 
 /**
  * 设置缓存内容
  * @param $content 内容html字符串
     */
 public function set($content) {
  $fp = fopen($this->file, 'w');
  fwrite($fp, $content);
  fclose($fp);
 }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值