Skip to content

Commit

Permalink
add redis Zrevrank (zeromicro#137)
Browse files Browse the repository at this point in the history
* update goctl rpc template log print url

* add redis Zrevrank

Co-authored-by: zhangkai <zhangkai@laoyuegou.com>
  • Loading branch information
mywaystay and zhangkai authored Oct 19, 2020
1 parent 81bf122 commit 33faab6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/stores/kv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type (
ZrevrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error)
ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) ([]redis.Pair, error)
Zscore(key string, value string) (int64, error)
Zrevrank(key, field string) (int64, error)
}

clusterStore struct {
Expand Down Expand Up @@ -644,6 +645,15 @@ func (cs clusterStore) Zscore(key string, value string) (int64, error) {
return node.Zscore(key, value)
}

func (cs clusterStore) Zrevrank(key, field string) (int64, error) {
node, err := cs.getRedis(key)
if err != nil {
return 0, err
}

return node.Zrevrank(key, field)
}

func (cs clusterStore) getRedis(key string) (*redis.Redis, error) {
if val, ok := cs.dispatcher.Get(key); !ok {
return nil, ErrNoRedisNode
Expand Down
14 changes: 14 additions & 0 deletions core/stores/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,20 @@ func (s *Redis) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64
return
}

func (s *Redis) Zrevrank(key string, field string) (val int64, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}

val, err = conn.ZRevRank(key, field).Result()
return err
}, acceptable)

return
}

func (s *Redis) String() string {
return s.Addr
}
Expand Down
3 changes: 3 additions & 0 deletions core/stores/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ func TestRedis_SortedSet(t *testing.T) {
rank, err := client.Zrank("key", "value2")
assert.Nil(t, err)
assert.Equal(t, int64(1), rank)
rank, err = client.Zrevrank("key", "value1")
assert.Nil(t, err)
assert.Equal(t, int64(2), rank)
_, err = NewRedis(client.Addr, "").Zrank("key", "value4")
assert.NotNil(t, err)
_, err = client.Zrank("key", "value4")
Expand Down

0 comments on commit 33faab6

Please sign in to comment.