以文件形式缓存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的FTP学习(一)[转自奥索]
Oct 09 PHP
php中static静态变量的使用方法详解
Jun 04 PHP
PHP __autoload函数(自动载入类文件)的使用方法
Feb 04 PHP
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
Jun 20 PHP
PHP中ini_set与ini_get用法实例
Nov 04 PHP
php实现等比例不失真缩放上传图片的方法
Nov 14 PHP
IIS 7.5 asp Session超时时间设置方法
Apr 17 PHP
PHP-CGI远程代码执行漏洞分析与防范
May 07 PHP
php简单构造json多维数组的方法示例
Jun 08 PHP
php+js实现裁剪任意形状图片
Oct 31 PHP
laravel 实现登陆后返回登陆前的页面方法
Oct 03 PHP
phpcmsv9.0任意文件上传漏洞解析
Oct 20 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中session过期时间设置及session回收机制介绍
2014/05/05 PHP
浅谈PHP中的Trait使用方法
2019/03/22 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
2020/02/27 PHP
用javascript父窗口控制只弹出一个子窗口
2007/04/10 Javascript
javascript返回顶部效果(自写代码)
2013/01/06 Javascript
使用JavaScript实现网页版Pongo设计思路及源代码分享
2014/06/16 Javascript
javascript显示中文日期的方法
2015/06/18 Javascript
Javascript中replace()小结
2015/09/30 Javascript
javascript实现表单验证
2016/01/29 Javascript
BootStrap中Datepicker控件带中文的js文件
2016/08/10 Javascript
javascript实现消灭星星小游戏简单版
2016/11/15 Javascript
jQuery如何跳转到另一个网页 就这么简单
2016/12/28 Javascript
JavaScript实现同一个页面打开多张图片
2016/12/29 Javascript
js实现鼠标拖动功能
2017/03/20 Javascript
如何编写jquery插件
2017/03/29 jQuery
基于vue2.x的电商图片放大镜插件的使用
2018/01/22 Javascript
vue组件中的样式属性scoped实例详解
2018/10/30 Javascript
在vue中使用G2图表的示例代码
2019/03/19 Javascript
vue项目中企业微信使用js-sdk时config和agentConfig配置方式详解
2020/12/15 Vue.js
Python内置函数Type()函数一个有趣的用法
2015/02/18 Python
Python通过matplotlib绘制动画简单实例
2017/12/13 Python
Python实现合并两个列表的方法分析
2018/05/28 Python
Python3标准库总结
2019/02/19 Python
django中ORM模型常用的字段的使用方法
2019/03/05 Python
python集合删除多种方法详解
2020/02/10 Python
PyQt5-QDateEdit的简单使用操作
2020/07/12 Python
YOOX台湾:意大利奢侈品电商
2018/10/13 全球购物
大学生毕业的自我鉴定
2013/11/13 职场文书
大学生开西餐厅创业计划书
2014/02/01 职场文书
酒店保安领班职务说明书
2014/03/04 职场文书
勤俭节约倡议书范文
2015/04/29 职场文书
时尚女魔头观后感
2015/06/04 职场文书
怎样写家长意见
2015/06/04 职场文书
城南旧事电影观后感
2015/06/16 职场文书
务工证明怎么写
2015/06/18 职场文书
读《儒林外史》有感:少一些功利,多一些真诚
2020/01/19 职场文书