以文件形式缓存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 30 PHP
php_screw 1.5:php加密: 安装与使用详解
Jun 20 PHP
php inc文件使用的风险和注意事项
Nov 12 PHP
php编写的简单页面跳转功能实现代码
Nov 27 PHP
PHP之APC缓存详细介绍 apc模块安装
Jan 13 PHP
PHP中new static()与new self()的区别异同分析
Aug 22 PHP
PHP中判断文件存在使用is_file还是file_exists?
Apr 03 PHP
微信access_token的获取开发示例
Apr 16 PHP
php中session_id()函数详细介绍,会话id生成过程及session id长度
Sep 23 PHP
Yii2中使用asset压缩js,css文件的方法
Nov 24 PHP
laravel框架关于搜索功能的实现
Mar 15 PHP
PHP PDOStatement::bindParam讲解
Jan 30 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获得文件扩展名三法
2006/11/25 PHP
改写函数实现PHP二维/三维数组转字符串
2013/09/13 PHP
PHP使用strtotime计算两个给定日期之间天数的方法
2015/03/18 PHP
php通过baihui网API实现读取word文档并展示
2015/06/22 PHP
php魔术方法功能与用法实例分析
2016/10/19 PHP
关于PHP5.6+版本“No input file specified”问题的解决
2019/12/11 PHP
jQuery解决iframe高度自适应代码
2009/12/20 Javascript
document.documentElement的一些使用技巧
2013/04/18 Javascript
对 jQuery 中 data 方法的误解分析
2014/06/18 Javascript
node.js 开发指南 ? Node.js 连接 MySQL 并进行数据库操作
2014/07/29 Javascript
js实现仿MSN带关闭功能的右下角弹窗代码
2015/09/04 Javascript
基于jQuery的左滑出现删除按钮的示例
2017/08/29 jQuery
jQuery Validate插件ajax方式验证输入值的实例
2017/12/21 jQuery
vue中进行微博分享的实例讲解
2019/10/14 Javascript
解决三元运算符 报错“SyntaxError: can''t assign to conditional expression”
2020/02/12 Javascript
基于PHP pthreads实现多线程代码实例
2020/06/24 Javascript
python进阶教程之文本文件的读取和写入
2014/08/29 Python
Python argv用法详解
2016/01/08 Python
Python对数据库操作
2016/03/28 Python
python+selenium开发环境搭建图文教程
2017/08/11 Python
详解supervisor使用教程
2017/11/21 Python
python实现二叉树的遍历
2017/12/11 Python
Python实现的圆形绘制(画圆)示例
2018/01/31 Python
Windows下python3.7安装教程
2018/07/31 Python
Tesserocr库的正确安装方式
2018/10/19 Python
Python实现FLV视频拼接功能
2020/01/21 Python
css3实现椭圆轨迹旋转的示例代码
2018/10/29 HTML / CSS
canvas实现手机的手势解锁的步骤详细
2020/03/16 HTML / CSS
Ever New加拿大官网:彰显女性美
2018/10/05 全球购物
PHP如何对用户密码进行加密
2014/07/31 面试题
公司部门司机岗位职责
2014/01/03 职场文书
2016年“节能宣传周”活动总结
2016/04/05 职场文书
2019XX公司员工考核管理制度!
2019/08/07 职场文书
jQuery class属性操作addClass()与removeClass()、hasClass()、toggleClass()
2021/03/31 jQuery
MySQL空间数据存储及函数
2021/09/25 MySQL
python保存图片的四个常用方法
2022/02/28 Python