00001 <?php 00023 class FileCache implements CacheInterface 00024 { 00032 protected static function _get_filename($id, $group) 00033 { 00034 return 'cache_'.md5($id).'.'.md5($group); 00035 } 00043 public static function get($id, $group) 00044 { 00045 $filename = APP_PATH . 'temp/cache/'.self::_get_filename($id, $group); 00046 if(file_exists($filename)){ 00047 $fh = fopen($filename, 'r'); 00048 00049 $lifetime = trim(fgets($fh)); 00050 if($lifetime == 'undefined' || $lifetime >= time()) 00051 $data = stream_get_contents($fh); 00052 else 00053 $data = null; 00054 00055 fclose($fh); 00056 return $data; 00057 } 00058 return null; 00059 } 00069 public static function save($id, $group, $value, $lifetime) 00070 { 00071 if($lifetime == null) 00072 $lifetime = 'undefined'; 00073 return file_put_contents(APP_PATH . 'temp/cache/'.self::_get_filename($id, $group), "$lifetime\n$value"); 00074 } 00081 public static function clean($group=false) 00082 { 00083 $pattern = $group ? APP_PATH . 'temp/cache/'.'*.'.md5($group) : APP_PATH . 'temp/cache/'.'*'; 00084 foreach (glob($pattern) as $filename) { 00085 if(!unlink($filename)) { 00086 return false; 00087 } 00088 } 00089 } 00097 public static function remove($id, $group) 00098 { 00099 return unlink(APP_PATH . 'temp/cache/'.self::_get_filename($id, $group)); 00100 } 00101 }