File.php 6.9 KB

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