以文件形式缓存php变量的方法

本文实例讲述了以文件形式缓存php变量的方法。分享给大家供大家参考。具体实现方法如下:

[php] view plaincopy

  1. <?php  

  2. /* 

  3. $cache_set = array( 

  4. //缓存路径 , 最后要加"/" 

  5. 'cacheRoot'=>'./cache/', 

  6. //缓存时间 

  7. 'cacheTime'=>20, 

  8. //cache type 

  9. 'cacheType'=>1, 

  10. //扩展名 

  11. 'cacheExe'=>'.php' 

  12. ); 

  13. $cache = new Cache($cache_set); 

  14. $a=array('1','2'); 

  15. $a="aaa"; 

  16. $b=''; 

  17. if($cache->cache_is("d")){ 

  18.  $c=$cache->cache_read("d"); 

  19.  echo "c"; 

  20.  print_r($c); 

  21. }else { 

  22. $b=$cache->cache_data('d',$a); 

  23. } 

  24. print_r($b); 

  25. //$cache->clear("a"); 

  26. //echo $cache->cache_read("./cache/d.php"); 

  27. //echo $d; 

  28. */  

  29. /** 

  30.  * 数据缓存类 v1.0 

  31.  * @author shooke 

  32.  * 2009-11-13 16:02:26 

  33.  * 用于缓存数据,如变量,但不能缓存页面 

  34.  */  

  35. class Cache{  

  36.  //配置  

  37.  public $config = array(  

  38.  //缓存路径  

  39.  'cacheRoot'=>'./cache/',  

  40.  //缓存时间  

  41.  'cacheTime'=>1,  

  42.  //cache 类型 1串化数据 2变量  

  43.  'cacheType'=>2,  

  44.  //扩展名  

  45.  'cacheExe'=>'.php'  

  46.  //转换中间变量  

  47.  );  

  48.  public $return_name=array();  

  49.  function __construct($cache_set = array())  

  50.  {  

  51.   if(!empty($cache_set)) $this->config=array_merge($this->config,$cache_set);  

  52.   $this->config['ClassName'] = __CLASS__;  

  53.  }  

  54.  public function clear($filename=''){  

  55.   if (file_exists($this->cache_file($filename))) {  

  56.    @unlink($this->cache_file($filename));  

  57.   }elseif (empty($filename)){  

  58.    $this->clear_dir($this->config['cacheRoot']);  

  59.   }else{  

  60.    $this->clear_dir($this->config['cacheRoot'].$filename);  

  61.    echo $this->config['cacheRoot'].$filename;  

  62.   }  

  63.  }  

  64.  //循环删除路径  

  65.  private function clear_dir($dir,$to = false)  

  66.  {  

  67.   if ($list = glob($dir.'/*'))  

  68.   {  

  69.    foreach ($list as $file)  

  70.    {  

  71.     is_dir($file) ? $this->clear_dir($file) : unlink($file);  

  72.    }  

  73.   }  

  74.   if ($to === false) rmdir($dir);  

  75.  }  

  76.  //写入缓存  

  77.  private function cache_write($filename$writetext$openmod='w'){  

  78.   if (!file_exists($filename)) {  

  79.    @$this->makeDir( dirname($filename ));  

  80.   }  

  81.   if(@$fp = fopen($filename$openmod)) {  

  82.    flock($fp, 2);  

  83.    fwrite($fp$writetext);  

  84.    fclose($fp);  

  85.    return true;  

  86.   } else {  

  87.    echo "File: $filename write error.";  

  88.    return false;  

  89.   }  

  90.  }  

  91.  //缓存有效期 有效返回 true  

  92.  public function cache_is($fileName){  

  93.   $fileName=$this->cache_file($fileName);  

  94.   iffile_exists$fileName ) ) {  

  95.    //如果缓存时间为负数则永不过期  

  96.    if ($this->config['cacheTime'] < 0) {  

  97.     return true;  

  98.    }  

  99.    //如果缓存时间为0则一直过期  

  100.    if ($this->config['cacheTime'] == 0) {  

  101.     return false;  

  102.    }  

  103.    //获取缓存文件的建立时间  

  104.    $ctime = intval(filemtime$fileName ));  

  105.    //比较是否大于缓存时间,是则过期 否则不过期  

  106.    if (time() - $ctime > $this->config['cacheTime']) {  

  107.     return false;  

  108.    }else {  

  109.     return true;  

  110.    }  

  111.    //文件不存在视为过期失效  

  112.   }else {  

  113.    return false;  

  114.   }  

  115.  }  

  116.  public function cache_data($name,$data){  

  117.   $varname=$name;  

  118.   $name = $this->cache_file($name);  

  119.   //config['cacheTime']==0也就是不启用缓存是直接返回数据  

  120.   if ($this->config['cacheTime'] <> 0) {  

  121.    if($this->config['cacheType']==1){  

  122.     $write_data = "<?php exit;?>".serialize($data);  

  123.     //return $data;  

  124.    }else {  

  125.     $write_data = "<?php\\r\\n\\$var= ";  

  126.     $write_data .= var_export($data,true);  

  127.     $write_data .=";\\r\\n?>";  

  128.    }  

  129.    $this->cache_write($name,$write_data);  

  130.   }  

  131.   return $data;  

  132.  }  

  133.  //缓存文件名  

  134.  private function cache_file($filename){  

  135.   return $this->config['cacheRoot'].$filename.$this->config['cacheExe'];  

  136.  }  

  137.  //读取文件  

  138.  public function cache_read($file){  

  139.   $file=$this->cache_file($file);  

  140.   if (!file_exists($file)) {  

  141.    return '';  

  142.   }  

  143.   if($this->config['cacheType']==1){  

  144.    if (function_exists('file_get_contents')){  

  145.     $cache_Contentfile_get_contents($file);  

  146.    }else{  

  147.     $fopen = fopen($file,'r');  

  148.     $cache_Content = '';  

  149.     do {  

  150.      $data = fread($fopen,filesize($file));  

  151.      if (strlen($data)===0) break;  

  152.      $cache_Content .= $data;  

  153.     }while(1);  

  154.     fclose($fopen);  

  155.    }  

  156.    $cache_Content = substr($cache_Content,13);/* 去除<?php exit;?> */  

  157.    $cache_Content = unserialize($cache_Content);  

  158.    return $cache_Content;  

  159.   }else{  

  160.    include_once($file);  

  161.    return $var;  

  162.   }  

  163.  }  

  164.  //循环创建目录  

  165.  private function makeDir( $dir$mode = 0777 ) {  

  166.   if( ! $dir ) return 0;  

  167.   $dir = str_replace"\\\\", "/", $dir );  

  168.   $mdir = "";  

  169.   foreachexplode"/"$dir ) as $val ) {  

  170.    $mdir .= $val."/";  

  171.    if$val == ".." || $val == "." || trim( $val ) == "" ) continue;  

  172.    if( ! file_exists$mdir ) ) {  

  173.     if(!@mkdir$mdir$mode )){  

  174.      return false;  

  175.     }  

  176.    }  

  177.   }  

  178.   return true;  

  179.  }  

  180. }  

  181. ?>  


转载于:https://my.oschina.net/yonghan/blog/471849

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值