数据库
首页 > 数据库> > Redis学习笔记三:Redis有序集的底层实现(跳表)

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

作者:互联网

我们可以使用Redis中的sorted_set对数据进行排序。

基本用法

#有序集
#sorted_set
#Z开头的命令,ZADD,ZCOUNT

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"

更多命令见:Redis学习笔记二:基本命令与使用场景

sorted_set的底层实现--跳表

关于跳表,我这里写了一篇文章详细介绍了一下跳表的实现细节:LeetCode 1206. Design Skiplist

参考资料

数据结构与算法——跳表

跳表(SkipList)设计与实现

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

标签:127.0,0.1,Redis,笔记,6379,fruit,跳表,k4,integer
来源: https://www.cnblogs.com/greyzeng/p/14750096.html