cffttRedis ZSET (Sorted Set) is a collection where:
- value(zset) ≈ { member: score }
- each element is a unique
member - each member has a numeric
score(float)- e.g.,
key = topic:123:comments:hot value(zset)={('cmt_777', 532), ('cmt_102', 120), ('cmt_888', 999) ... }
- e.g.,
- Redis keeps the set automatically sorted by score (ascending)
- if scores tie, it sorts by member lexicographically
Core commands
-
Add/update score
ZADD <key> <score1> <member1> <score2> <member2> ...
-
Increment a member’s score
ZINCRBY <key> 5 bob
-
Get Top N (highest score first)
ZREVRANGE <key> 0 9 WITHSCORES
-
Get rank
- rank from high → low:
ZREVRANK <key> bob - rank from low → high:
ZRANK <key> bob
- rank from high → low:
-
Get a member’s score
ZSCORE <key> alice
-
Get members by score range
ZRANGEBYSCORE <key> 90 100 WITHSCORES
-
Count / size
ZCARD <key>ZCOUNT <key> 90 100
-
Remove
ZREM <key> bob
-
Write/update
ZINCRBY topic:123:comments:hot 1 cmt_777cmt_777+ 1
-
取 Top N 最热评论(Top 10)
ZREVRANGE topic:123:comments:hot 0 9 WITHSCORES
-
分页获取(第 2 页)
ZREVRANGE topic:123:comments:hot 10 19 WITHSCORES
-
取某个评论的 rank(排名)和 score