CacheService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use think\cache\Driver;
  13. use think\cache\TagSet;
  14. use think\facade\Cache;
  15. use think\facade\Config;
  16. /**
  17. * CRMEB 缓存类
  18. * Class CacheService
  19. * @package crmeb\services
  20. */
  21. class CacheService
  22. {
  23. /**
  24. * 缓存队列key
  25. * @var string[]
  26. */
  27. protected static $redisQueueKey = [
  28. 0 => 'product',
  29. 1 => 'seckill',
  30. 2 => 'bargain',
  31. 3 => 'combination',
  32. 6 => 'advance'
  33. ];
  34. /**
  35. * 过期时间
  36. * @var int
  37. */
  38. protected static $expire;
  39. /**
  40. * 获取缓存过期时间
  41. * @param int|null $expire
  42. * @return int
  43. */
  44. protected static function getExpire(int $expire = null): int
  45. {
  46. if ($expire == null) {
  47. if (self::$expire) {
  48. return (int)self::$expire;
  49. }
  50. $default = Config::get('cache.default');
  51. $expire = Config::get('cache.stores.' . $default . '.expire');
  52. if (!is_int($expire)) {
  53. $expire = (int)$expire;
  54. }
  55. }
  56. return self::$expire = $expire;
  57. }
  58. /**
  59. * 写入缓存
  60. * @param string $name 缓存名称
  61. * @param mixed $value 缓存值
  62. * @param int|null $expire 缓存时间,为0读取系统缓存时间
  63. */
  64. public static function set(string $name, $value, int $expire = null, string $tag = 'crmeb')
  65. {
  66. try {
  67. return Cache::tag($tag)->set($name, $value, $expire ?? self::getExpire($expire));
  68. } catch (\Throwable $e) {
  69. return false;
  70. }
  71. }
  72. /**
  73. * 如果不存在则写入缓存
  74. * @param string $name
  75. * @param mixed $default
  76. * @param int|null $expire
  77. * @param string $tag
  78. * @return mixed|string|null
  79. */
  80. public static function remember(string $name, $default = '', int $expire = null, string $tag = 'crmeb')
  81. {
  82. try {
  83. return Cache::tag($tag)->remember($name, $default, $expire ?? self::getExpire($expire));
  84. } catch (\Throwable $e) {
  85. try {
  86. if (is_callable($default)) {
  87. return $default();
  88. } else {
  89. return $default;
  90. }
  91. } catch (\Throwable $e) {
  92. return null;
  93. }
  94. }
  95. }
  96. /**
  97. * 读取缓存
  98. * @param string $name
  99. * @param mixed $default
  100. * @return mixed|string
  101. */
  102. public static function get(string $name, $default = '')
  103. {
  104. return Cache::get($name) ?? $default;
  105. }
  106. /**
  107. * 删除缓存
  108. * @param string $name
  109. * @return bool
  110. */
  111. public static function delete(string $name)
  112. {
  113. return Cache::delete($name);
  114. }
  115. /**
  116. * 清空缓存池
  117. * @return bool
  118. */
  119. public static function clear(string $tag = 'crmeb')
  120. {
  121. return Cache::tag($tag)->clear();
  122. }
  123. /**
  124. * 检查缓存是否存在
  125. * @param string $key
  126. * @return bool
  127. */
  128. public static function has(string $key)
  129. {
  130. try {
  131. return Cache::has($key);
  132. } catch (\Throwable $e) {
  133. return false;
  134. }
  135. }
  136. /** 以下三个方法仅开启redis之后才使用 */
  137. /**
  138. * 设置redis入库队列
  139. * @param string $unique
  140. * @param int $number
  141. * @param int $type
  142. * @param bool $isPush
  143. * @return false
  144. * @throws \Psr\SimpleCache\InvalidArgumentException
  145. */
  146. public static function setStock(string $unique, int $number, int $type = 1, bool $isPush = true)
  147. {
  148. if (Config::get('cache.default') == 'file') return true;
  149. if (!$unique || !$number) return false;
  150. $name = (self::$redisQueueKey[$type] ?? '') . '_' . $type . '_' . $unique;
  151. if ($isPush) {
  152. Cache::store('redis')->delete($name);
  153. }
  154. $data = [];
  155. for ($i = 1; $i <= $number; $i++) {
  156. $data[] = $i;
  157. }
  158. return Cache::store('redis')->lPush($name, ...$data);
  159. }
  160. /**
  161. * 是否有库存|返回库存
  162. * @param string $unique
  163. * @param int $number
  164. * @param int $type
  165. * @return bool
  166. */
  167. public static function checkStock(string $unique, int $number = 0, int $type = 1)
  168. {
  169. if (Config::get('cache.default') == 'file') return true;
  170. $name = (self::$redisQueueKey[$type] ?? '') . '_' . $type . '_' . $unique;
  171. if ($number) {
  172. return Cache::store('redis')->lLen($name) >= $number;
  173. } else {
  174. return Cache::store('redis')->lLen($name);
  175. }
  176. }
  177. /**
  178. * 弹出redis队列中的库存条数
  179. * @param string $unique
  180. * @param int $number
  181. * @param int $type
  182. * @return bool
  183. */
  184. public static function popStock(string $unique, int $number, int $type = 1)
  185. {
  186. if (Config::get('cache.default') == 'file') return true;
  187. if (!$unique || !$number) return false;
  188. $name = (self::$redisQueueKey[$type] ?? '') . '_' . $type . '_' . $unique;
  189. $res = true;
  190. if ($number > Cache::store('redis')->lLen($name)) {
  191. return false;
  192. }
  193. for ($i = 1; $i <= $number; $i++) {
  194. $res = $res && Cache::store('redis')->lPop($name);
  195. }
  196. return $res;
  197. }
  198. /**
  199. * 存入当前秒杀商品属性有序集合
  200. * @param $set_key
  201. * @param $score
  202. * @param $value
  203. * @return false
  204. */
  205. public static function zAdd($set_key, $score, $value)
  206. {
  207. if (Config::get('cache.default') == 'file') return true;
  208. try {
  209. return Cache::store('redis')->zAdd($set_key, $score, $value);
  210. } catch (\Throwable $e) {
  211. return false;
  212. }
  213. }
  214. /**
  215. * 取消集合中的秒杀商品
  216. * @param $set_key
  217. * @param $value
  218. * @return false
  219. */
  220. public static function zRem($set_key, $value)
  221. {
  222. if (Config::get('cache.default') == 'file') return true;
  223. try {
  224. return Cache::store('redis')->zRem($set_key, $value);
  225. } catch (\Throwable $e) {
  226. return false;
  227. }
  228. }
  229. /**
  230. * 检查锁
  231. * @param string $key
  232. * @param int $timeout
  233. * @return bool
  234. * @author 等风来
  235. * @email 136327134@qq.com
  236. * @date 2022/11/22
  237. */
  238. public static function setMutex(string $key, int $timeout = 10): bool
  239. {
  240. $curTime = time();
  241. $readMutexKey = "redis:mutex:{$key}";
  242. $mutexRes = Cache::store('redis')->handler()->setnx($readMutexKey, $curTime + $timeout);
  243. if ($mutexRes) {
  244. return true;
  245. }
  246. //就算意外退出,下次进来也会检查key,防止死锁
  247. $time = Cache::store('redis')->handler()->get($readMutexKey);
  248. if ($curTime > $time) {
  249. Cache::store('redis')->handler()->del($readMutexKey);
  250. return Cache::store('redis')->handler()->setnx($readMutexKey, $curTime + $timeout);
  251. }
  252. return false;
  253. }
  254. /**
  255. * 删除锁
  256. * @param string $key
  257. * @author 等风来
  258. * @email 136327134@qq.com
  259. * @date 2022/11/22
  260. */
  261. public static function delMutex(string $key)
  262. {
  263. $readMutexKey = "redis:mutex:{$key}";
  264. Cache::store('redis')->handler()->del($readMutexKey);
  265. }
  266. /**
  267. * Redis缓存句柄
  268. * @param null $type
  269. * @return mixed
  270. * @author 吴汐
  271. * @email 442384644@qq.com
  272. * @date 2023/02/10
  273. */
  274. public static function redisHandler($type = null)
  275. {
  276. if ($type) {
  277. return Cache::store('redis')->tag($type);
  278. } else {
  279. return Cache::store('redis');
  280. }
  281. }
  282. }