説明なし

RedisModel.php 4.5KB

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