文章目录
- Redis之zset类型
- 1. 添加元素/获取集合中元素的数量
- 2. 按照元素分数从小到大的顺序返回索引从start到stop之间的所有元素
- 3. 获取元素的分数
- 4. 删除元素
- 5. 获取指定分数范围的元素
- 6. 增加某个元素的分数
- 7. 获得指定分数范围内的元素个数
- 8. 获取元素的排名
- 9. 按照排名范围内删除元素
- 10. 应用场景
- 10.1 商品销量排名
- 10.2 抖音热搜
Redis之zset类型
1. 添加元素/获取集合中元素的数量
添加元素:ZADD key score member [score member …]
获取集合中元素的数量:ZCARD key
127.0.0.1:6379> zadd z1 100 apple 200 banana
(integer) 2
127.0.0.1:6379> zcard z1
(integer) 2
2. 按照元素分数从小到大的顺序返回索引从start到stop之间的所有元素
ZRANGE key start stop [WITHSCORES]
127.0.0.1:6379> zrange z1 0 -1
1) "apple"
2) "banana"
3. 获取元素的分数
ZSCORE key member
127.0.0.1:6379> zscore z1 apple
"100"
127.0.0.1:6379> zscore z1 banana
"200"
4. 删除元素
ZREM key member [member …]
127.0.0.1:6379> zrem z1 banana
(integer) 1
127.0.0.1:6379> zcard z1
(integer) 1
127.0.0.1:6379> zrange z1 0 -1
1) "apple"
5. 获取指定分数范围的元素
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
127.0.0.1:6379> zadd z1 200 banana 300 pear 400 peach
(integer) 3
127.0.0.1:6379> zcard z1
(integer) 4
127.0.0.1:6379> zrangebyscore z1 200 400
1) "banana"
2) "pear"
3) "peach"
127.0.0.1:6379> zrangebyscore z1 100 300
1) "apple"
2) "banana"
3) "pear"
6. 增加某个元素的分数
ZINCRBY key increment member
127.0.0.1:6379> zincrby z1 50 apple
"150"
127.0.0.1:6379> zscore z1 apple
"150"
7. 获得指定分数范围内的元素个数
ZCOUNT key min max
127.0.0.1:6379> zcount z1 200 400
(integer) 3
127.0.0.1:6379> zcount z1 100 400
(integer) 4
8. 获取元素的排名
从小到大:ZRANK key member
从大到小:ZREVRANK key member
127.0.0.1:6379> zrank z1 apple
(integer) 0
127.0.0.1:6379> zrank z1 peach
(integer) 3
127.0.0.1:6379> zrevrank z1 peach
(integer) 0
127.0.0.1:6379> zrevrank z1 apple
(integer) 3
9. 按照排名范围内删除元素
ZREMRANGEBYRANK key start stop
127.0.0.1:6379> zremrangebyrank z1 0 -1
(integer) 4
127.0.0.1:6379> zcard z1
(integer) 0
127.0.0.1:6379> zrange z1 0 -1
(empty list or set)
10. 应用场景
10.1 商品销量排名
定义商品销量排行榜(sorted set集合),key为goods:sellsort,分数为商品销售数量。
商品编号1001销量9,1002销量12,1003销量15…
zadd goods:sellsort 9 1001 12 1002 15 1003 18 1004 21 1005 16 1006
有一个客户又买了5件商品1001:zincrby goods:sellsort 5 1001
商品销量由低到高销量前10名:zrange goods:sellsort 0 10 withscores
商品销量由高到低销量前10名:zrevrange goods:sellsort 0 10 withscores
127.0.0.1:6379> zadd goods:sellsort 9 1001 12 1002 15 1003 18 1004 21 1005 16 1006
(integer) 6
127.0.0.1:6379> zincrby goods:sellsort 5 1001
"14"
127.0.0.1:6379> zrange goods:sellsort 0 10 withscores1) "1002"2) "12"3) "1001"4) "14"5) "1003"6) "15"7) "1006"8) "16"9) "1004"
10) "18"
11) "1005"
12) "21"
127.0.0.1:6379> zrevrange goods:sellsort 0 10 withscores1) "1005"2) "21"3) "1004"4) "18"5) "1006"6) "16"7) "1003"8) "15"9) "1001"
10) "14"
11) "1002"
12) "12"
127.0.0.1:6379> zrevrange goods:sellsort 0 3 withscores
1) "1005"
2) "21"
3) "1004"
4) "18"
5) "1006"
6) "16"
7) "1003"
8) "15"
10.2 抖音热搜
点击视频一次访问次数加1:zincrby hotsearch:20230927 1 movie1
造数据movie1 11次,movie2 10次,movie3 18次
对访问次数又高到底显示前10名:zrevrange hotsearch:20230927 0 9 withscores
127.0.0.1:6379> zincrby hotsearch:20230927 1 movie1
"1"
127.0.0.1:6379> zincrby hotsearch:20230927 10 movie1
"11"
127.0.0.1:6379> zincrby hotsearch:20230927 10 movie2
"10"
127.0.0.1:6379> zincrby hotsearch:20230927 18 movie3
"18"
127.0.0.1:6379> zrevrange hotsearch:20230927 0 9 withscores
1) "movie3"
2) "18"
3) "movie1"
4) "11"
5) "movie2"
6) "10"