RedisService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * REDIS缓存模块服务层
  4. * Class RedisService
  5. */
  6. namespace App\Services;
  7. use Illuminate\Support\Facades\Redis as BaseRedis;
  8. class RedisService extends BaseRedis
  9. {
  10. /**
  11. * 设置缓存
  12. * @param $key 键名
  13. * @param $data 数据:数组和字符串,数组自动转化JSON
  14. * @param int $expire 有效期:0为永久
  15. * @return mixed
  16. */
  17. public static function set($key, $data, $expire =null)
  18. {
  19. try{
  20. $expire = $expire !=null ? $expire : env('REDIS_EXPIRE', 24 * 3600);
  21. $data = is_array($data) && !empty($data)? json_encode($data, 256) : $data;
  22. $data = !empty($data)? $data : null;
  23. if ($expire > 0) {
  24. return BaseRedis::setex($key, $expire, $data);
  25. } else {
  26. return BaseRedis::set($key, $data);
  27. }
  28. }catch (\Exception $exception){
  29. return false;
  30. }
  31. }
  32. /**
  33. * 获取缓存数据
  34. * @param $key 键名
  35. * @return array|mixed
  36. */
  37. public static function get($key)
  38. {
  39. try {
  40. $data = BaseRedis::get($key);
  41. $jsonData = $data ? json_decode($data, true) : [];
  42. return $jsonData ? $jsonData : $data;
  43. }catch (\Exception $exception){
  44. return false;
  45. }
  46. }
  47. /**
  48. * 键名是否存在
  49. * @param $key
  50. * @param $expire
  51. * @return mixed
  52. */
  53. public static function exists($key){
  54. return BaseRedis::exists($key);
  55. }
  56. /**
  57. * 递增
  58. * @param $key
  59. * @return bool
  60. */
  61. public static function incr($key, $step=1){
  62. return BaseRedis::incrby($key, $step);
  63. }
  64. /**
  65. * 递减
  66. * @param $key
  67. * @return bool
  68. */
  69. public static function decr($key, $step=1){
  70. return BaseRedis::decrby($key, $step);
  71. }
  72. /**
  73. * 进队列缓存
  74. * @param $key 键名
  75. * @param $data 队列数据
  76. * @param bool $clear 是否清除之前数据
  77. * @return mixed
  78. */
  79. public static function rPush($key, $data, $clear = false)
  80. {
  81. if ($clear) {
  82. BaseRedis::del($key);
  83. }
  84. $data = is_array($data) ? json_encode($data, 256) : $data;
  85. return BaseRedis::rPush($key, $data);
  86. }
  87. /**
  88. * 出队列
  89. * @param $key 键名
  90. * @return array|mixed
  91. */
  92. public static function lPop($key)
  93. {
  94. $data = BaseRedis::lPop($key);
  95. $jsonData = $data ? json_decode($data, true) : [];
  96. return $jsonData ? $jsonData : $data;
  97. }
  98. /**
  99. * 清除缓存
  100. * @param $key 键名
  101. * @return mixed
  102. */
  103. public static function clear($key)
  104. {
  105. try{
  106. return BaseRedis::del($key);
  107. }catch (\Exception $exception){
  108. return false;
  109. }
  110. }
  111. /**
  112. * 按键名匹配删除
  113. * @param $key
  114. * @return bool
  115. */
  116. public static function keyDel($key){
  117. try {
  118. $keys = BaseRedis::keys($key);
  119. foreach($keys as $key){
  120. BaseRedis::del($key);
  121. }
  122. return true;
  123. } catch (\Exception $exception){
  124. return false;
  125. }
  126. }
  127. /**
  128. * 有效期设置
  129. * @param $key
  130. * @param $expire
  131. * @return mixed
  132. */
  133. public static function expire($key, $expire){
  134. return BaseRedis::expire($key, $expire);
  135. }
  136. /**
  137. * 原子锁
  138. * @param $key
  139. * @param $value
  140. * @param int $expire
  141. * @return mixed
  142. */
  143. public static function setnx($key, $value, $expire=0){
  144. $res = BaseRedis::setnx($key, $value);
  145. if($expire){
  146. BaseRedis::expire($key, $expire);
  147. }
  148. return $res;
  149. }
  150. }