优惠券小程序

RedisModel.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Models\Redis;
  3. use Illuminate\Support\Facades\Redis;
  4. /**
  5. * @see \Predis\ClientInterface
  6. */
  7. class RedisModel
  8. {
  9. public static function keys($pattern)
  10. {
  11. return Redis::keys($pattern);
  12. }
  13. public static function exists($key)
  14. {
  15. return Redis::exists($key);
  16. }
  17. // -------------------------------- 字符串 string
  18. public static function get($key){
  19. return Redis::get($key);
  20. }
  21. public static function set($key, $data){
  22. return Redis::set($key, $data);
  23. }
  24. public static function expire($key, $time = 60){
  25. return Redis::expire($key, $time);
  26. }
  27. public static function setnx($key, $data){
  28. return Redis::setnx($key, $data);
  29. }
  30. public static function setex($key, $time, $data)
  31. {
  32. return Redis::setex($key, $time, $data);
  33. }
  34. public static function del($keys){
  35. return Redis::del(...(array) $keys);
  36. }
  37. // -------------------------------- 列表 List
  38. public static function lPush($key, $values)
  39. {
  40. return Redis::lPush($key, ...(array) $values);
  41. }
  42. public static function rPush($key, $values)
  43. {
  44. return Redis::rPush($key, ...(array) $values);
  45. }
  46. public static function rPop($key)
  47. {
  48. return Redis::rPop($key);
  49. }
  50. public static function brPop($key, $timeout = 0)
  51. {
  52. return Redis::brPop($key, $timeout);
  53. }
  54. public static function lPop($key)
  55. {
  56. return Redis::lPop($key);
  57. }
  58. public static function lIndex($key, $index)
  59. {
  60. return Redis::lIndex($key, $index);
  61. }
  62. public static function lRange($key, $start = 0, $end = -1)
  63. {
  64. return Redis::lRange($key, $start, $end);
  65. }
  66. public static function lLen($key)
  67. {
  68. return Redis::lLen($key);
  69. }
  70. // -------------------------------- Hash
  71. public static function hSet($key, $field, $value)
  72. {
  73. return Redis::hSet($key, $field, $value);
  74. }
  75. public static function hMset($key, $hashKvs)
  76. {
  77. return Redis::hMset($key, $hashKvs);
  78. }
  79. public static function hMget($key, $hashKeys)
  80. {
  81. return Redis::hMget($key, (array) $hashKeys);
  82. }
  83. public static function hGetAll($key)
  84. {
  85. return Redis::hGetAll($key);
  86. }
  87. public static function hGet($key, $hashkey)
  88. {
  89. return Redis::hGet($key, $hashkey);
  90. }
  91. public static function hDel($key, $hashKeys)
  92. {
  93. return Redis::hDel($key, ...(array) $hashKeys);
  94. }
  95. public static function hLen($key)
  96. {
  97. return Redis::hLen($key);
  98. }
  99. public static function hSetnx($key, $field, $val)
  100. {
  101. return Redis::hSetnx($key, $field, $val);
  102. }
  103. public static function hIncrby($key, $field, $val)
  104. {
  105. return Redis::hIncrby($key, $field, $val);
  106. }
  107. /**
  108. * 从redis中取出数据并进行反序列化
  109. * @param $key
  110. * @return mixed
  111. */
  112. public static function getAfterDecode($key)
  113. {
  114. $res = static::get($key);
  115. return json_decode($res, true);
  116. }
  117. /**
  118. * 将数据序列化后存入redis
  119. * @param $key
  120. * @param $data
  121. * @param int $timeout
  122. * @return bool
  123. */
  124. public static function setAfterEncode($key, $data, $timeout = 0)
  125. {
  126. $data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  127. Redis::setnx($key, $timeout);
  128. return Redis::set($key, $data);
  129. }
  130. }