新版订单消耗系统

RedisModel.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App;
  3. use Illuminate\Support\Facades\Redis;
  4. class RedisModel
  5. {
  6. public static function get( $key ){
  7. return Redis::get($key);
  8. }
  9. public static function set( $key, $data){
  10. return Redis::set( $key, $data);
  11. }
  12. public static function expire( $key, $time = 60 ){
  13. return Redis::expire( $key, $time );
  14. }
  15. public static function setnx( $key, $data){
  16. return Redis::setnx( $key, $data);
  17. }
  18. public static function del( $keys ){
  19. return Redis::del( ...(array) $keys);
  20. }
  21. /**
  22. * 从redis中取出数据并进行反序列化
  23. * @param $key
  24. * @return mixed
  25. */
  26. public static function getAfterDecode($key)
  27. {
  28. $res = static::get($key);
  29. return json_decode($res, true);
  30. }
  31. /**
  32. * 将数据序列化后存入redis
  33. * @param $key
  34. * @param $data
  35. * @param int $timeout
  36. * @return bool
  37. */
  38. public static function setAfterEncode($key, $data, $timeout = 0)
  39. {
  40. $data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  41. Redis::setnx($key, $timeout);
  42. return Redis::set($key, $data);
  43. }
  44. public static function keys($pattern)
  45. {
  46. return Redis::keys($pattern);
  47. }
  48. // -------------------------------- 列表 List
  49. public static function lPush($key, $values)
  50. {
  51. return Redis::lPush($key, ...(array) $values);
  52. }
  53. public static function rPush($key, $values)
  54. {
  55. return Redis::rPush($key, ...(array) $values);
  56. }
  57. public static function rPop($key)
  58. {
  59. return Redis::rpop($key);
  60. }
  61. public static function lPop($key)
  62. {
  63. return Redis::lpop($key);
  64. }
  65. /**
  66. * 返回列表指定范围内的元素
  67. * @param $key
  68. * @param int $start
  69. * @param int $end
  70. * @return array
  71. */
  72. public static function lRange($key, $start = 0, $end = -1)
  73. {
  74. return Redis::lRange($key, $start, $end);
  75. }
  76. public static function lLen($key)
  77. {
  78. return Redis::llen($key);
  79. }
  80. public static function rPopLpush($srcKey, $dstKey)
  81. {
  82. $data = Redis::rpop($srcKey);
  83. Redis::lPush($dstKey, $data);
  84. return $data;
  85. }
  86. // -------------------------------- 集合 Set
  87. /**
  88. * # SADD key member1 [member2]
  89. *
  90. * 向集合添加一个或多个成员
  91. *
  92. * @param $key
  93. * @param $members
  94. * @return int 成功添加数量
  95. */
  96. public static function sAdd($key, $members)
  97. {
  98. // php >= 5.6
  99. return (int) Redis::sadd($key, ...(array) $members);
  100. // 通用写法
  101. // $params = array_merge([$key], (array) $members);
  102. // return (int) call_user_func_array([$redis, 'sadd'], $params);
  103. }
  104. public static function sRem($key, $members)
  105. {
  106. // php >= 5.6
  107. return Redis::sRem($key, ...(array) $members);
  108. }
  109. /**
  110. * @param $key
  111. * @return int
  112. */
  113. public static function sMembers($key)
  114. {
  115. return (int) Redis::sMembers($key);
  116. }
  117. /**
  118. * @param $key
  119. * @return int
  120. */
  121. public static function scard($key)
  122. {
  123. return (int) Redis::sCard($key);
  124. }
  125. // -------------------------------- 有序集合 Sorted Set
  126. public static function zAdd($key, $fieldScoreKv)
  127. {
  128. $params = [];
  129. foreach ($fieldScoreKv as $field => $score) {
  130. $params[] = $score;
  131. $params[] = $field;
  132. }
  133. return Redis::zAdd($key, ... $params);
  134. }
  135. public static function zRem($key, $members)
  136. {
  137. return Redis::zRem($key, ...(array) $members);
  138. }
  139. public static function zCard($key)
  140. {
  141. return Redis::zCard($key);
  142. }
  143. /**
  144. * 返回指定元素的分数
  145. * @param $key
  146. * @param $member
  147. * @return float
  148. */
  149. public static function zScore($key, $member)
  150. {
  151. return Redis::zScore($key, $member);
  152. }
  153. // -------------------------------- Hash
  154. public static function hMset($key, $hashKvs)
  155. {
  156. return Redis::hMset($key, $hashKvs);
  157. }
  158. public static function hMget($key, $hashKeys)
  159. {
  160. return Redis::hMget($key, (array) $hashKeys);
  161. }
  162. public static function hGetAll($key)
  163. {
  164. return Redis::hGetAll($key);
  165. }
  166. public static function hDel($key, $hashKeys)
  167. {
  168. return Redis::hDel($key, ...(array) $hashKeys);
  169. }
  170. public static function hLen($key)
  171. {
  172. return Redis::hLen($key);
  173. }
  174. }