Redis Series-Storage chapter sorted set main Operation function Summary

Source: Internet
Author: User

Redis supports an ordered set, that is, sorted set. Sorted set on the basis of set, added the sorting property, is the set of the upgraded version. Here's a brief discussion of the common functions of sorted set:

1) Insert

A) Zadd

Syntax: Zadd key score member [[Score member] [SCORE member] ...]

Explanation: Add one or more member "sort by score" to the ordered set key, and only update score if member already exists. Returns an increase in the number of member and does not contain an already existing member

[root@xsf001 ~]# redis-cli 
redis 127.0.0.1:6379> zadd score 230 zhangsan
(integer) 1
redis 127.0.0.1:6379> Zadd score Lisi Wangwu
(integer) 2 redis 127.0.0.1:6379> zadd score-Liuli
re Dis 127.0.0.1:6379> zadd score 249 Wangwu
(integer) 0

Note: Some Redis versions can only add one member at a time


2) Select

A) Zrange

Syntax: Zrange key start stop [Withscores]

Explanation: Returns the member[with the specified range "Index start Stop" in the sequel Key and its score];

Redis 127.0.0.1:6379> zrange score 0-1  #所有元素
1) "Liuli"
2) "Zhangsan"
3) "Lisi"
4) "Wangwu"
Redis 127.0.0.1:6379> zrange score 0-1 withscores  #所有member及score
1) "Liuli"
2) "
3" Zhangsan "
4" 230 "
5" "Lisi"
6) "7", "Wangwu"
8) "249" Redis
127.0.0.1:6379> Zrange score 0 2 withscores  #前三个元素
1) "Liuli"
2) "M"
3) "Zhangsan"
4) "230"
5) "Lisi" c22/>6) "Redis"
127.0.0.1:6379> zrange score 0-2  #第一个元素到
1) "Liuli"
2) "Zhangsan"
3) " Lisi

Note: 0, represents the first element,-1 represents the last element,-2 represents the penultimate element

b) Zcount

Syntax: Zcount key min Max

Explanation: The number of score values between Min and Max in the statistics key

Redis 127.0.0.1:6379> zrange score 0-1 withscores
1) "Liuli"
2) "3"
"Zhangsan"
4) "230"
5) " Lisi "
6" "7" "
Wangwu"
8) "249" Redis 127.0.0.1:6379> zcount score 230  # count (Score > =230 && score <=240)
(integer) 2
c) Zscore

Syntax: Zscore key Member

Explanation: Returns the score with the member of the sequel key

Redis 127.0.0.1:6379> zscore score liuli
"100"

D) zrevrange

Syntax: Zrevrange key start stop [Withscores]

Explanation: Returns member[and score with the specified range in the sequel key [via index start stop], returning member according to score in descending order

Redis 127.0.0.1:6379> zrevrange score 0-1 withscores
1) "Wangwu"
2) "249"
3) "Lisi"
4) "
5" Zhangsan "
6" 230 "
7" "Liuli"
8) "MB" Redis 127.0.0.1:6379> zrevrange score 1-2
1) "Lisi"
2) "Zhangsan"

Note: Index 0 represents the first element,-1 last element,-2 the penultimate element;

e) Zrangebyscore

Syntax: Zrangebyscore key min Max [withscores] [limit offset count]

Explanation: Returns a sequel key, score is greater than Min and is less than or equal to Max's member. Returns the results in the order of score increments. Optional Withscores Determines whether the return result set returns only member or member and score; optional parameter limit specifies the range of numbers to return the result.

Redis 127.0.0.1:6379> Zrangebyscore score 10000 withscores
1) "Liuli"
2) "3" "
Zhangsan"
4) "230"
5) "Lisi"
6 "" 7 "
" Wangwu "
8)" 249 "
Redis 127.0.0.1:6379> zrangebyscore Score 10000  Limit 1 3
1) "Zhangsan"
2) "Lisi"
3) "Wangwu"

f) Zrevrangebyscore

Syntax: Zrevrangescore key Max min [withscores] [limit offset count]

Explanation: Returns the elements that have the Score<=max and Score>=min in the sequel key, and returns the results according to score from large to small order. Optional parameter withscores determines whether the result set contains score, optional parameter limit specifies the range of result sets to be returned.

