File.php 7.7 KB

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