Cache.php 6.0 KB

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