HandleConcurrencyCache.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\cache;
  20. use think\facade\Cache;
  21. /**
  22. * 处理高并发缓存
  23. */
  24. class HandleConcurrencyCache extends BaseCache
  25. {
  26. protected $tagName = 'concurrency';
  27. /**
  28. * @notes 获取缓存
  29. * @param $cacheName //缓存名称
  30. * @return false|mixed
  31. * @author ljj
  32. * @date 2025/2/20 下午2:50
  33. */
  34. public function getCache($cacheName)
  35. {
  36. $result = $this->get($this->tagName.'-'.$cacheName);
  37. if($result) {
  38. return $result;
  39. }
  40. return false;
  41. }
  42. /**
  43. * @notes 设置缓存
  44. * @param $cacheName //缓存名称
  45. * @param $data //缓存数据
  46. * @param $expire //缓存过期时间
  47. * @author ljj
  48. * @date 2025/2/20 下午2:50
  49. */
  50. public function setCache($cacheName,$data, $expire = null)
  51. {
  52. //设置新缓存
  53. $this->set($this->tagName.'-'.$cacheName, $data, $expire);
  54. }
  55. /**
  56. * @notes 删除缓存
  57. * @param $cacheName
  58. * @author ljj
  59. * @date 2025/2/20 下午8:09
  60. */
  61. public function deleteCache($cacheName)
  62. {
  63. //删除旧缓存
  64. $this->delete($this->tagName.'-'.$cacheName);
  65. }
  66. /**
  67. * @notes 清除指定前缀的缓存
  68. * @param $cacheName
  69. * @return true
  70. * @author ljj
  71. * @date 2025/2/20 下午9:03
  72. */
  73. public function clearPrefix($cacheName)
  74. {
  75. $keys = $this->get($this->tagName.'-'.$cacheName.'*');
  76. foreach ($keys as $key) {
  77. $this->delete($key);
  78. }
  79. return true;
  80. }
  81. /**
  82. * @notes 校验并获取redis缓存对象
  83. * @return false|\think\cache\Driver
  84. * @author ljj
  85. * @date 2025/3/24 下午5:50
  86. */
  87. public function getRedis()
  88. {
  89. try {
  90. // 尝试连接 Redis
  91. $redis = Cache::store('redis')->handler();
  92. $redis->ping(); // 测试连接,成功返回 +PONG
  93. return Cache::store('redis');
  94. } catch (\Exception $e) {
  95. return false;
  96. }
  97. }
  98. //装修主题配置缓存键
  99. public function getDecorateThemeConfigKey()
  100. {
  101. return 'decorate_theme_config';
  102. }
  103. //分享配置缓存键
  104. public function getShareConfigKey()
  105. {
  106. return 'share_config';
  107. }
  108. //首页装修缓存键
  109. public function getDecorateThemeHomeKey()
  110. {
  111. return 'decorate_theme_home';
  112. }
  113. //装修页面缓存键
  114. public function getDecorateThemePageKey($type,$userId,$goodsId)
  115. {
  116. return 'decorate_theme_page_'.$type.$userId.$goodsId;
  117. }
  118. //商品列表缓存键
  119. public function getGoodsListsKey($suffix)
  120. {
  121. return 'goods_lists_'.$suffix;
  122. }
  123. //商品搜索记录缓存键
  124. public function getGoodsSearchRecordKey($user_id)
  125. {
  126. return 'goods_search_record_'.$user_id;
  127. }
  128. //下单限流缓存键
  129. public function getSubmitOrderLimitKey($user_id)
  130. {
  131. return 'submit_order_limit_'.$user_id;
  132. }
  133. }