zset.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package cache
  2. import (
  3. "fmt"
  4. "github.com/go-redis/redis"
  5. )
  6. func (p *RedisCache) ZRevRange(key string, start, stop int64) ([]string, error) {
  7. if p.isCluster {
  8. return p.cluster.ZRevRange(key, start, stop).Result()
  9. }
  10. return p.client.ZRevRange(key, start, stop).Result()
  11. }
  12. func (p *RedisCache) ZRevRangeByScore(key string, max, min string, offset, count int64) ([]string, error) {
  13. if p.isCluster {
  14. return p.cluster.ZRevRangeByScore(key,
  15. redis.ZRangeBy{
  16. Min: min,
  17. Max: max,
  18. Offset: offset,
  19. Count: count,
  20. }).Result()
  21. }
  22. return p.client.ZRevRangeByScore(key,
  23. redis.ZRangeBy{
  24. Min: min,
  25. Max: max,
  26. Offset: offset,
  27. Count: count,
  28. }).Result()
  29. }
  30. func (p *RedisCache) ZAdd(key string, score float64, member interface{}) (int64, error) {
  31. if p.isCluster {
  32. return p.cluster.ZAdd(key,
  33. redis.Z{
  34. Score: score,
  35. Member: member,
  36. }).Result()
  37. }
  38. return p.client.ZAdd(key,
  39. redis.Z{
  40. Score: score,
  41. Member: member,
  42. }).Result()
  43. }
  44. func (p *RedisCache) ZCount(key, min, max string) (int64, error) {
  45. if p.isCluster {
  46. return p.cluster.ZCount(key, min, max).Result()
  47. }
  48. return p.client.ZCount(key, min, max).Result()
  49. }
  50. func (p *RedisCache) ZScore(key, member string) (float64, error) {
  51. if p.isCluster {
  52. return p.cluster.ZScore(key, member).Result()
  53. }
  54. return p.client.ZScore(key, member).Result()
  55. }
  56. func (p *RedisCache) ZRange(key string, start, stop int64) ([]string, error) {
  57. if p.isCluster {
  58. return p.cluster.ZRange(key, start, stop).Result()
  59. }
  60. return p.client.ZRange(key, start, stop).Result()
  61. }
  62. func (p *RedisCache) ZIncrBy(key string, incr float64, member string) (float64, error) {
  63. if p.isCluster {
  64. return p.cluster.ZIncrBy(key, incr, member).Result()
  65. }
  66. return p.client.ZIncrBy(key, incr, member).Result()
  67. }
  68. func (p *RedisCache) ZRem(key string, members ...interface{}) (int64, error) {
  69. if p.isCluster {
  70. return p.cluster.ZRem(key, members).Result()
  71. }
  72. return p.client.ZRem(key, members).Result()
  73. }
  74. func (p *RedisCache) ZUnionStore(dest string, weights []float64, aggregate string, keys ...string) (int64, error) {
  75. if p.isCluster {
  76. return 0, fmt.Errorf("cluster unsupport <zunionstore>.")
  77. }
  78. return p.client.ZUnionStore(dest,
  79. redis.ZStore{
  80. Weights: weights,
  81. Aggregate: aggregate,
  82. },
  83. keys...).Result()
  84. }
  85. func (p *RedisCache) ZCard(key string) (int64, error) {
  86. if p.isCluster {
  87. return p.cluster.ZCard(key).Result()
  88. }
  89. return p.client.ZCard(key).Result()
  90. }
  91. func (p *RedisCache) ZRemRangeByScore(key, min, max string) (int64, error) {
  92. if p.isCluster {
  93. return p.cluster.ZRemRangeByScore(key, min, max).Result()
  94. }
  95. return p.client.ZRemRangeByScore(key, min, max).Result()
  96. }