以文件形式缓存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缓存技术介绍
Nov 25 PHP
WindowsXP中快速配置Apache+PHP5+Mysql
Jun 05 PHP
php中模拟POST传递数据的两种方法分享
Sep 16 PHP
PHP动态分页函数,PHP开发分页必备啦
Nov 07 PHP
php和javascript之间变量的传递实现代码
Dec 19 PHP
php中使用__autoload()自动加载未定义类的实现代码
Feb 06 PHP
CodeIgniter常用知识点小结
May 26 PHP
thinkPHP框架对接支付宝即时到账接口回调操作示例
Nov 14 PHP
PHP实践教程之过滤、验证、转义与密码详解
Jul 24 PHP
laravel使用Faker数据填充的实现方法
Apr 12 PHP
解决laravel上传图片之后,目录有图片,但是访问不到(404)的问题
Oct 14 PHP
php pdo连接数据库操作示例
Nov 18 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
PHP防止刷新重复提交页面的示例代码
2015/11/11 PHP
php实现微信支付之退款功能
2018/05/30 PHP
php pdo连接数据库操作示例
2019/11/18 PHP
javascript一元操作符(递增、递减)使用示例
2013/08/07 Javascript
javascript实现微信分享
2014/12/23 Javascript
JavaScript生成二维码图片小结
2015/12/27 Javascript
改变checkbox默认选中状态及取值的实现代码
2016/05/26 Javascript
基于JS实现发送短信验证码后的倒计时功能(无视页面刷新,页面关闭不进行倒计时功能)
2016/09/02 Javascript
Vue 实用分页paging实例代码
2017/04/12 Javascript
详解vue2.0脚手架的webpack 配置文件分析
2017/05/27 Javascript
浅谈vue,angular,react数据双向绑定原理分析
2017/11/28 Javascript
微信web端后退强制刷新功能的实现代码
2018/03/04 Javascript
Vue写一个简单的倒计时按钮功能
2018/04/20 Javascript
《javascript少儿编程》location术语总结
2018/05/27 Javascript
js 数组详细操作方法及解析合集
2018/06/01 Javascript
JavaScript中this关键字用法实例分析
2018/08/24 Javascript
原生js+css调节音量滑块
2020/01/15 Javascript
Flask框架学习笔记(一)安装篇(windows安装与centos安装)
2014/06/25 Python
线程安全及Python中的GIL原理分析
2019/10/29 Python
使用python快速在局域网内搭建http传输文件服务的方法
2019/11/14 Python
带你学习Python如何实现回归树模型
2020/07/16 Python
Python3 ffmpeg视频转换工具使用方法解析
2020/08/10 Python
Python+OpenCV检测灯光亮点的实现方法
2020/11/02 Python
Python 中如何使用 virtualenv 管理虚拟环境
2021/01/21 Python
HTML5 语音搜索(淘宝店语音搜素)
2013/01/03 HTML / CSS
Html5插件教程之添加浏览器放大镜效果的商品橱窗
2016/01/07 HTML / CSS
Chemist Warehouse中文网:澳洲连锁大药房
2021/02/05 全球购物
应届毕业生个人求职信范文
2014/01/29 职场文书
颁奖晚会主持词
2014/03/25 职场文书
《与朱元思书》的教学反思
2014/04/17 职场文书
2014年单位法制宣传日活动总结
2014/11/01 职场文书
党员考试作弊检讨书1000字
2015/02/16 职场文书
导游词之河北滦平金山岭长城
2019/10/16 职场文书
Oracle11g R2 安装教程完整版
2021/06/04 Oracle
Redis命令处理过程源码解析
2022/02/12 Redis
BCL经典机 SONY ICF-5900W电路分析
2022/04/24 无线电