Five common data structures in redis-ZSet ordered collection scenario

 

 

 

 

 

 

Hot search

 

 

 



#tom第一天获得5个贴纸
> zadd day1 5 tom
(integer) 1
#jacy第一天获得2个贴纸
> zadd day1 2 jack
(integer) 1
#merry第一天获得8个贴纸
> zadd day1 8 merry
(integer) 1
#jacy第一天又多增加获得1个贴纸
> zincrby day1 1 jack
"3"
#查看jack的排名
> zrank day1 jack
(integer) 0
#查看merry的排名
> zrank day1 merry
(integer) 2
#查看所有人的排名
> zrange day1 0 -1
1) "jack"
2) "tom"
3) "merry"
#查看获得5到9个之间的小朋友的名单
> zrangebyscore day1 5 9
1) "tom"
2) "merry"
#查看day1总共有多少个人
> zcard day1
(integer) 3
#jack第二天又获得了3个贴纸
> zadd day2 3 jack
(integer) 1
#统计day1,day2两天总共获得的贴纸排名,并且把结果放到union:day1andday2这个key里面
> zunionstore union:day1andday2 2 day1 day2 
(integer) 3
#统计排名并显示他们获得贴纸的个数
> zrangebyscore union:day1andday2 0 1000 withscores
1) "tom"
2) "5"
3) "jack"
4) "6"
5) "merry"
6) "8"

 

 

 

Other usage scenarios:
1: Statistics of daily activity
2: Statistics of the income of the last 24 hours, etc.
3: Reward daily list
4: Climbing list
5: There is also a special application that can set the score as the expiration time and customize it according to socre Clean up something

Implementation: The
sorted set internally uses HashMap and SkipList to ensure the storage and order of the data. The HashMap puts the mapping from members to the score, and the skip table stores all members, and the sorting basis is HashMap The stored score, using the structure of the jump table can obtain a relatively high search efficiency, and the implementation is relatively simple.

zset (sorted set) operation command

zadd(key, score, member): Add the element member to the zset named key, and score is used for sorting. If the element already exists, the order of the element is updated according to the score.

zrem(key, member): delete the element member in the zset named key

zincrby(key, increment, member): If the element member already exists in the zset named key, the score of the element is increased by increment; otherwise, the element is added to the set, and the score value is increment

zrank(key, member): Returns the rank (ie index, starting from 0) of the member element in the zset named key (the elements are sorted by score from small to large). If there is no member element, return "nil"

Zcard statistics

zrevrank(key, member): Returns the rank (ie index, starting from 0) of the member element in the zset named key (the elements are sorted by score from largest to smallest). If there is no member element, return "nil"

zrange(key, start, end): Returns all elements whose index is from start to end in the zset named key (the elements have been sorted by score from small to large)

zrevrange(key, start, end): Returns all elements whose index is from start to end in the zset named key (the elements have been sorted by score from large to small)

zrangebyscore(key, min, max): Returns all elements in the zset named key whose score >= min and score <= max zcard(key): returns the cardinality of the zset named key zscore(key, element): returns the name The score of the element element in the key zset zremrangebyrank(key, min, max): delete all elements whose rank is >= min and rank <= max in the zset whose name is key zremrangebyscore(key, min, max): delete all elements whose name is key All elements in the zset with score >= min and score <= max

zunionstore / zinterstore(dstkeyN, key1,…,keyN, WEIGHTS w1,…wN, AGGREGATE SUM|MIN|MAX): Find the union and intersection of N zsets, and save the final set in dstkeyN. For the score of each element in the set, it must be multiplied by the WEIGHT parameter before performing the AGGREGATE operation. If WEIGHT is not provided, it defaults to 1. The default AGGREGATE is SUM, that is, the score of the element in the result set is the value of the SUM operation of all the corresponding elements in the set, and MIN and MAX mean that the score of the element in the result set is the minimum and maximum of all the corresponding elements in the set.

Guess you like

Origin blog.csdn.net/liuming690452074/article/details/113803525