File.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\cache\driver;
  12. use think\cache\Driver;
  13. /**
  14. * 文件类型缓存类
  15. * @author liu21st <liu21st@gmail.com>
  16. */
  17. class File extends Driver
  18. {
  19. protected $options = [
  20. 'expire' => 0,
  21. 'cache_subdir' => true,
  22. 'prefix' => '',
  23. 'path' => CACHE_PATH,
  24. 'data_compress' => false,
  25. ];
  26. protected $expire;
  27. /**
  28. * 构造函数
  29. * @param array $options
  30. */
  31. public function __construct($options = [])
  32. {
  33. if (!empty($options)) {
  34. $this->options = array_merge($this->options, $options);
  35. }
  36. if (substr($this->options['path'], -1) != DS) {
  37. $this->options['path'] .= DS;
  38. }
  39. $this->init();
  40. }
  41. /**
  42. * 初始化检查
  43. * @access private
  44. * @return boolean
  45. */
  46. private function init()
  47. {
  48. // 创建项目缓存目录
  49. if (!is_dir($this->options['path'])) {
  50. if (mkdir($this->options['path'], 0755, true)) {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. /**
  57. * 取得变量的存储文件名
  58. * @access protected
  59. * @param string $name 缓存变量名
  60. * @param bool $auto 是否自动创建目录
  61. * @return string
  62. */
  63. protected function getCacheKey($name, $auto = false)
  64. {
  65. $name = md5($name);
  66. if ($this->options['cache_subdir']) {
  67. // 使用子目录
  68. $name = substr($name, 0, 2) . DS . substr($name, 2);
  69. }
  70. if ($this->options['prefix']) {
  71. $name = $this->options['prefix'] . DS . $name;
  72. }
  73. $filename = $this->options['path'] . $name . '.php';
  74. $dir = dirname($filename);
  75. if ($auto && !is_dir($dir)) {
  76. mkdir($dir, 0755, true);
  77. }
  78. return $filename;
  79. }
  80. /**
  81. * 判断缓存是否存在
  82. * @access public
  83. * @param string $name 缓存变量名
  84. * @return bool
  85. */
  86. public function has($name)
  87. {
  88. return $this->get($name) ? true : false;
  89. }
  90. /**
  91. * 读取缓存
  92. * @access public
  93. * @param string $name 缓存变量名
  94. * @param mixed $default 默认值
  95. * @return mixed
  96. */
  97. public function get($name, $default = false)
  98. {
  99. $filename = $this->getCacheKey($name);
  100. if (!is_file($filename)) {
  101. return $default;
  102. }
  103. $content = file_get_contents($filename);
  104. $this->expire = null;
  105. if (false !== $content) {
  106. $expire = (int) substr($content, 8, 12);
  107. if (0 != $expire && time() > filemtime($filename) + $expire) {
  108. return $default;
  109. }
  110. $this->expire = $expire;
  111. $content = substr($content, 32);
  112. if ($this->options['data_compress'] && function_exists('gzcompress')) {
  113. //启用数据压缩
  114. $content = gzuncompress($content);
  115. }
  116. $content = preg_replace_callback('#s:(\d+):"(.*?)";#s',function($match){return 's:'.strlen($match[2]).':"'.$match[2].'";';},$content);
  117. $content = unserialize($content);
  118. return $content;
  119. } else {
  120. return $default;
  121. }
  122. }
  123. /**
  124. * 写入缓存
  125. * @access public
  126. * @param string $name 缓存变量名
  127. * @param mixed $value 存储数据
  128. * @param integer|\DateTime $expire 有效时间(秒)
  129. * @return boolean
  130. */
  131. public function set($name, $value, $expire = null)
  132. {
  133. if (is_null($expire)) {
  134. $expire = $this->options['expire'];
  135. }
  136. if ($expire instanceof \DateTime) {
  137. $expire = $expire->getTimestamp() - time();
  138. }
  139. $filename = $this->getCacheKey($name, true);
  140. if ($this->tag && !is_file($filename)) {
  141. $first = true;
  142. }
  143. $data = serialize($value);
  144. if ($this->options['data_compress'] && function_exists('gzcompress')) {
  145. //数据压缩
  146. $data = gzcompress($data, 3);
  147. }
  148. $data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
  149. $result = file_put_contents($filename, $data);
  150. if ($result) {
  151. isset($first) && $this->setTagItem($filename);
  152. clearstatcache();
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. /**
  159. * 自增缓存(针对数值缓存)
  160. * @access public
  161. * @param string $name 缓存变量名
  162. * @param int $step 步长
  163. * @return false|int
  164. */
  165. public function inc($name, $step = 1)
  166. {
  167. if ($this->has($name)) {
  168. $value = $this->get($name) + $step;
  169. $expire = $this->expire;
  170. } else {
  171. $value = $step;
  172. $expire = 0;
  173. }
  174. return $this->set($name, $value, $expire) ? $value : false;
  175. }
  176. /**
  177. * 自减缓存(针对数值缓存)
  178. * @access public
  179. * @param string $name 缓存变量名
  180. * @param int $step 步长
  181. * @return false|int
  182. */
  183. public function dec($name, $step = 1)
  184. {
  185. if ($this->has($name)) {
  186. $value = $this->get($name) - $step;
  187. $expire = $this->expire;
  188. } else {
  189. $value = -$step;
  190. $expire = 0;
  191. }
  192. return $this->set($name, $value, $expire) ? $value : false;
  193. }
  194. /**
  195. * 删除缓存
  196. * @access public
  197. * @param string $name 缓存变量名
  198. * @return boolean
  199. */
  200. public function rm($name)
  201. {
  202. $filename = $this->getCacheKey($name);
  203. try {
  204. return $this->unlink($filename);
  205. } catch (\Exception $e) {
  206. }
  207. }
  208. /**
  209. * 清除缓存
  210. * @access public
  211. * @param string $tag 标签名
  212. * @return boolean
  213. */
  214. public function clear($tag = null)
  215. {
  216. if ($tag) {
  217. // 指定标签清除
  218. $keys = $this->getTagItem($tag);
  219. foreach ($keys as $key) {
  220. $this->unlink($key);
  221. }
  222. $this->rm('tag_' . md5($tag));
  223. return true;
  224. }
  225. $files = (array) glob($this->options['path'] . ($this->options['prefix'] ? $this->options['prefix'] . DS : '') . '*');
  226. foreach ($files as $path) {
  227. if (is_dir($path)) {
  228. $matches = glob($path . '/*.php');
  229. if (is_array($matches)) {
  230. array_map('unlink', $matches);
  231. }
  232. rmdir($path);
  233. } else {
  234. unlink($path);
  235. }
  236. }
  237. return true;
  238. }
  239. /**
  240. * 判断文件是否存在后,删除
  241. * @param $path
  242. * @return bool
  243. * @author byron sampson <xiaobo.sun@qq.com>
  244. * @return boolean
  245. */
  246. private function unlink($path)
  247. {
  248. return is_file($path) && unlink($path);
  249. }
  250. }