説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

File.php 8.2KB

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