Lite.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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) != DIRECTORY_SEPARATOR) {
  36. $this->options['path'] .= DIRECTORY_SEPARATOR;
  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. $this->readTimes++;
  69. $filename = $this->getCacheKey($name);
  70. if (is_file($filename)) {
  71. // 判断是否过期
  72. $mtime = filemtime($filename);
  73. if ($mtime < time()) {
  74. // 清除已经过期的文件
  75. unlink($filename);
  76. return $default;
  77. }
  78. return include $filename;
  79. } else {
  80. return $default;
  81. }
  82. }
  83. /**
  84. * 写入缓存
  85. * @access public
  86. * @param string $name 缓存变量名
  87. * @param mixed $value 存储数据
  88. * @param int|\DateTime $expire 有效时间 0为永久
  89. * @return bool
  90. */
  91. public function set($name, $value, $expire = null)
  92. {
  93. $this->writeTimes++;
  94. if (is_null($expire)) {
  95. $expire = $this->options['expire'];
  96. }
  97. if ($expire instanceof \DateTime) {
  98. $expire = $expire->getTimestamp();
  99. } else {
  100. $expire = 0 === $expire ? 10 * 365 * 24 * 3600 : $expire;
  101. $expire = time() + $expire;
  102. }
  103. $filename = $this->getCacheKey($name);
  104. if ($this->tag && !is_file($filename)) {
  105. $first = true;
  106. }
  107. $ret = file_put_contents($filename, ("<?php return " . var_export($value, true) . ";"));
  108. // 通过设置修改时间实现有效期
  109. if ($ret) {
  110. isset($first) && $this->setTagItem($filename);
  111. touch($filename, $expire);
  112. }
  113. return $ret;
  114. }
  115. /**
  116. * 自增缓存(针对数值缓存)
  117. * @access public
  118. * @param string $name 缓存变量名
  119. * @param int $step 步长
  120. * @return false|int
  121. */
  122. public function inc($name, $step = 1)
  123. {
  124. if ($this->has($name)) {
  125. $value = $this->get($name) + $step;
  126. } else {
  127. $value = $step;
  128. }
  129. return $this->set($name, $value, 0) ? $value : false;
  130. }
  131. /**
  132. * 自减缓存(针对数值缓存)
  133. * @access public
  134. * @param string $name 缓存变量名
  135. * @param int $step 步长
  136. * @return false|int
  137. */
  138. public function dec($name, $step = 1)
  139. {
  140. if ($this->has($name)) {
  141. $value = $this->get($name) - $step;
  142. } else {
  143. $value = -$step;
  144. }
  145. return $this->set($name, $value, 0) ? $value : false;
  146. }
  147. /**
  148. * 删除缓存
  149. * @access public
  150. * @param string $name 缓存变量名
  151. * @return boolean
  152. */
  153. public function rm($name)
  154. {
  155. $this->writeTimes++;
  156. return unlink($this->getCacheKey($name));
  157. }
  158. /**
  159. * 清除缓存
  160. * @access public
  161. * @param string $tag 标签名
  162. * @return bool
  163. */
  164. public function clear($tag = null)
  165. {
  166. if ($tag) {
  167. // 指定标签清除
  168. $keys = $this->getTagItem($tag);
  169. foreach ($keys as $key) {
  170. unlink($key);
  171. }
  172. $this->rm($this->getTagKey($tag));
  173. return true;
  174. }
  175. $this->writeTimes++;
  176. array_map("unlink", glob($this->options['path'] . ($this->options['prefix'] ? $this->options['prefix'] . DIRECTORY_SEPARATOR : '') . '*.php'));
  177. }
  178. }