>  기사  >  백엔드 개발  >  PHP缓存之文件缓存

PHP缓存之文件缓存

WBOY
WBOY원래의
2016-06-23 13:50:42669검색

1、PHP文件缓存内容保存格式
       PHP文件缓存内容保存格式主要有三种:
       (1)变量 var_export 格式化成PHP正常的赋值书写格式;
       (2)变量 serialize 序列化之后保存,用的时候反序列化;
       (3)变量 json_encode格式化之后保存,用的时候json_decode
       互联网上测试结果是:serialize格式的文件解析效率大于Json,Json的解析效率大于PHP正常赋值。
       所以我们要是缓存数据建议采用序列化的形式解析数据会更快。

2、PHP文件缓存的简单案例

[php] view plain copy print ?

  1. class Cache_Driver{ 
  2.     //定义缓存的路径 
  3.     protected $_cache_path; 
  4.  
  5. //根据$config中的cache_path值获取路径信息 
  6.     public function Cache_Driver($config) 
  7.     { 
  8.         if(is_array($config) && isset($config['cache_path'])) 
  9.         { 
  10.            $this->_cache_path = $config['cache_path']; 
  11.         } 
  12.         else 
  13.         { 
  14.            $this->_cache_path = realpath(dirname(__FILE__)."/")."/cache/"; 
  15.         } 
  16.     } 
  17. //判断key值对应的文件是否存在,如果存在,读取value值,value以序列化存储 
  18.     public function get($id) 
  19.     { 
  20.         if ( ! file_exists($this->_cache_path.$id)) 
  21.         { 
  22.             return FALSE; 
  23.         } 
  24.          
  25.         $data = @file_get_contents($this->_cache_path.$id); 
  26.         $data = unserialize($data); 
  27.          
  28.         if(!is_array($data) || !isset($data['time']) || !isset($data['ttl'])) 
  29.         { 
  30.             return FALSE; 
  31.         } 
  32.          
  33.         if ($data['ttl'] > 0 && time() >  $data['time'] + $data['ttl']) 
  34.         { 
  35.             @unlink($this->_cache_path.$id); 
  36.             return FALSE; 
  37.         } 
  38.          
  39.         return $data['data']; 
  40.     } 
  41. //设置缓存信息,根据key值,生成相应的缓存文件 
  42.     public function set($id, $data, $ttl = 60) 
  43.     {        
  44.         $contents = array( 
  45.                 'time'      => time(), 
  46.                 'ttl'       => $ttl,          
  47.                 'data'      => $data 
  48.             ); 
  49.          
  50.         if (@file_put_contents($this->_cache_path.$id, serialize($contents))) 
  51.         { 
  52.             @chmod($this->_cache_path.$id, 0777); 
  53.             return TRUE;             
  54.         } 
  55.  
  56.         return FALSE; 
  57.     } 
  58. //根据key值,删除缓存文件 
  59.     public function delete($id) 
  60.     { 
  61.         return @unlink($this->_cache_path.$id); 
  62.     } 
  63.  
  64.     public function clean() 
  65.     { 
  66.       $dh = @opendir($this->_cache_path); 
  67.        if(!$dh) 
  68.          return FALSE; 
  69.        
  70.       while ($file = @readdir($dh)) 
  71.       { 
  72.          if($file == "." || $file == "..") 
  73.             continue; 
  74.           
  75.          $path = $this->_cache_path."/".$file; 
  76.          if(is_file($path)) 
  77.             @unlink($path); 
  78.       } 
  79.       @closedir($dh); 
  80.        
  81.         return TRUE; 
  82.     } 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.