RedisService.php 3.4 KB

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