redis基本数据结构-有序集合zset
特性
- 使用哈希表+跳表数据结构存储
- 每个元素由 分数和字段名 两部分组成
- 增加元素
zadd key score1 member1 [score2 member2 score3 member3 ...]
zadd scores 90 zhangsan 99 lisi 95 wangwu
返回3 (返回写入有序集合中的元素个数)
scores = {90 zhangsan, 99 lisi, 95 wangwu}
- 修改元素的分数(与增加类似)
zadd key score member
zadd socres 92 zhangsan
scores = {92 zhangsan, 99 lisi, 95 wangwu}
- 获取元素的分数
zscore key member
zscore scores lisi
返回99
- 获取排名在某个范围内的元素列表(从小到大的升序)
zrange key startRank endRank [WITHSCORES]
zrange scores 0 1 withscores
返回{92 zhangsan, 95 wangwu}
- 获取排名在某个范围内的元素列表(从大到小降序)
zrevrange key startRank endRank [WITHSCORES]
zrevrange scores 0 1 withscores
返回{99 lisi, 95 wangwu}
zrevrange scores 0 -1 withscores
返回{99 lisi, 95 wangwu, 92 zhangsan}
说明:以上排序中,当score相同时,按照 '0' < '9' < 'A' < 'Z' < 'a' < 'z' 的顺序排序。
- 获取指定分数范围内的元素
ZRangeByScore key min max [WITHSCORES] [LIMIT offset count]
zrangebyscore scores 93 100 withscores
返回 {99 lisi, 95 wangwu}
zrangebyscore scores (95 +inf withscores
返回 {99 lisi}
zrangebyscore scores 90 100 LIMIT 1 10000
返回 {99 lisi, 95 wangwu}, 跳过了1个(92 zhangsan),返回10000个,但是不足10000个所以只返回了实际个数的元素
- 增加某个元素的分数
ZIncrBy key increment member
zincrby scores 5 zhangsan
返回 97 (张三的92 + 5 == 97)
scores = {97 zhangsan, 99 lisi, 95 wangwu}
- 获取集合中的元素数量
ZCard key
zcard scores
返回3
- 获取指定分数范围内的元素个数
ZCount key min max
zcount scores (95 +inf
返回2 ( (95, +inf)共有2个)
- 删除一个或多个元素
ZRem key member1 [member2 member3 ...]
zrem scores zhangsan wagnwu
返回2 (实际删除的元素个数)
scores = {99 lisi}
zrem scores
返回1,删除scores的所有元素(删除前还剩一个)
- 按照排名范围删除元素
ZRemRangeByRank key startRank endRank
zadd scores 97 zhangsan 99 lisi 95 wangwu 90 liuwu
scores = { 97 zhangsan, 99 lisi, 95 wangwu, 90 liuwu}
zremrangebyrank scores 0 2
返回3 (删除了3个)
scores = { 99 lisi}
- 按照分数范围删除元素
ZRemRangeByScore key minScore maxScore
zadd scores 97 zhangsan 95 wangwu 90 liuwu
scores = { 97 zhangsan, 99 lisi, 95 wangwu, 90 liuwu}
zremrangebyscore scores 90 96
返回2(实际删除了2个元素)
scores = { 97 zhangsan, 99 lisi}
- 获得元素的排名(升序)
ZRank key member
zrank scores lisi
返回1,排名1
zrank scors zhangsan
返回0,排名0
- 获得元素的排名(降序)
ZRevRank key member
zrevrank scores lisi
返回0,排名0
zrevrank scores zhangsan
返回1,排名1
- 有序集合的交集运算
ZInterStore destSet setCounts zset1 zset2 [zset3 ...] [WEIGHTS weight1 weight2 [weight3 ...]] [AGGREGATE SUM | MAX | MIN]
参数说明:
destSet: 交集运算结果存放到destSet 有序集合
setCounts:参与运算的集合数量
zset1 zset2 ... :参与运算的集合,setCounts 要与实际参与运算的集合数量一致
WEIGHTS:命令选型,后面接每个set的在进行 SUM 或 MAX 或 MIN 之前先使用weight值对自身的score进行乘法运算,然后再进行SUM | MAX | MIN 运算
AGGRATE:命令选型,后接SUM 集合中相同元素的分数经过weight*score后相加 或 MAX 集合中相同元素的分数经过weight * score后取分数大的那个 或 MIN 集合中相同元素的分数经过weight * score 后取分数小的;
zadd zset1 100 first 200 second
zset1 = {100 first, 200 second}
zadd zset2 10 first 20 second
zset2 = {10 first, 20 second}
zinterstore zset3 2 zset1 zset2 === zinterstore zset3 zset1 zset2 AGGREGATE SUM === zinterstore zset3 WEIGHTS 1 1 AGGREGATE SUM
zset3 = {110 first, 220 second}
zinterstore zset3 2 zset1 zset2 WEIGHTS 0.01 1 AGGREGATE MAX
zset3 = {max(100 * 0.01, 10 * 1) first, max(200 * 0.01, 20 * 1) second} === {1 first, 2 second}
- 有序集合的并集运算
与 交集运算 类似,略
ZInterStore destSet setCounts zset1 zset2 [zset3 ...] [WEIGHTS weight1 weight2 [weight3 ...]] [AGGREGATE SUM | MAX | MIN]