SortedSetTrait.php 2.8 KB

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