1
0

filecache_class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @copyright (c) 2011 aircheng.com
  4. * @file IFile_cache.php
  5. * @brief 文件级缓存类
  6. * @author chendeshan
  7. * @date 2011-7-8 16:02:31
  8. * @version 0.6
  9. */
  10. /**
  11. * @brief 文件级缓存类
  12. * @class IFileCache
  13. */
  14. class IFileCache implements ICacheInte
  15. {
  16. private $cachePath = 'runtime/cache'; //默认文件缓存存放路径
  17. private $cacheExt = '.data'; //默认文件缓存扩展名
  18. private $directoryLevel = 1; //目录层级,基于$cachePath之下的
  19. /**
  20. * @brief 构造函数
  21. */
  22. public function __construct()
  23. {
  24. $this->cachePath = isset(IWeb::$app->config['cache']['path']) ? IWeb::$app->config['cache']['path'] : $this->cachePath;
  25. $this->cacheExt = isset(IWeb::$app->config['cache']['ext']) ? IWeb::$app->config['cache']['ext'] : $this->cacheExt;
  26. }
  27. /**
  28. * @brief 根据key值计算缓存文件名
  29. * @param string $key 缓存的唯一key值
  30. * @return string 缓存文件路径
  31. */
  32. private function getFileName($key)
  33. {
  34. $key = str_replace(' ','',$key);
  35. $cacheDir = rtrim($this->cachePath,'\\/').'/';
  36. if($this->directoryLevel > 0)
  37. {
  38. $hash = abs(crc32($key));
  39. $cacheDir .= $hash % 1024;
  40. for($i = 1;$i < $this->directoryLevel;++$i)
  41. {
  42. if(($prefix = substr($hash,$i,2)) !== false)
  43. {
  44. $cacheDir .= '/'.$prefix;
  45. }
  46. }
  47. }
  48. return $cacheDir.'/'.md5($key).$this->cacheExt;
  49. }
  50. /**
  51. * @brief 写入缓存
  52. * @param string $key 缓存的唯一key值
  53. * @param mixed $data 要写入的缓存数据
  54. * @param int $expire 缓存数据失效时间,单位:秒
  55. * @return bool true:成功;false:失败;
  56. */
  57. public function set($key,$data,$expire = '')
  58. {
  59. $fileName = $this->getFileName($key);
  60. if(!file_exists($dirname=dirname($fileName)))
  61. {
  62. IFile::mkdir($dirname);
  63. }
  64. $writeLen = file_put_contents($fileName,$data);
  65. if($writeLen == 0)
  66. {
  67. return false;
  68. }
  69. else
  70. {
  71. $expire = time() + $expire;
  72. touch($fileName,$expire);
  73. return true;
  74. }
  75. }
  76. /**
  77. * @brief 读取缓存
  78. * @param string $key 缓存的唯一key值,当要返回多个值时可以写成数组
  79. * @return mixed 读取出的缓存数据;null:没有取到数据或者缓存已经过期了;
  80. */
  81. public function get($key)
  82. {
  83. $fileName = $this->getFileName($key);
  84. if(file_exists($fileName))
  85. {
  86. if(time() > filemtime($fileName))
  87. {
  88. $this->del($key,0);
  89. return null;
  90. }
  91. else
  92. {
  93. return file_get_contents($fileName);
  94. }
  95. }
  96. else
  97. {
  98. return null;
  99. }
  100. }
  101. /**
  102. * @brief 删除缓存
  103. * @param string $key 缓存的唯一key值
  104. * @param int $timeout 在间隔单位时间内自动删除,单位:秒
  105. * @return bool true:成功; false:失败;
  106. */
  107. public function del($key,$timeout = '')
  108. {
  109. $fileName = $this->getFileName($key);
  110. if(file_exists($fileName))
  111. {
  112. if($timeout > 0)
  113. {
  114. $timeout = time() + $timeout;
  115. return touch($fileName,$timeout);
  116. }
  117. else
  118. {
  119. return unlink($fileName);
  120. }
  121. }
  122. else
  123. {
  124. return true;
  125. }
  126. }
  127. /**
  128. * @brief 删除全部缓存
  129. * @return bool true:成功;false:失败;
  130. */
  131. public function flush()
  132. {
  133. return IFile::clearDir($this->cachePath);
  134. }
  135. }