Lite.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Lite extends Driver
  18. {
  19. protected $options = [
  20. 'prefix' => '',
  21. 'path' => '',
  22. 'expire' => 0, // 等于 10*365*24*3600(10年)
  23. ];
  24. /**
  25. * 构造函数
  26. * @access public
  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. }
  39. /**
  40. * 取得变量的存储文件名
  41. * @access protected
  42. * @param string $name 缓存变量名
  43. * @return string
  44. */
  45. protected function getCacheKey($name)
  46. {
  47. return $this->options['path'] . $this->options['prefix'] . md5($name) . '.php';
  48. }
  49. /**
  50. * 判断缓存是否存在
  51. * @access public
  52. * @param string $name 缓存变量名
  53. * @return mixed
  54. */
  55. public function has($name)
  56. {
  57. return $this->get($name) ? true : false;
  58. }
  59. /**
  60. * 读取缓存
  61. * @access public
  62. * @param string $name 缓存变量名
  63. * @param mixed $default 默认值
  64. * @return mixed
  65. */
  66. public function get($name, $default = false)
  67. {
  68. $filename = $this->getCacheKey($name);
  69. if (is_file($filename)) {
  70. // 判断是否过期
  71. $mtime = filemtime($filename);
  72. if ($mtime < time()) {
  73. // 清除已经过期的文件
  74. unlink($filename);
  75. return $default;
  76. }
  77. return include $filename;
  78. } else {
  79. return $default;
  80. }
  81. }
  82. /**
  83. * 写入缓存
  84. * @access public
  85. * @param string $name 缓存变量名
  86. * @param mixed $value 存储数据
  87. * @param integer|\DateTime $expire 有效时间(秒)
  88. * @return bool
  89. */
  90. public function set($name, $value, $expire = null)
  91. {
  92. if (is_null($expire)) {
  93. $expire = $this->options['expire'];
  94. }
  95. if ($expire instanceof \DateTime) {
  96. $expire = $expire->getTimestamp();
  97. } else {
  98. $expire = 0 === $expire ? 10 * 365 * 24 * 3600 : $expire;
  99. $expire = time() + $expire;
  100. }
  101. $filename = $this->getCacheKey($name);
  102. if ($this->tag && !is_file($filename)) {
  103. $first = true;
  104. }
  105. $ret = file_put_contents($filename, ("<?php return " . var_export($value, true) . ";"));
  106. // 通过设置修改时间实现有效期
  107. if ($ret) {
  108. isset($first) && $this->setTagItem($filename);
  109. touch($filename, $expire);
  110. }
  111. return $ret;
  112. }
  113. /**
  114. * 自增缓存(针对数值缓存)
  115. * @access public
  116. * @param string $name 缓存变量名
  117. * @param int $step 步长
  118. * @return false|int
  119. */
  120. public function inc($name, $step = 1)
  121. {
  122. if ($this->has($name)) {
  123. $value = $this->get($name) + $step;
  124. } else {
  125. $value = $step;
  126. }
  127. return $this->set($name, $value, 0) ? $value : false;
  128. }
  129. /**
  130. * 自减缓存(针对数值缓存)
  131. * @access public
  132. * @param string $name 缓存变量名
  133. * @param int $step 步长
  134. * @return false|int
  135. */
  136. public function dec($name, $step = 1)
  137. {
  138. if ($this->has($name)) {
  139. $value = $this->get($name) - $step;
  140. } else {
  141. $value = -$step;
  142. }
  143. return $this->set($name, $value, 0) ? $value : false;
  144. }
  145. /**
  146. * 删除缓存
  147. * @access public
  148. * @param string $name 缓存变量名
  149. * @return boolean
  150. */
  151. public function rm($name)
  152. {
  153. return unlink($this->getCacheKey($name));
  154. }
  155. /**
  156. * 清除缓存
  157. * @access public
  158. * @param string $tag 标签名
  159. * @return bool
  160. */
  161. public function clear($tag = null)
  162. {
  163. if ($tag) {
  164. // 指定标签清除
  165. $keys = $this->getTagItem($tag);
  166. foreach ($keys as $key) {
  167. unlink($key);
  168. }
  169. $this->rm('tag_' . md5($tag));
  170. return true;
  171. }
  172. array_map("unlink", glob($this->options['path'] . ($this->options['prefix'] ? $this->options['prefix'] . DS : '') . '*.php'));
  173. }
  174. }