首页 > 代码库 > 文件缓存

文件缓存

Cache:

  1 <?php  2 class Cache  3 {  4     private static $_instance = null;  5       6     protected $_options = array();  7       8     protected function __construct()  9     { 10     } 11      12     /** 13      * 得到本类实例 14      * array( 15      *            ‘cache_dir‘ => ‘‘, 16      *            ‘file_name_prefix‘ => ‘‘, 17      *        ) 18      * @return Ambiguous 19      */ 20     public static function getInstance($config) 21     { 22         if (self::$_instance === null) { 23             self::$_instance           = new self(); 24             self::$_instance->_options = $config; 25         } 26         return self::$_instance; 27     } 28      29     /** 30      * 得到缓存信息 31      *  32      * @param string $id 33      * @return boolean|array 34      */ 35     public static function get($id) 36     { 37         $instance = self::$_instance; 38          39         //缓存文件不存在 40         if (!$instance->has($id)) { 41             return false; 42         } 43          44         $file = $instance->_file($id); 45          46         $data = $instance->_fileGetContents($file); 47          48         if ($data[‘expire‘] == 0 || time() < $data[‘expire‘]) { 49             return $data[‘contents‘]; 50         } else { 51             @unlink($file); //过期删除缓存 52         } 53         return false; 54     } 55      56     /** 57      * 设置一个缓存 58      *  59      * @param string $id   缓存id 60      * @param array  $data 缓存内容 61      * @param int    $cacheLife 缓存生命 默认为0无限生命 62      */ 63     public static function set($id, $data, $cacheLife = 0) 64     { 65         $instance = self::$_instance; 66          67         $time              = time(); 68         $cache             = array(); 69         $cache[‘contents‘] = $data; 70         $cache[‘expire‘]   = $cacheLife === 0 ? 0 : $time + $cacheLife; 71         $cache[‘mtime‘]    = $time; 72          73         $file = $instance->_file($id); 74          75         return $instance->_filePutContents($file, $cache); 76     } 77      78     /** 79      * 清除一条缓存 80      *  81      * @param string cache id     82      * @return void 83      */ 84     public static function delete($id) 85     { 86         $instance = self::$_instance; 87          88         if (!$instance->has($id)) { 89             return false; 90         } 91         $file = $instance->_file($id); 92         //删除该缓存 93         return unlink($file); 94     } 95      96     /** 97      * 判断缓存是否存在 98      *  99      * @param string $id cache_id100      * @return boolean true 缓存存在 false 缓存不存在101      */102     public static function has($id)103     {104         $instance = self::$_instance;105         $file     = $instance->_file($id);106         107         if (!is_file($file)) {108             return false;109         }110         return true;111     }112     113     /**114      * 通过缓存id得到缓存信息路径115      * @param string $id116      * @return string 缓存文件路径117      */118     protected function _file($id)119     {120         $instance = self::$_instance;121         $fileNmae = $instance->_idToFileName($id);122         return $instance->_options[‘cache_dir‘] . $fileNmae;123     }124     125     /**126      * 通过id得到缓存信息存储文件名127      * 128      * @param  $id129      * @return string 缓存文件名130      */131     protected function _idToFileName($id)132     {133         $instance = self::$_instance;134         $prefix   = $instance->_options[‘file_name_prefix‘];135         return sha1($prefix . $id) . ‘.dat‘;136     }137     138     /**139      * 把数据写入文件140      * 141      * @param string $file 文件名称142      * @param array  $contents 数据内容143      * @return bool 144      */145     protected function _filePutContents($file, $contents)146     {147         $contents = serialize($contents);148         149         $result = false;150         $f      = @fopen($file, ‘w‘);151         if ($f) {152             @flock($f, LOCK_EX);153             fseek($f, 0);154             ftruncate($f, 0);155             $tmp = @fwrite($f, $contents);156             if (!($tmp === false)) {157                 $result = true;158             }159             @flock($f, LOCK_UN);160             @fclose($f);161         }162         @chmod($file, 0777);163         return $result;164     }165     166     /**167      * 从文件得到数据168      * 169      * @param  sring $file170      * @return boolean|array171      */172     protected function _fileGetContents($file)173     {174         if (!is_file($file)) {175             return false;176         }177         178         $f = @fopen($file, ‘r‘);179         @$data = fread($f, filesize($file));180         @fclose($f);181         182         return unserialize($data);183     }184     185     /**186      * 设置缓存路径187      * 188      * @param string $path189      * @return self190      */191     public static function setCacheDir($path)192     {193         $instance = self::$_instance;194         if (!is_dir($path)) {195             exit(‘file_cache: ‘ . $path . ‘ 不是一个有效路径 ‘);196         }197         if (!is_writable($path)) {198             exit(‘file_cache: 路径 "‘ . $path . ‘" 不可写‘);199         }200         201         $path                            = rtrim($path, ‘/‘) . ‘/‘;202         $instance->_options[‘cache_dir‘] = $path;203         204         return $instance;205     }206     207     /**208      * 设置缓存文件前缀209      * 210      * @param srting $prefix211      * @return self212      */213     public static function setCachePrefix($prefix)214     {215         $instance                               = self::$_instance;216         $instance->_options[‘file_name_prefix‘] = $prefix;217         return $instance;218     }219     220     /**221      * 删除所有缓存222      * @return boolean223      */224     public static function flush()225     {226         $instance = self::$_instance;227         $glob     = @glob($instance->_options[‘cache_dir‘] . ‘*‘);228         229         if (empty($glob)) {230             return false;231         }232         233         foreach ($glob as $v) {234             @unlink($v);235         }236         237         return true;238     }239 }

 

调用:

 1 <?php 2 require ‘FileCache.php‘; 3  4 $cachPath = dirname(__FILE__) . ‘/cache/‘; 5  6 $config = array( 7     ‘cache_dir‘ => $cachPath, 8     ‘file_name_prefix‘ => ‘cache‘, 9 );10 11 $cache = Cache::getInstance($config);12 13 $cache->set(‘sex‘, ‘women‘, 5);14 $cache->set(‘name‘, ‘jack‘);15 16 $cache->set(‘age‘, 20, 3);17 18 echo $cache->get(‘name‘);19 echo $cache->get(‘sex‘);

 

文件缓存