以文件形式缓存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脚本的10个技巧(8)
Oct 09 PHP
php heredoc和phpwind的模板技术使用方法小结
Mar 28 PHP
php url地址栏传中文乱码解决方法集合
Jun 25 PHP
php中session过期时间设置及session回收机制介绍
May 05 PHP
php导入excel文件到mysql数据库的方法
Jan 14 PHP
关于WordPress的SEO优化相关的一些PHP页面脚本技巧
Dec 10 PHP
php制作的简单验证码识别代码
Jan 26 PHP
PHP类和对象相关系统函数与运算符小结
Sep 28 PHP
深入了解PHP中的Array数组和foreach
Nov 06 PHP
php中static 静态变量和普通变量的区别
Dec 01 PHP
PHP 进度条函数的简单实例
Sep 19 PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
Nov 14 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
使用sockets:从新闻组中获取文章(二)
2006/10/09 PHP
在项目中寻找代码的坏命名
2012/07/14 PHP
PHP实现的简单排列组合算法应用示例
2017/06/20 PHP
javascript eval和JSON之间的联系
2009/12/31 Javascript
关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别
2010/10/18 Javascript
关于图片的预加载过程中隐藏未知的
2012/12/19 Javascript
JS幻灯片可循环播放可平滑旋转带滚动导航(自写)
2013/08/05 Javascript
JS操作iframe里的dom(实例讲解)
2014/01/29 Javascript
input:checkbox多选框实现单选效果跟radio一样
2014/06/16 Javascript
Ext JS框架程序中阻止键盘触发回退或者刷新页面的代码分享
2016/06/07 Javascript
Bootstrap复选框和单选按钮美化插件(推荐)
2016/11/23 Javascript
setTimeout函数的神奇使用
2017/02/26 Javascript
使用ionic播放轮询广告的实现方法(必看)
2017/04/24 Javascript
JavaScript限制在客户区可见范围的拖拽(解决scrollLeft和scrollTop的问题)(2)
2017/05/17 Javascript
jquery.validate表单验证插件使用详解
2017/06/21 jQuery
jQuery发请求传输中文参数乱码问题的解决方案
2018/05/22 jQuery
微信小程序实现滴滴导航tab切换效果
2018/07/24 Javascript
vue-cli3+typescript初体验小结
2019/02/28 Javascript
浅析JavaScript 函数防抖和节流
2020/07/13 Javascript
[00:08]DOTA2勇士令状等级奖励“天外飞星”
2019/05/24 DOTA
进一步了解Python中的XML 工具
2015/04/13 Python
python读写LMDB文件的方法
2018/07/02 Python
Python读取Excel表格,并同时画折线图和柱状图的方法
2018/10/14 Python
python-opencv 将连续图片写成视频格式的方法
2019/01/08 Python
python快速编写单行注释多行注释的方法
2019/07/31 Python
python deque模块简单使用代码实例
2020/03/12 Python
CSS3实现的文本3D效果附图
2014/09/03 HTML / CSS
html5唤醒APP小记
2019/03/27 HTML / CSS
英国羊绒服装购物网站:Pure Collection
2018/10/22 全球购物
FC-Moto美国:欧洲最大的摩托车服装和头盔商店之一
2019/08/24 全球购物
高三语文教学反思
2014/01/15 职场文书
报关专员求职信范文
2014/02/22 职场文书
求职面试个人自我评价
2014/02/28 职场文书
企业转让协议书(范文2篇)
2019/08/15 职场文书
导游词之徐州-云龙山
2019/09/29 职场文书
PyQt5实现多张图片显示并滚动
2021/06/11 Python