当前位置:网站首页>Redis learning notes - data type: ordered set (Zset)
Redis learning notes - data type: ordered set (Zset)
2022-06-23 09:10:00 【Love Guoba】
Ordered sets are relative to hashes 、 list 、 It will be a little strange for the collection , But since it's called ordered set , So it must have something to do with the set , It preserves the property that a set cannot have duplicate members , But the difference is , Elements in an ordered set can be ordered . But it's different from a list that uses index subscripts as sort criteria , It sets a score for each element (score) Sort by .
Operations within a set
1. Add members
zadd key [nx|xx] [ch] [incr] score member [score member ...]
- nx:member Must not exist , Can be set successfully , Used to add
- xx:member There must be , Can be set successfully , Used to update the
- ch: Return after this operation , The number of elements and fractions of an ordered set that have changed
- incr: Yes score Do increase , Equivalent to the following zincrby
- Ordered sets provide sort fields compared to sets , But there are costs ,zadd The time complexity of is O(log(n)),sadd The time complexity of is O(1).
add to key by “user:ranking” Set
127.0.0.1:6379> zadd user:ranking 150 Tom
(integer) 1
127.0.0.1:6379> zadd user:ranking 1 Charlie 14 Day 66 Ling 124 Zhang
(integer) 4
2. Count the number of members
zcard key
Calculation key by “user:ranking” Number of set members of
127.0.0.1:6379> zcard user:ranking
(integer) 5
3. Calculate the score of a member
zscore key member
Calculation key by “user:ranking” Collection members of “Charlie” The scores of
127.0.0.1:6379> zscore user:ranking Charlie
"1"
4. Calculate the ranking of members
From low to high
zrank key member
From high to low
zrevrank key member
Calculation key by “user:ranking” The ranking of the collection members of
127.0.0.1:6379> zrank user:ranking Day
(integer) 1
127.0.0.1:6379> zrank user:ranking Charlie
(integer) 0
127.0.0.1:6379> zrank user:ranking Zhang
(integer) 3
5. Delete members
zrem key member [member ...]
Delete key by “user:ranking” Collection members of “Zhang”
127.0.0.1:6379> zrem user:ranking Zhang
(integer) 1
6. Increase the score of members
zincrby key increment member
increase key by “user:ranking” Collection members of “Ling”5 branch
127.0.0.1:6379> zincrby user:ranking 5 Ling
"71"
7. Returns the member of the specified ranking range
From low to high
zrange key start end [withscores]
From high to low
zrevrange key start end [withscores]
return key by “user:ranking” The set of specifies the range and score
127.0.0.1:6379> zrange user:ranking 0 -1
1) "Charlie"
2) "Day"
3) "Ling"
4) "Tom"
127.0.0.1:6379> zrange user:ranking 0 -1 withscores
1) "Charlie"
2) "1"
3) "Day"
4) "14"
5) "Ling"
6) "71"
7) "Tom"
8) "150"
8. Returns the member of the specified score range
From low to high
zrangebyscore key min max [withscores] [limit offset count]
From high to low
zrevrangebyscore key max min [withscores] [limit offset count]
return key by “user:ranking” The set of specifies the members of the score range
127.0.0.1:6379> zrangebyscore user:ranking 10 80 withscores
1) "Day"
2) "14"
3) "Ling"
4) "71"
127.0.0.1:6379> zrevrangebyscore user:ranking 80 10 withscores
1) "Ling"
2) "71"
3) "Day"
4) "14"
min and max Default closed interval , Support open range ( parentheses ),-inf and +inf They represent infinitesimally small and infinitesimally large
127.0.0.1:6379> zrangebyscore user:ranking 10 +inf withscores
1) "Day"
2) "14"
3) "Ling"
4) "71"
5) "Tom"
6) "150"
127.0.0.1:6379> zrevrangebyscore user:ranking 80 -inf withscores
1) "Ling"
2) "71"
3) "Day"
4) "14"
5) "Charlie"
6) "1"
127.0.0.1:6379> zrevrangebyscore user:ranking 71 -inf withscores
1) "Ling"
2) "71"
3) "Day"
4) "14"
5) "Charlie"
6) "1"
127.0.0.1:6379> zrevrangebyscore user:ranking (71 -inf withscores
1) "Day"
2) "14"
3) "Charlie"
4) "1"
9. Returns the number of members in the specified score range
zcount key min max
return key by “user:ranking” Specifies the number of members of the score range
127.0.0.1:6379> zcount user:ranking 10 80
(integer) 2
10. Deletes the ascending element in the specified ranking
zremrangebyrank key start stop
11. Deletes the member of the specified score range
zremrangebyscore key min max
Operations between sets
Add test data
127.0.0.1:6379> zadd user:ranking:1 1 Carlie 15 Day 67 Lin 124 Zhang
(integer) 4
127.0.0.1:6379> zadd user:ranking:2 5 Tom 35 Jerry 564 Zhang
(integer) 3
1. intersection
zinterstore destination numkeys key [key ...] [weights weight [weight ...]] [aggregate sum|min|max]
- destination: The intersection calculation is saved to this key
- numkeys: You have to do the intersection to calculate the number of keys
- key[key…]: The key that needs to do the intersection computation
- weights weight[weight…]: The weight of each key , When you do the intersection calculation , Each of these keys member I'm going to multiply my score by this weight , The weight of each key is by default 1
- aggregate sum|min|max: After the member intersection is calculated , The score can be calculated by sum( and )、min( minimum value )、max( Maximum ) Make a summary , The default value is sum
The following operation is right user:ranking:1 and user:ranking:2 Do intersection ,weights and aggregate Default configuration used , You can see the target key user:ranking:1_inter_2 The score was sum operation
127.0.0.1:6379> zinterstore user:ranking:1_inter_2 2 user:ranking:1 user:ranking:2
(integer) 1
127.0.0.1:6379> zrange user:ranking:1_inter_2 0 -1 withscores
1) "Zhang"
2) "688"
2. Combine
zunionstore destination numkeys key [key ...] [weights weight [weight ...]] [aggregate sum|min|max]
Generate key by “user:ranking:1_union_2” Union
127.0.0.1:6379> zunionstore user:ranking:1_union_2 2 user:ranking:1 user:ranking:2
(integer) 6
127.0.0.1:6379> zrange user:ranking:1_union_2 0 -1 withscores
1) "Carlie"
2) "1"
3) "Tom"
4) "5"
5) "Day"
6) "15"
7) "Jerry"
8) "35"
9) "Lin"
10) "67"
11) "Zhang"
12) "688"
Internal encoding
- ziplist( Compressed list ): When the number of elements in an ordered set is less than zset-max-ziplist-entries To configure ( Default 128 individual ), At the same time, the value of each element is less than zset-max-ziplist-value To configure ( Default 64 byte ) when ,Redis Will use ziplist As an internal implementation of an ordered set ,ziplist Can effectively reduce the use of memory
- skiplist( Skip list ): When ziplist When the conditions are not met , Ordered sets will use skiplist As an internal implementation , Because at this time ziplist The efficiency of reading and writing will decrease .
Use scenarios
Ranking system
- To add user likes, you can use zadd and zincrby function
- Cancel user likes , Users cheat or log out , It can be used zrem
- Show leaderboard use zrevrange command
- To display user information or scores, you can use zscore and zrank Two functions
边栏推荐
- S5P4418裸机编程的实现(替换2ndboot)
- Linux MySQL installation
- Community article | mosn building subset optimization ideas sharing
- 670. Maximum Swap
- 125. Valid Palindrome
- 位绑定
- MySQL fault case | error 1071 (42000): specified key was too long
- Node request module cookie usage
- 披萨订购设计----简单工厂模式
- Leetcode topic analysis count primes
猜你喜欢
随机推荐
Community article | mosn building subset optimization ideas sharing
在小程序中实现视频通话及互动直播的一种方法
Tencent cloud arm server evaluation practice
Kotlin Series 1: getting started with basics
How postman does interface testing 1: how to import swagger interface documents
How to use matrix analysis to build your thinking scaffold in flowus, notation and other note taking software
Custom tag - JSP tag Foundation
Isomorphic strings for leetcode topic resolution
Redis学习笔记—数据类型:集合(set)
Subsets II of leetcode topic analysis
GPIO初识
[qnx hypervisor 2.2 user manual]5.6.1 silent device during guest shutdown
65. Valid Number
MySQL fault case | error 1071 (42000): specified key was too long
[qnx hypervisor 2.2 user manual]6.2 network
一个采用直接映射方式的32KB缓存......存储器课后习题
如何在 FlowUs、Notion 等笔记软件中使用矩阵分析法建立你的思维脚手架
636. Exclusive Time of Functions
[qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册