Redis 127.0.0.1:6379> zrevrangebyscore score 0 10000 empty
(Redis list or set) 127.0.0.1:6379>
Zrevrangebyscore Score 10000 0
1) "Wangwu"
2) "Lisi"
3) "Zhangsan"
4) "Liuli"
Redis 127.0.0.1:6379 > Zrevrangebyscore score 10000 0 withscores limit 0, 2
1) "Wangwu"
2) "249"
3) "Lisi" 4
) "240"

Note: Max before min

g) Zrank

Syntax: Zrank key Member

Explanation: According to score from low to high, returns member in the index of a renewed set

Redis 127.0.0.1:6379> zrange score 0-1
1) "Liuli"
2) "Zhangsan"
3) "Lisi"
4) "Wangwu"
Redis 127.0.0.1:6379> Zrank score Liuli
(integer) 0
redis 127.0.0.1:6379> zrank score Wangwu
(integer) 3

h) Zrevrank

Syntax: Zrevrank key Member

Explanation: Returns the index of member in the ordered set key according to the score sort from high to low

Redis 127.0.0.1:6379> zrange score 0-1
1) "Liuli"
2) "Zhangsan"
3) "Lisi"
4) "Wangwu"
Redis 127.0.0.1:6379> Zrevrank score Liuli
(integer) 3 redis 127.0.0.1:6379> Zrevrank score Wangwu
( Integer) 0
i) Zcard

Syntax: Zcard key

Explanation: Returns the cardinality of a sequel key

Redis 127.0.0.1:6379> zcard score
(integer) 4
redis 127.0.0.1:6379> zcard stdu  #有续集不存在返回0
( Integer) 0

3) Update

A) Zincrby

Syntax: Zincrby key Increment member

Explanation: The member with the sequel key increases the increment increment, returning the added score

Redis 127.0.0.1:6379> zscore score Liuli "Redis 127.0.0.1:6379> Zincrby Score-liuli
" 400 " Redis 127.0.0.1:6379> zscore score Liuli "Redis 127.0.0.1:6379>-Zincrby score-Xie
" 500 "

Note: If member does not exist in key, a new member is added

4) Delete

A) Zrem

Syntax: Zrem key member [member ...]

Explanation: Removes one or more member (member) from a renewed set, returning the number of member removed

Redis 127.0.0.1:6379> zrange score 0-1
1) "Zhangsan"
2) "Lisi"
3) "Wangwu"
4) "Liuli"
5) "Xie" C5/>redis 127.0.0.1:6379> Zrem score Xie wwww  #www不存在
(integer) 1 redis 127.0.0.1:6379> Zrange score
0 -1
1) "Zhangsan"
2 "Lisi"
3) "Wangwu"
4) "Liuli"

Note: If member does not exist, ignore

b) Zremrangebyrank

Syntax: Zremrangebyrank key start stop

Explanation: Removes the element that has the specified rank "start stop" in the continuation set, and returns the number of removed elements

Redis 127.0.0.1:6379> zrange score 0-1
1) "Zhangsan"
2) "Lisi"
3) "Wangwu"
4) "Liuli
" Redis 127.0.0.1:6379> Zremrangebyrank score 0 1
(integer) 2
redis
127.0.0.1:6379> zrange Score 1) "Wangwu"
2) "Liuli"

Note: If the stop is before start, remove the 0

c) Zremrangebyscore

Syntax: Zremrangebyscore key min Max

Explanation: Remove member with a continuation set, remove member score greater than or equal to min less than or Max; return number of removed elements

Redis 127.0.0.1:6379> zrange score 0-1 withscores
1) "Wangwu"
2) "249"
3) "Liuli"
4) "
Redis" 127.0.0.1:6379> Zremrangebyscore score 248
(integer) 1
redis 127.0.0.1:6379> zrange score 0-1 ES
1) "Liuli"
2) "400"

5) Other

A) Zinterstore

Syntax: Zinterstore destination Numkeys Key[key ...] [Weights weight] [Aggregate Sum|min|max]

Explanation: The intersection of several sequels is counted, and the number of the sequel keys must be specified with the Numkeys parameter and the statistic results stored to destination. By default, the score of elements in destination is the sum of the score of the elements in the sequel key. Use weights to specify a multiplication factor for each sequel, and each score with a sequel is multiplied by the multiplication factor before passing it to the aggregate function (aggregate). If you do not specify a multiplication factor weight, the default is 1; Use the aggregate option to specify how the intersection is aggregated. Min, min score,max, max Score,sum, sum.

