# Redis学习笔记三：Redis有序集的底层实现(跳表)

## 基本用法

#有序集
#sorted_set

zadd fruit 8 apple 2 banana 3 orange
zrange fruit 0 -1
1) "banana"
2) "orange"
3) "apple"
zrange fruit 0 -1 withscores
1) "banana"
2) "2"
3) "orange"
4) "3"
5) "apple"
6) "8"

zcount fruit 3 8
(integer) 2
zscore fruit apple
"8"

#价格由低到高取出前两位
zarange k1 0 1

#价格由高到低取出前两位
zrevarange k1 0 1

zscore fruit apple
"8"
zrank fruit banana
0

zincrby fruit 2.5 banana
"4.5"

127.0.0.1:6379> zadd k1 2 a 3 b 4 c
(integer) 3
127.0.0.1:6379> zadd k2 3 a 1 b 2 c
(integer) 3
127.0.0.1:6379> ZUNIONSTORE k4 2 k1 k2
(integer) 3
127.0.0.1:6379> zrange k4 0 -1
1) "b"
2) "a"
3) "c"
127.0.0.1:6379> ZUNIONSTORE k4 2 k1 k2 aggregate sum
(integer) 3
127.0.0.1:6379> zrange k4 0 -1
1) "b"
2) "a"
3) "c"
127.0.0.1:6379> ZUNIONSTORE k4 2 k1 k2 aggregate max
(integer) 3
127.0.0.1:6379> zrange k4 0 -1
1) "a"
2) "b"
3) "c"
127.0.0.1:6379> ZUNIONSTORE k4 2 k1 k2 aggregate min
(integer) 3
127.0.0.1:6379> zrange k4 0 -1
1) "b"
2) "a"
3) "c"


## 参考资料

Skip Lists: A Probabilistic Alternative to Balanced Trees (1990)

posted @ 2021-05-13 12:50  Grey Zeng  阅读(29)  评论(0编辑  收藏  举报