1.  
  2. /** 
  3. * 文件缓存类 
  4. * 
  5. * @copyright Copyright (c) 2006 - 2008 MYRIS.CN 
  6. * @author 志凡 <> 
  7. * @package cache 
  8. * @version v0.1 
  9. */ 
  10. class FileCache { 
  11. /** 
  12. * @var string $cachePath 缓存文件目录 
  13. * @access public 
  14. */ 
  15. public $cachePath = './'
  16.  
  17. /** 
  18. * 构造函数 
  19. * @param string $path 缓存文件目录 
  20. */ 
  21. function __construct($path = NULL) { 
  22.    if ($path) { 
  23.     $this->cachePath = $path
  24.    } 
  25.    
  26.    
  27.     } 
  28.  
  29. /** 
  30. * 析构函数 
  31. */ 
  32. function __destruct() { 
  33.     //nothing 
  34.     } 
  35.  
  36. /** 
  37. * 在cache中设置键为$key的项的值,如果该项不存在,则新建一个项 
  38. * @param string $key 键值 
  39. * @param mix $var 值 
  40. * @param int $expire 到期秒数 
  41. * @param int $flag 标志位 
  42. * @return bool 如果成功则返回 TRUE,失败则返回 FALSE。 
  43. * @access public 
  44. */ 
  45.     public function set($key$var$expire = 36000, $flag = 0) { 
  46.    $value = serialize($var); 
  47.    $timeout = time() + $expire
  48.    $result = safe_file_put_contents($this->cachePath . urlencode($key) .'.cache'
  49.      $timeout . '<<%-==-%>>' . $value); 
  50.    return $result
  51.  
  52. /** 
  53. * 在cache中获取键为$key的项的值 
  54. * @param string $key 键值 
  55. * @return string 如果该项不存在,则返回false 
  56. * @access public 
  57. */ 
  58.     public function get($key) { 
  59.    $file = $this->cachePath . urlencode($key) .'.cache'
  60.    if (file_exists($file)) { 
  61.     $content = safe_file_get_contents($file); 
  62.     if ($content===false) { 
  63.      return false; 
  64.     } 
  65.     $tmp = explode('<<%-==-%>>'$content); 
  66.     $timeout = $tmp[0]; 
  67.     $value = $tmp[1]; 
  68.     if (time()>$timeout) { 
  69.      $result = false; 
  70.     } else { 
  71.      $result = unserialize($value); 
  72.     } 
  73.    } else { 
  74.     $result = false; 
  75.    } 
  76.    return $result
  77.  
  78. /** 
  79. * 清空cache中所有项 
  80. * @return 如果成功则返回 TRUE,失败则返回 FALSE。 
  81. * @access public 
  82. */ 
  83.     public function flush() { 
  84.    $fileList = FileSystem::ls($this->cachePath,array(),'asc',true); 
  85.    return FileSystem::rm($fileList); 
  86.  
  87. /** 
  88. * 删除在cache中键为$key的项的值 
  89. * @param string $key 键值 
  90. * @return 如果成功则返回 TRUE,失败则返回 FALSE。 
  91. * @access public 
  92. */ 
  93.     public function delete($key) { 
  94.    return FileSystem::rm($this->cachePath . $key .'.cache'); 
  95. if (!function_exists('safe_file_put_contents')) { 
  96. function safe_file_put_contents($filename$content
  97.    $fp = fopen($filename'wb'); 
  98.    if ($fp) { 
  99.     flock($fp, LOCK_EX); 
  100.     fwrite($fp$content); 
  101.     flock($fp, LOCK_UN); 
  102.     fclose($fp); 
  103.     return true; 
  104.    } else { 
  105.     return false; 
  106.    } 
  107. if (!function_exists('safe_file_get_contents')) { 
  108. function safe_file_get_contents($filename
  109.    $fp = fopen($filename'rb'); 
  110.    if ($fp) { 
  111.     flock($fp, LOCK_SH); 
  112.     clearstatcache(); 
  113.     $filesize = filesize($filename); 
  114.     if ($filesize > 0) { 
  115.      $data = fread($fp$filesize); 
  116.     } 
  117.     flock($fp, LOCK_UN); 
  118.     fclose($fp); 
  119.     return $data
  120.    } else { 
  121.     return false; 
  122.    } 
  123.  
  124. //例子 
  125. $cache = new FileCache(); 
  126. $data = $cache->get('yourkey');//yourkey是你为每一个要缓存的数据定义的缓存名字 
  127. if ($data===false) { 
  128.     $data = '从数据库取出的数据或很复杂很耗时的弄出来的数据'
  129.     $cache->set('yourkey',$data,3600);//缓存3600秒 
  130. // use your $data