SortedSetTrait.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace tw\redis\traits;
  3. use tw\redis\TwRedis;
  4. trait SortedSetTrait
  5. {
  6. /**
  7. * 添加/更新一个元素
  8. *
  9. * @param string $val
  10. * @param float $score
  11. * @return int 0|1 1代表新增
  12. */
  13. public function zadd($word, $val, $score): int
  14. {
  15. return TwRedis::zAdd($this->key($word), $score, $val);
  16. }
  17. /**
  18. * 删除 1 个或多个元素
  19. */
  20. public function zrem($word, $members): int
  21. {
  22. if (is_array($members)) {
  23. return TwRedis::zRem($this->key($word), ...$members);
  24. } else {
  25. return TwRedis::zRem($this->key($word), $members);
  26. }
  27. }
  28. /**
  29. * 求一个元素的分数
  30. *
  31. * @param string $member
  32. * @return float|bool 分数|失败
  33. */
  34. public function zscore($word, $member)
  35. {
  36. return TwRedis::zScore($this->key($word), $member);
  37. }
  38. /**
  39. * 求一个元素的排名
  40. *
  41. * @param string $member
  42. * @return float 名次
  43. */
  44. public function zrank($word, $member): float
  45. {
  46. return TwRedis::zRank($this->key($word), $member);
  47. }
  48. /**
  49. * 求 zset 的基数
  50. */
  51. public function zcard($word): int
  52. {
  53. return TwRedis::zCard($this->key($word));
  54. }
  55. /**
  56. * 求分数在 $start,$end 之间的元素个数
  57. *
  58. * @param float|string $start,$end: 也可以包括 +inf -inf 表示正负无穷
  59. * @return
  60. */
  61. public function zcount($word, $start, $end): int
  62. {
  63. return TwRedis::zCount($this->key($word), $start, $end);
  64. }
  65. /**
  66. * 增加指定元素的分数
  67. */
  68. public function zincr_by($word, $member, $by): float
  69. {
  70. return TwRedis::zIncrBy($this->key($word), $by, $member);
  71. }
  72. /**
  73. * 弹出分数最大的几个
  74. */
  75. public function zpop_max($word, $num): array
  76. {
  77. return TwRedis::zPopMax($this->key($word), $num);
  78. }
  79. /**
  80. * 弹出分数最小的几个
  81. */
  82. public function zpop_min($word, $num): array
  83. {
  84. return TwRedis::zPopMin($this->key($word), $num);
  85. }
  86. /**
  87. * 返回分数在 $start, $end 之间的子集合
  88. * @param float|string $start, $end: 范围 +inf, -inf 也可以
  89. * @param array $options: 例子:
  90. * ['withscores' => TRUE]
  91. * ['limit' => [1, 1]]
  92. * ['withscores' => TRUE, 'limit' => [1, 1]]
  93. * @return
  94. */
  95. public function zrange_by_score($word, $start, $end, $options): array
  96. {
  97. return TwRedis::zRangeByScore($this->key($word), $start, $end, $options);
  98. }
  99. public function zrem_range_by_rank($word, $start, $end)
  100. {
  101. return TwRedis::zRemRangeByRank($this->key($word), $start, $end);
  102. }
  103. public function zrem_range_by_score($word, $start, $end)
  104. {
  105. return TwRedis::zRemRangeByScore($this->key($word), $start, $end);
  106. }
  107. }