以文件形式缓存php变量的方法


Posted in PHP onJune 26, 2015

本文实例讲述了以文件形式缓存php变量的方法。分享给大家供大家参考。具体实现方法如下:

<?php
/*
$cache_set = array(
//缓存路径 , 最后要加"/"
'cacheRoot'=>'./cache/',
//缓存时间
'cacheTime'=>20,
//cache type
'cacheType'=>1,
//扩展名
'cacheExe'=>'.php'
);
$cache = new Cache($cache_set);
$a=array('1','2');
$a="aaa";
$b='';
if($cache->cache_is("d")){
 $c=$cache->cache_read("d");
 echo "c";
 print_r($c);
}else {
$b=$cache->cache_data('d',$a);
}
print_r($b);
//$cache->clear("a");
//echo $cache->cache_read("./cache/d.php");
//echo $d;
*/
/**
 * 数据缓存类 v1.0
 * @author shooke
 * 2009-11-13 16:02:26
 * 用于缓存数据,如变量,但不能缓存页面
 */
class Cache{
 //配置
 public $config = array(
 //缓存路径
 'cacheRoot'=>'./cache/',
 //缓存时间
 'cacheTime'=>1,
 //cache 类型 1串化数据 2变量
 'cacheType'=>2,
 //扩展名
 'cacheExe'=>'.php'
 //转换中间变量
 );
 public $return_name=array();
 function __construct($cache_set = array())
 {
  if(!empty($cache_set)) $this->config=array_merge($this->config,$cache_set);
  $this->config['ClassName'] = __CLASS__;
 }
 public function clear($filename=''){
  if (file_exists($this->cache_file($filename))) {
   @unlink($this->cache_file($filename));
  }elseif (empty($filename)){
   $this->clear_dir($this->config['cacheRoot']);
  }else{
   $this->clear_dir($this->config['cacheRoot'].$filename);
   echo $this->config['cacheRoot'].$filename;
  }
 }
 //循环删除路径
 private function clear_dir($dir,$to = false)
 {
  if ($list = glob($dir.'/*'))
  {
   foreach ($list as $file)
   {
    is_dir($file) ? $this->clear_dir($file) : unlink($file);
   }
  }
  if ($to === false) rmdir($dir);
 }
 //写入缓存
 private function cache_write($filename, $writetext, $openmod='w'){
  if (!file_exists($filename)) {
   @$this->makeDir( dirname($filename ));
  }
  if(@$fp = fopen($filename, $openmod)) {
   flock($fp, 2);
   fwrite($fp, $writetext);
   fclose($fp);
   return true;
  } else {
   echo "File: $filename write error.";
   return false;
  }
 }
 //缓存有效期 有效返回 true
 public function cache_is($fileName){
  $fileName=$this->cache_file($fileName);
  if( file_exists( $fileName ) ) {
   //如果缓存时间为负数则永不过期
   if ($this->config['cacheTime'] < 0) {
    return true;
   }
   //如果缓存时间为0则一直过期
   if ($this->config['cacheTime'] == 0) {
    return false;
   }
   //获取缓存文件的建立时间
   $ctime = intval(filemtime( $fileName ));
   //比较是否大于缓存时间,是则过期 否则不过期
   if (time() - $ctime > $this->config['cacheTime']) {
    return false;
   }else {
    return true;
   }
   //文件不存在视为过期失效
  }else {
   return false;
  }
 }
 public function cache_data($name,$data){
  $varname=$name;
  $name = $this->cache_file($name);
  //config['cacheTime']==0也就是不启用缓存是直接返回数据
  if ($this->config['cacheTime'] <> 0) {
   if($this->config['cacheType']==1){
    $write_data = "<?php exit;?>".serialize($data);
    //return $data;
   }else {
    $write_data = "<?php\\r\\n\\$var= ";
    $write_data .= var_export($data,true);
    $write_data .=";\\r\\n?>";
   }
   $this->cache_write($name,$write_data);
  }
  return $data;
 }
 //缓存文件名
 private function cache_file($filename){
  return $this->config['cacheRoot'].$filename.$this->config['cacheExe'];
 }
 //读取文件
 public function cache_read($file){
  $file=$this->cache_file($file);
  if (!file_exists($file)) {
   return '';
  }
  if($this->config['cacheType']==1){
   if (function_exists('file_get_contents')){
    $cache_Content= file_get_contents($file);
   }else{
    $fopen = fopen($file,'r');
    $cache_Content = '';
    do {
     $data = fread($fopen,filesize($file));
     if (strlen($data)===0) break;
     $cache_Content .= $data;
    }while(1);
    fclose($fopen);
   }
   $cache_Content = substr($cache_Content,13);/* 去除<?php exit;?> */
   $cache_Content = unserialize($cache_Content);
   return $cache_Content;
  }else{
   include_once($file);
   return $var;
  }
 }
 //循环创建目录
 private function makeDir( $dir, $mode = 0777 ) {
  if( ! $dir ) return 0;
  $dir = str_replace( "\\\\", "/", $dir );
  $mdir = "";
  foreach( explode( "/", $dir ) as $val ) {
   $mdir .= $val."/";
   if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
   if( ! file_exists( $mdir ) ) {
    if(!@mkdir( $mdir, $mode )){
     return false;
    }
   }
  }
  return true;
 }
}
?>

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
php获取地址栏信息的代码
Oct 08 PHP
一个PHP的QRcode类与大家分享
Nov 13 PHP
php函数array_merge用法一例(合并同类数组)
Feb 03 PHP
浅析php中jsonp的跨域实例
Jun 21 PHP
php+mysqli实现批量替换数据库表前缀的方法
Dec 29 PHP
javascript数组与php数组的地址传递及值传递用法实例
Jan 22 PHP
PHP中开启gzip压缩的2种方法
Jan 31 PHP
浅谈PHP接收POST数据方式
Jun 05 PHP
PHP递归遍历指定文件夹内的文件实现方法
Nov 15 PHP
PHP面向对象学习之parent::关键字
Jan 18 PHP
php中的buffer缓冲区用法分析
May 31 PHP
Laravel 5.5 实现禁用用户注册示例
Oct 24 PHP
PHP批量去除BOM头代码分享
Jun 26 #PHP
PHP多态代码实例
Jun 26 #PHP
PHP微信开发之二维码生成类
Jun 26 #PHP
Thinkphp关闭缓存的方法
Jun 26 #PHP
php获取、检查类名、函数名、方法名的函数方法
Jun 25 #PHP
php header函数的常用http头设置
Jun 25 #PHP
PHP里的单例类写法实例
Jun 25 #PHP
You might like
PHP5.4起内置web服务器使用方法
2016/08/09 PHP
php连接mysql数据库最简单的实现方法
2019/09/24 PHP
prototype 的说明 js类
2006/09/07 Javascript
sina的lightbox效果。
2007/01/09 Javascript
基于逻辑运算的简单权限系统(实现) JS 版
2007/03/24 Javascript
Javascript 布尔型分析
2008/12/22 Javascript
JavaScript判断窗口是否最小化的代码(跨浏览器)
2010/08/01 Javascript
JavaScript之appendChild、insertBefore和insertAfter使用说明
2010/12/30 Javascript
javascript实现的一个带下拉框功能的文本框
2014/05/08 Javascript
整理关于Bootstrap过渡动画的慕课笔记
2017/03/29 Javascript
基于Vue实现图书管理功能
2017/10/17 Javascript
微信小程序实现卡片左右滑动效果的示例代码
2019/05/01 Javascript
Windows上node.js的多版本管理工具用法实例分析
2019/11/06 Javascript
js中复选框的取值及赋值示例详解
2020/10/18 Javascript
利用node.js开发cli的完整步骤
2020/12/29 Javascript
[01:52]深扒TI7聊天轮盘语音出处7
2017/05/11 DOTA
python获取beautifulphoto随机某图片代码实例
2013/12/18 Python
Python常用的爬虫技巧总结
2016/03/28 Python
django启动uwsgi报错的解决方法
2018/04/08 Python
python 实现将字典dict、列表list中的中文正常显示方法
2018/07/06 Python
python3 unicode列表转换为中文的实例
2018/10/26 Python
对python借助百度云API对评论进行观点抽取的方法详解
2019/02/21 Python
Python列表删除元素del、pop()和remove()的区别小结
2019/09/11 Python
python编写俄罗斯方块
2020/03/13 Python
python线程优先级队列知识点总结
2021/02/28 Python
Python里面search()和match()的区别
2016/09/21 面试题
建筑工地宣传标语
2014/06/18 职场文书
教师反腐倡廉演讲稿
2014/09/03 职场文书
大学新生军训自我鉴定范文
2014/09/13 职场文书
"9.18"国耻日演讲稿范文
2014/09/14 职场文书
房屋买卖协议书范本
2014/09/27 职场文书
大雁塔英文导游词
2015/02/10 职场文书
廉政承诺书范文
2015/04/28 职场文书
在人间读书笔记
2015/06/30 职场文书
Win11安装升级时提示“该电脑必须支持安全启动”
2022/04/19 数码科技
mysql如何查询连续记录
2022/05/11 MySQL