File.php 8.0 KB

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