RedisService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. $expire = $expire !=null ? $expire : env('REDIS_EXPIRE', 24 * 3600);
  20. $data = is_array($data) && !empty($data)? json_encode($data, 256) : $data;
  21. $data = !empty($data)? $data : null;
  22. if ($expire > 0) {
  23. return BaseRedis::setex($key, $expire, $data);
  24. } else {
  25. return BaseRedis::set($key, $data);
  26. }
  27. }
  28. /**
  29. * 获取缓存数据
  30. * @param $key 键名
  31. * @return array|mixed
  32. */
  33. public static function get($key)
  34. {
  35. try{
  36. $data = BaseRedis::get($key);
  37. $jsonData = $data ? json_decode($data, true) : [];
  38. return $jsonData ? $jsonData : $data;
  39. } catch (\Exception $exception){
  40. return false;
  41. }
  42. }
  43. /**
  44. * 键名是否存在
  45. * @param $key
  46. * @param $expire
  47. * @return mixed
  48. */
  49. public static function exists($key){
  50. return BaseRedis::exists($key);
  51. }
  52. /**
  53. * 递增
  54. * @param $key
  55. * @return bool
  56. */
  57. public static function incr($key, $step=1){
  58. return BaseRedis::incrby($key, $step);
  59. }
  60. /**
  61. * 递减
  62. * @param $key
  63. * @return bool
  64. */
  65. public static function decr($key, $step=1){
  66. return BaseRedis::decrby($key, $step);
  67. }
  68. /**
  69. * 进队列缓存
  70. * @param $key 键名
  71. * @param $data 队列数据
  72. * @param bool $clear 是否清除之前数据
  73. * @return mixed
  74. */
  75. public static function rPush($key, $data, $clear = false)
  76. {
  77. if ($clear) {
  78. BaseRedis::del($key);
  79. }
  80. $data = is_array($data) ? json_encode($data, 256) : $data;
  81. return BaseRedis::rPush($key, $data);
  82. }
  83. /**
  84. * 出队列
  85. * @param $key 键名
  86. * @return array|mixed
  87. */
  88. public static function lPop($key)
  89. {
  90. $data = BaseRedis::lPop($key);
  91. $jsonData = $data ? json_decode($data, true) : [];
  92. return $jsonData ? $jsonData : $data;
  93. }
  94. /**
  95. * 清除缓存
  96. * @param $key 键名
  97. * @return mixed
  98. */
  99. public static function clear($key)
  100. {
  101. return BaseRedis::del($key);
  102. }
  103. /**
  104. * 按键名匹配删除
  105. * @param $key
  106. * @return bool
  107. */
  108. public static function keyDel($key){
  109. $keys = BaseRedis::keys($key);
  110. foreach($keys as $key){
  111. BaseRedis::del($key);
  112. }
  113. return true;
  114. }
  115. /**
  116. * 有效期设置
  117. * @param $key
  118. * @param $expire
  119. * @return mixed
  120. */
  121. public static function expire($key, $expire){
  122. return BaseRedis::expire($key, $expire);
  123. }
  124. /**
  125. * 原子锁
  126. * @param $key
  127. * @param $value
  128. * @param int $expire
  129. * @return mixed
  130. */
  131. public static function setnx($key, $value, $expire=0){
  132. $res = BaseRedis::setnx($key, $value);
  133. if($expire){
  134. BaseRedis::expire($key, $expire);
  135. }
  136. return $res;
  137. }
  138. }