Cache.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <[email protected]>
  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. public static function reset_init($options)
  78. {
  79. self::$handler = self::connect($options);
  80. return self::$handler;
  81. }
  82. /**
  83. * 切换缓存类型 需要配置 cache.type 为 complex
  84. * @access public
  85. * @param string $name 缓存标识
  86. * @return Driver
  87. */
  88. public static function store($name = '')
  89. {
  90. if ('' !== $name && 'complex' == Config::get('cache.type')) {
  91. return self::connect(Config::get('cache.' . $name), strtolower($name));
  92. }
  93. return self::init();
  94. }
  95. /**
  96. * 判断缓存是否存在
  97. * @access public
  98. * @param string $name 缓存变量名
  99. * @return bool
  100. */
  101. public static function has($name)
  102. {
  103. self::$readTimes++;
  104. return self::init()->has($name);
  105. }
  106. /**
  107. * 读取缓存
  108. * @access public
  109. * @param string $name 缓存标识
  110. * @param mixed $default 默认值
  111. * @return mixed
  112. */
  113. public static function get($name, $default = false)
  114. {
  115. self::$readTimes++;
  116. return self::init()->get($name, $default);
  117. }
  118. /**
  119. * 写入缓存
  120. * @access public
  121. * @param string $name 缓存标识
  122. * @param mixed $value 存储数据
  123. * @param int|null $expire 有效时间 0为永久
  124. * @return boolean
  125. */
  126. public static function set($name, $value, $expire = null)
  127. {
  128. self::$writeTimes++;
  129. return self::init()->set($name, $value, $expire);
  130. }
  131. /**
  132. * 自增缓存(针对数值缓存)
  133. * @access public
  134. * @param string $name 缓存变量名
  135. * @param int $step 步长
  136. * @return false|int
  137. */
  138. public static function inc($name, $step = 1)
  139. {
  140. self::$writeTimes++;
  141. return self::init()->inc($name, $step);
  142. }
  143. /**
  144. * 自减缓存(针对数值缓存)
  145. * @access public
  146. * @param string $name 缓存变量名
  147. * @param int $step 步长
  148. * @return false|int
  149. */
  150. public static function dec($name, $step = 1)
  151. {
  152. self::$writeTimes++;
  153. return self::init()->dec($name, $step);
  154. }
  155. /**
  156. * 删除缓存
  157. * @access public
  158. * @param string $name 缓存标识
  159. * @return boolean
  160. */
  161. public static function rm($name)
  162. {
  163. self::$writeTimes++;
  164. return self::init()->rm($name);
  165. }
  166. /**
  167. * 清除缓存
  168. * @access public
  169. * @param string $tag 标签名
  170. * @return boolean
  171. */
  172. public static function clear($tag = null)
  173. {
  174. self::$writeTimes++;
  175. return self::init()->clear($tag);
  176. }
  177. /**
  178. * 读取缓存并删除
  179. * @access public
  180. * @param string $name 缓存变量名
  181. * @return mixed
  182. */
  183. public static function pull($name)
  184. {
  185. self::$readTimes++;
  186. self::$writeTimes++;
  187. return self::init()->pull($name);
  188. }
  189. /**
  190. * 如果不存在则写入缓存
  191. * @access public
  192. * @param string $name 缓存变量名
  193. * @param mixed $value 存储数据
  194. * @param int $expire 有效时间 0为永久
  195. * @return mixed
  196. */
  197. public static function remember($name, $value, $expire = null)
  198. {
  199. self::$readTimes++;
  200. return self::init()->remember($name, $value, $expire);
  201. }
  202. /**
  203. * 缓存标签
  204. * @access public
  205. * @param string $name 标签名
  206. * @param string|array $keys 缓存标识
  207. * @param bool $overlay 是否覆盖
  208. * @return Driver
  209. */
  210. public static function tag($name, $keys = null, $overlay = false)
  211. {
  212. return self::init()->tag($name, $keys, $overlay);
  213. }
  214. }