ChatCache.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * Class ChatTokenCache
  24. * @package app\common\cache
  25. */
  26. class ChatCache
  27. {
  28. private $redis;
  29. public function __construct()
  30. {
  31. $this->redis = Cache::store('redis')->handler();
  32. }
  33. /**
  34. * @notes 设置缓存
  35. * @param $key
  36. * @param $val
  37. * @param null $time
  38. * @return false
  39. * @author 段誉
  40. * @date 2021/12/20 12:13
  41. */
  42. public function set($key, $val, $time = null)
  43. {
  44. if (empty($key)) {
  45. return false;
  46. }
  47. return $this->redis->set($key, $val, $time);
  48. }
  49. /**
  50. * @notes 获取缓存
  51. * @param $key
  52. * @return false
  53. * @author 段誉
  54. * @date 2021/12/20 12:14
  55. */
  56. public function get($key)
  57. {
  58. if (empty($key)) {
  59. return false;
  60. }
  61. return $this->redis->get($key);
  62. }
  63. /**
  64. * @notes 删除指定
  65. * @param $key
  66. * @return mixed
  67. * @author 段誉
  68. * @date 2021/12/20 12:02
  69. */
  70. public function del($key)
  71. {
  72. return $this->redis->del($key);
  73. }
  74. /**
  75. * @notes 清空
  76. * @return mixed
  77. * @author 段誉
  78. * @date 2021/12/20 12:02
  79. */
  80. public function flashAll()
  81. {
  82. return $this->redis->flushAll();
  83. }
  84. /**
  85. * @notes 获取集合
  86. * @param $key
  87. * @return mixed
  88. * @author 段誉
  89. * @date 2021/12/20 12:11
  90. */
  91. public function sMembers($key)
  92. {
  93. return $this->redis->sMembers($key);
  94. }
  95. /**
  96. * @notes 设置缓存时间
  97. * @param $key
  98. * @param $ttl
  99. * @return mixed
  100. * @author 段誉
  101. * @date 2021/12/20 12:02
  102. */
  103. public function expire($key, $ttl)
  104. {
  105. return $this->redis->expire($key, $ttl);
  106. }
  107. /**
  108. * @notes 向集合添加成员
  109. * @param $key
  110. * @param $val
  111. * @return mixed
  112. * @author 段誉
  113. * @date 2021/12/20 12:04
  114. */
  115. public function sadd($key, $val)
  116. {
  117. return $this->redis->sAdd($key, $val);
  118. }
  119. /**
  120. * @notes 移除集合成员
  121. * @param $key
  122. * @param $val
  123. * @return mixed
  124. * @author 段誉
  125. * @date 2021/12/20 12:04
  126. */
  127. public function srem($key, $val)
  128. {
  129. return $this->redis->sRem($key, $val);
  130. }
  131. /**
  132. * @notes 对象转数组
  133. * @param $key
  134. * @return array|false
  135. * @author 段誉
  136. * @date 2021/12/20 12:03
  137. */
  138. public function getSmembersArray($key)
  139. {
  140. $res = $this->sMembers($key);
  141. if (is_object($res)) {
  142. return (array)$res;
  143. }
  144. return $res;
  145. }
  146. /**
  147. * @notes 相似keys
  148. * @param $prefix
  149. * @return mixed
  150. * @author 段誉
  151. * @date 2021/12/20 12:02
  152. */
  153. public function keys($prefix)
  154. {
  155. return $this->redis->keys($prefix.'*');
  156. }
  157. }