Redis 127.0.0.1:6379> zrange score 0-1 withscores 1) "Lisi" 2) "3)" Liuli "4)" Redis 127.0.0.1:6379> Zrang E score1 0-1 withscores 1) "Wangwu" 2) "3" "Liuli" 4) "Redis 127.0.0.1:6379> zinterstore dest 2 score Score1 # Intersection sum sum (score.member.score * 1 + score1.member.score * 1) (integer) 1 redis 127.0.0.1:6379> zrange dest 0-1 withscore S 1) "Liuli" 2) "430" Redis 127.0.0.1:6379> zinterstore dest 2 score score1 weights 1 2 #交集求和, SUM (Score.member.score * 1 + score1.member.score * 2) (integer) 1 redis 127.0.0.1:6379> zrange dest 0-1 withscores 1) "Liuli" 2) "460" Redis 12 7.0.0.1:6379> Zinterstore dest 2 score score1 min #交集求最小值, min (aggregate * 1, Score.member.score * 1) (integer) 1 redis 127.0.0.1:6379> zrange dest 0-1 withscores 1) "Liuli" 2) "Redis 127.0.0.1:6379> Zinterstor E dest 2 score score1 aggregate Max #交集求最大值 max (Score.member.score * 1, Score1.member.score * 1) (integer) 1 Redis 127.0. 0.1:6379> Zrange dest 0-1 withscores 1) "Liuli" 2) "2" Redis 127.0.0.1:6379> Zinterstore-dest + score score1 aggregate sum #交集求和, SUM (SCO 
Re.member.score * 1, Score1.member.score * 1) (integer) 1 redis 127.0.0.1:6379> zrange dest 0-1 withscores 1) "Liuli" 2) "430" Redis 127.0.0.1:6379> zinterstore dest 2 score score1 weights 1 2 aggregate min #交集求最小值 min (score.member.scor
 E * 1, Score1.member.score * 2) (integer) 1 redis 127.0.0.1:6379> zrange dest 0-1 withscores 1) "Liuli" 2) "60"

b) Zunionstore

Syntax: Zunionstore destination Numkeys Key[key ...] [Weights weight] [Aggregate Sum|min|max]

Explanation: Count multiple sequels with a sequel, in which the number of sequels keys must be specified with the Numkeys parameter and the statistics are stored to destination. By default, the score of elements in destination is the sum of the score of the elements in the sequel key. Weights specify the multiplication factor, aggregate specify the aggregation method, see Zinterstore Introduction

Redis 127.0.0.1:6379> zrange score 0-1 withscores 1) "Lisi" 2) "3)" Liuli "4)" Redis 127.0.0.1:6379> Zrang E score1 0-1 withscores 1) "Wangwu" 2) "3" "Liuli" 4) "Redis 127.0.0.1:6379> zunionstore dest 2 score Score1 # Intersection sum sum (score.member.score * 1, Score1.member.score * 1) (integer) 3 redis 127.0.0.1:6379> zrange dest 0-1 withscore S 1) "Lisi" 2) "3" "Wangwu" 4) "5" "Liuli" 6) "430" Redis 127.0.0.1:6379> zunionstore dest 2 score Score1 Weig HTS 1 2 aggregate sum #交集求和 sum (score.member.score * 1, Score1.member.score * 2) (integer) 3 Redis 127.0.0.1:6379> Zra Nge dest 0-1 withscores 1) "Lisi" 2) "3" "Wangwu" 4) "(5)" Liuli "6)" 460 "Redis 127.0.0.1:6379> zunionstore de St 2 score score1 weights 1 2 aggregate max #交集最大值 max (Score.member.score * 1, Score1.member.score * 2) (integer) 3 Redi s 127.0.0.1:6379> zrange dest 0-1 withscores 1) "Lisi" 2) "3" "Wangwu" 4) "(5)" Liuli "6)" Redis 127.0.0.1 :6379> Zunionstore deSt 2 score score1 weights 2 4 aggregate min #交集最小值 min (score.member.score * 2, Score1.member.score * 4) (integer) 3 Redi s 127.0.0.1:6379> zrange dest 0-1 withscores 1) "Lisi" 2) "(3)" Wangwu "4)" "5" "Liuli" 6) "120"

Reminder: Sorted set of all functions, please use the following way to view

[root@xsf001 ~]# redis-cli 
redis 127.0.0.1:6379> help @sorted_set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.