以文件形式缓存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 相关文章推荐
第六节--访问属性和方法
Nov 16 PHP
php中计算程序运行时间的类代码
Nov 03 PHP
ThinkPHP模板引擎之导入资源文件方法详解
Jun 18 PHP
PHP获取当前所在目录位置的方法
Nov 26 PHP
php实现异步数据调用的方法
Dec 24 PHP
PHP微信开发之微信消息自动回复下所遇到的坑
May 09 PHP
php下载文件超时时间的设置方法
Oct 06 PHP
php微信公众号开发模式详解
Nov 28 PHP
浅谈thinkphp的nginx配置,以及重写隐藏index.php入口文件方法
Oct 12 PHP
PHP查找一列有序数组是否包含某值的方法
Feb 07 PHP
PHP实现Snowflake生成分布式唯一ID的方法示例
Aug 30 PHP
详解PHP用mb_string处理windows中文字符
May 26 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 discuz 主题表和回帖表的设计
2009/03/13 PHP
PHP日期时间函数的高级应用技巧
2009/05/16 PHP
thinkphp文件处理类Dir.class.php的用法分析
2014/12/08 PHP
完美解决phpdoc导出文档中@package的warning及Error的错误
2016/05/17 PHP
浅谈PHP链表数据结构(单链表)
2016/06/08 PHP
Yii CGridView用法实例详解
2016/07/12 PHP
js 获取中文拼音,Select自动匹配字母获取值的代码
2009/09/23 Javascript
Javascript中获取出错代码所在文件及行数的代码
2010/09/23 Javascript
基于jquery的textarea发布框限制文字字数输入(添加中文识别)
2012/02/16 Javascript
js清除input中type等于file的值域(示例代码)
2013/12/24 Javascript
js获取下拉列表的值和元素个数示例
2014/05/07 Javascript
浅谈$(document)和$(window)的区别
2015/07/15 Javascript
js实现当前输入框高亮显示的方法
2015/08/19 Javascript
jQuery Mobile 触摸事件实例
2016/06/04 Javascript
教你用十行node.js代码读取docx的文本
2017/03/08 Javascript
微信小程序实现流程进度的图样式功能
2018/01/16 Javascript
解决layer.msg 不居中 ifram中的问题
2019/09/05 Javascript
浅谈vue3中effect与computed的亲密关系
2019/10/10 Javascript
Python实现栈的方法
2015/05/26 Python
转换科学计数法的数值字符串为decimal类型的方法
2018/07/16 Python
Python 新建文件夹与复制文件夹内所有内容的方法
2018/10/27 Python
使用django的ORM框架按月统计近一年内的数据方法
2019/07/18 Python
Python内置方法实现字符串的秘钥加解密(推荐)
2019/12/09 Python
基于python实现简单C/S模式代码实例
2020/09/14 Python
有关HTML5页面在iPhoneX适配问题
2017/11/13 HTML / CSS
全球最大的在线旅游公司:Expedia
2017/11/16 全球购物
寻找迷宫的一条出路,o通路;X:障碍
2016/07/10 面试题
银行实习人员自我鉴定
2013/09/22 职场文书
自主实习接收函
2014/01/13 职场文书
推荐信格式要求
2014/05/09 职场文书
读书之星事迹材料
2014/05/12 职场文书
疾病防治方案
2014/05/31 职场文书
招商引资工作汇报材料
2014/10/28 职场文书
企业介绍信范文
2015/01/30 职场文书
餐厅服务员管理制度
2015/08/05 职场文书
如何更改Win11声音输出设备?Win11声音输出设备四种更改方法
2022/04/08 数码科技