RedisService.php 3.3 KB

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