00001 <?php
00014 class Cache_File extends Cache
00015 {
00016 var $cache_dir;
00017
00021 function Cache_File()
00022 {
00023 $this->Cache();
00024
00025 if(!defined('CACHE_FILES_DIR')) {
00026
00027 trigger_error("CACHE_FILES_DIR is not defined. Set it in app/config/cache.php");
00028 return;
00029 }
00030
00031 $this->cache_dir = CACHE_FILES_DIR;
00032 }
00033
00034 function set($key, $var, $expire=0)
00035 {
00036 $key = $this->_mangle_key($key);
00037 $fn = $this->cache_dir.DS.$key;
00038
00039 $fp = @fopen($fn, 'w');
00040 if($fp === false) {
00041 trigger_error("Unable to write to cache file: $fn");
00042 return;
00043 }
00044
00045 fputs($fp, serialize($var));
00046 fclose($fp);
00047
00048
00049
00050 $mtime = $expire > 0 ? time()+$expire : 0;
00051 touch($fn, $mtime);
00052 }
00053
00054 function &get($key)
00055 {
00056 $key = $this->_mangle_key($key);
00057 $fn = $this->cache_dir.DS.$key;
00058
00059
00060 $a = false;
00061
00062 if(!file_exists($fn)) return $a;
00063
00064 $ts = filemtime($fn);
00065 if($ts > 0 && $ts <= time()) {
00066
00067 $this->delete($key);
00068 return $a;
00069 }
00070
00071 $var = @file_get_contents($fn);
00072 if(!$var) return $a;
00073
00074 $data = unserialize($var);
00075 return $data;
00076 }
00077
00078 function delete($key)
00079 {
00080 $key = $this->_mangle_key($key);
00081 $fn = $this->cache_dir.DS.$key;
00082
00083 return @unlink($fn);
00084 }
00085
00089 function flush()
00090 {
00091 foreach(glob($this->cache_dir.DS.'*') as $fn) {
00092 if(is_file($fn)) @unlink($fn);
00093 }
00094 return true;
00095 }
00096
00100 function gc()
00101 {
00102
00103
00104 return true;
00105 }
00106
00110 function stats()
00111 {
00112 return array(
00113 'files' => glob($this->cache_dir.DS.'*')
00114 );
00115 }
00116
00120 function _mangle_key($key)
00121 {
00122 return str_replace(array('/','\\'), '_', $key);
00123 }
00124 }
00125
00126 ?>