File.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 = unserialize($content);
  117. return $content;
  118. } else {
  119. return $default;
  120. }
  121. }
  122. /**
  123. * 写入缓存
  124. * @access public
  125. * @param string $name 缓存变量名
  126. * @param mixed $value 存储数据
  127. * @param integer|\DateTime $expire 有效时间(秒)
  128. * @return boolean
  129. */
  130. public function set($name, $value, $expire = null)
  131. {
  132. if (is_null($expire)) {
  133. $expire = $this->options['expire'];
  134. }
  135. if ($expire instanceof \DateTime) {
  136. $expire = $expire->getTimestamp() - time();
  137. }
  138. $filename = $this->getCacheKey($name, true);
  139. if ($this->tag && !is_file($filename)) {
  140. $first = true;
  141. }
  142. $data = serialize($value);
  143. if ($this->options['data_compress'] && function_exists('gzcompress')) {
  144. //数据压缩
  145. $data = gzcompress($data, 3);
  146. }
  147. $data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
  148. $result = file_put_contents($filename, $data);
  149. if ($result) {
  150. isset($first) && $this->setTagItem($filename);
  151. clearstatcache();
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. }
  157. /**
  158. * 自增缓存(针对数值缓存)
  159. * @access public
  160. * @param string $name 缓存变量名
  161. * @param int $step 步长
  162. * @return false|int
  163. */
  164. public function inc($name, $step = 1)
  165. {
  166. if ($this->has($name)) {
  167. $value = $this->get($name) + $step;
  168. $expire = $this->expire;
  169. } else {
  170. $value = $step;
  171. $expire = 0;
  172. }
  173. return $this->set($name, $value, $expire) ? $value : false;
  174. }
  175. /**
  176. * 自减缓存(针对数值缓存)
  177. * @access public
  178. * @param string $name 缓存变量名
  179. * @param int $step 步长
  180. * @return false|int
  181. */
  182. public function dec($name, $step = 1)
  183. {
  184. if ($this->has($name)) {
  185. $value = $this->get($name) - $step;
  186. $expire = $this->expire;
  187. } else {
  188. $value = -$step;
  189. $expire = 0;
  190. }
  191. return $this->set($name, $value, $expire) ? $value : false;
  192. }
  193. /**
  194. * 删除缓存
  195. * @access public
  196. * @param string $name 缓存变量名
  197. * @return boolean
  198. */
  199. public function rm($name)
  200. {
  201. $filename = $this->getCacheKey($name);
  202. try {
  203. return $this->unlink($filename);
  204. } catch (\Exception $e) {
  205. }
  206. }
  207. /**
  208. * 清除缓存
  209. * @access public
  210. * @param string $tag 标签名
  211. * @return boolean
  212. */
  213. public function clear($tag = null)
  214. {
  215. if ($tag) {
  216. // 指定标签清除
  217. $keys = $this->getTagItem($tag);
  218. foreach ($keys as $key) {
  219. $this->unlink($key);
  220. }
  221. $this->rm('tag_' . md5($tag));
  222. return true;
  223. }
  224. $files = (array) glob($this->options['path'] . ($this->options['prefix'] ? $this->options['prefix'] . DS : '') . '*');
  225. foreach ($files as $path) {
  226. if (is_dir($path)) {
  227. $matches = glob($path . '/*.php');
  228. if (is_array($matches)) {
  229. array_map('unlink', $matches);
  230. }
  231. rmdir($path);
  232. } else {
  233. unlink($path);
  234. }
  235. }
  236. return true;
  237. }
  238. /**
  239. * 判断文件是否存在后,删除
  240. * @param $path
  241. * @return bool
  242. * @author byron sampson <xiaobo.sun@qq.com>
  243. * @return boolean
  244. */
  245. private function unlink($path)
  246. {
  247. return is_file($path) && unlink($path);
  248. }
  249. }