You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

248 lines
6.1 KiB

  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;
  12. use think\cache\Driver;
  13. class Cache
  14. {
  15. /**
  16. * @var array 缓存的实例
  17. */
  18. public static $instance = [];
  19. /**
  20. * @var int 缓存读取次数
  21. */
  22. public static $readTimes = 0;
  23. /**
  24. * @var int 缓存写入次数
  25. */
  26. public static $writeTimes = 0;
  27. /**
  28. * @var object 操作句柄
  29. */
  30. public static $handler;
  31. /**
  32. * 连接缓存驱动
  33. * @access public
  34. * @param array $options 配置数组
  35. * @param bool|string $name 缓存连接标识 true 强制重新连接
  36. * @return Driver
  37. */
  38. public static function connect(array $options = [], $name = false)
  39. {
  40. $type = !empty($options['type']) ? $options['type'] : 'File';
  41. if (false === $name) {
  42. $name = md5(serialize($options));
  43. }
  44. if (true === $name || !isset(self::$instance[$name])) {
  45. $class = false === strpos($type, '\\') ?
  46. '\\think\\cache\\driver\\' . ucwords($type) :
  47. $type;
  48. // 记录初始化信息
  49. App::$debug && Log::record('[ CACHE ] INIT ' . $type, 'info');
  50. if (true === $name) {
  51. return new $class($options);
  52. }
  53. self::$instance[$name] = new $class($options);
  54. }
  55. return self::$instance[$name];
  56. }
  57. /**
  58. * 自动初始化缓存
  59. * @access public
  60. * @param array $options 配置数组
  61. * @return Driver
  62. */
  63. public static function init(array $options = [])
  64. {
  65. if (is_null(self::$handler)) {
  66. if (empty($options) && 'complex' == Config::get('cache.type')) {
  67. $default = Config::get('cache.default');
  68. // 获取默认缓存配置,并连接
  69. $options = Config::get('cache.' . $default['type']) ?: $default;
  70. } elseif (empty($options)) {
  71. $options = Config::get('cache');
  72. }
  73. self::$handler = self::connect($options);
  74. }
  75. return self::$handler;
  76. }
  77. /**
  78. * 切换缓存类型 需要配置 cache.type 为 complex
  79. * @access public
  80. * @param string $name 缓存标识
  81. * @return Driver
  82. */
  83. public static function store($name = '')
  84. {
  85. if ('' !== $name && 'complex' == Config::get('cache.type')) {
  86. return self::connect(Config::get('cache.' . $name), strtolower($name));
  87. }
  88. return self::init();
  89. }
  90. /**
  91. * 判断缓存是否存在
  92. * @access public
  93. * @param string $name 缓存变量名
  94. * @return bool
  95. */
  96. public static function has($name)
  97. {
  98. self::$readTimes++;
  99. return self::init()->has($name);
  100. }
  101. /**
  102. * 读取缓存
  103. * @access public
  104. * @param string $name 缓存标识
  105. * @param mixed $default 默认值
  106. * @return mixed
  107. */
  108. public static function get($name, $default = false)
  109. {
  110. self::$readTimes++;
  111. return self::init()->get($name, $default);
  112. }
  113. /**
  114. * 写入缓存
  115. * @access public
  116. * @param string $name 缓存标识
  117. * @param mixed $value 存储数据
  118. * @param int|null $expire 有效时间 0为永久
  119. * @return boolean
  120. */
  121. public static function set($name, $value, $expire = null)
  122. {
  123. self::$writeTimes++;
  124. return self::init()->set($name, $value, $expire);
  125. }
  126. /**
  127. * 自增缓存(针对数值缓存)
  128. * @access public
  129. * @param string $name 缓存变量名
  130. * @param int $step 步长
  131. * @return false|int
  132. */
  133. public static function inc($name, $step = 1)
  134. {
  135. self::$writeTimes++;
  136. return self::init()->inc($name, $step);
  137. }
  138. /**
  139. * 自减缓存(针对数值缓存)
  140. * @access public
  141. * @param string $name 缓存变量名
  142. * @param int $step 步长
  143. * @return false|int
  144. */
  145. public static function dec($name, $step = 1)
  146. {
  147. self::$writeTimes++;
  148. return self::init()->dec($name, $step);
  149. }
  150. /**
  151. * 删除缓存
  152. * @access public
  153. * @param string $name 缓存标识
  154. * @return boolean
  155. */
  156. public static function rm($name)
  157. {
  158. self::$writeTimes++;
  159. return self::init()->rm($name);
  160. }
  161. /**
  162. * 清除缓存
  163. * @access public
  164. * @param string $tag 标签名
  165. * @return boolean
  166. */
  167. public static function clear($tag = null)
  168. {
  169. self::$writeTimes++;
  170. return self::init()->clear($tag);
  171. }
  172. /**
  173. * 读取缓存并删除
  174. * @access public
  175. * @param string $name 缓存变量名
  176. * @return mixed
  177. */
  178. public static function pull($name)
  179. {
  180. self::$readTimes++;
  181. self::$writeTimes++;
  182. return self::init()->pull($name);
  183. }
  184. /**
  185. * 如果不存在则写入缓存
  186. * @access public
  187. * @param string $name 缓存变量名
  188. * @param mixed $value 存储数据
  189. * @param int $expire 有效时间 0为永久
  190. * @return mixed
  191. */
  192. public static function remember($name, $value, $expire = null)
  193. {
  194. self::$readTimes++;
  195. return self::init()->remember($name, $value, $expire);
  196. }
  197. /**
  198. * 缓存标签
  199. * @access public
  200. * @param string $name 标签名
  201. * @param string|array $keys 缓存标识
  202. * @param bool $overlay 是否覆盖
  203. * @return Driver
  204. */
  205. public static function tag($name, $keys = null, $overlay = false)
  206. {
  207. return self::init()->tag($name, $keys, $overlay);
  208. }
  209. }