Redis command Detail and Usage Scenario Example--sortedset (ordered collection)

Source: Internet
Author: User
Tags redis redis version versions
Zadd key Score member [[Score member] [SCORE member] ...]

Adds one or more member elements and their score values to the ordered set key.
If a member is already a member of an ordered set, update the member score value and ensure that the member is in the correct position by re-inserting the member element.
The score value can be an integer value or a double-precision floating-point number.
If the key does not exist, an empty ordered set is created and the Zadd operation is performed.
An error is returned when key exists but is not an ordered set type.
For more information on ordered sets, see sorted set.
Prior to Redis version 2.4, Zadd can only add one element at a time.
Available Versions:
1.2.0+
complexity of Time:
O (M*log (N)), N is the cardinality of the ordered set, and M is the number of new members that were added successfully.
return value:
The number of new members that were added successfully, excluding those that were updated and that already exist.
Add a single element

redis> zadd page_rank google.com
(integer) 1

Add multiple elements

Redis> Zadd Page_rank 9 baidu.com 8 bing.com
(integer) 2
redis> zrange page_rank 0-1 withscores
1) "Bin G.com "
2)" 8 "
3)" Baidu.com "
4)" 9 "
5)" google.com "
6)" 10 "

Add an existing element, and the score value does not change

redis> zadd page_rank google.com
(integer) 0
redis> zrange page_rank 0-1 withscores  # no change
1) "B Ing.com "
2)" 8 "
3)" Baidu.com "
4)" 9 "
5)" google.com "
6)" 10 "

Add an existing element, but change the score value

Redis> zadd Page_rank 6 bing.com
(integer) 0
redis> zrange page_rank 0-1 withscores  # bing.com Element SC Ore value changed
1) "Bing.com"
2) "6"
3) "Baidu.com"
4) "9"
5) "google.com"
6) "10"
Zcard Key

Returns the cardinality of the ordered set key.
Available Versions:
1.2.0+
complexity of Time:
O (1)
return Value:
Returns the cardinality of an ordered set when key exists and is an ordered set type.
Returns 0 when key does not exist.

Redis > Zadd Salary # Tom    # Add a member
(integer) 1
redis > Zcard salary
(integer) 1
redis > ZA DD salary to   add a member
(integer) 1
redis > Zcard salary
(integer) 2
redis > EXISTS non_ex Ists_key   # Zcard operation on nonexistent key
(integer) 0
redis > Zcard non_exists_key
(integer) 0
zcount Key min Max

Returns the number of members in an ordered set key, where the score value is between Min and Max (the default includes the score value equals min or max).
Refer to the Zrangebyscore command for detailed instructions on how to use the parameter min and Max.
Available Versions:
2.0.0+
complexity of Time:
O (log (N) +m), N is the cardinality of the ordered set, and M is the number of elements between Min and Max.
return Value:
The number of members of the score value between Min and Max.

redis> zrange Salary 0-1 withscores    # test Data
1) "Jack"
2) "3" "
Peter"
4) "3500"
5) "Tom" c6/>6) "
redis> zcount salary          2000-5000" number of people
(integer) 3
redis> Zcount sal ary          # Number of people earning between 3000-5000
(integer) 2
Zincrby Key Increment member

Adds an increment increment to the score value of the member member of the ordered set key.
You can pass a negative value increment, let score subtract the corresponding value, such as Zincrby Key-5 member, is to let member score value minus 5.
When key does not exist, or member is not a member of key, Zincrby key increment member is equivalent to Zadd key increment member.
When key is not an ordered set type, an error is returned.
The score value can be an integer value or a double-precision floating-point number.
Available Versions:
1.2.0+
complexity of Time:
O (log (N))
return Value:
The new score value of the member member, expressed as a string.

Redis> Zscore salary Tom
"" "
redis> Zincrby salary Tom   # Tom raises.
"4000"
zrange key start stop [Withscores]

Returns the member of the ordered set key, within the specified interval.
The positions of the members are sorted by increasing the score value (from small to large).
Members with the same score value are sorted by dictionary order (lexicographical order).
Use the Zrevrange command if you want members to be arranged in descending order of score values (from large to small).
The subscript parameter start and stop are based on 0, that is, 0 indicates the first member of an ordered set, 1 indicates the second member of an ordered set, and so on.
You can also use a negative subscript, which means 1 for the last member, 2 for the penultimate member, and so on.
An out-of-range subscript does not cause an error.
For example, when the value of start is larger than the maximum subscript for an ordered set, or Start > Stop, the Zrange command simply returns an empty list.
On the other hand, if the value of the stop parameter is larger than the maximum subscript for an ordered set, Redis treats stop as the maximum subscript.
You can return the member and its score value by using the Withscores option, which returns the format representation of the list in Value1,score1, ..., Valuen,scoren.
The client library may return some more complex data types, such as arrays, tuples, and so on.
Available Versions:
1.2.0+
complexity of Time:
O (log (N) +m), N is the cardinality of the ordered set, and M is the cardinality of the result set.
return Value:
A list of ordered set members with the score value (optional) within the specified interval.

Redis > Zrange Salary 0-1 withscores             # Show entire ordered set members
1) "Jack"
2) "3500"
3) "Tom"
4) "5" "
bo SS "
6)" 10086 "
redis > Zrange Salary 1 2 withscores              # Display ordered set subscript interval 1 to 2 members
1)" Tom "
2)"
the " 3) "Boss"
4) "10086"
redis > Zrange salary 0 200000 withscores         # test End subscript When the maximum subscript is exceeded
1) "Jack" 
  
   2) "3500"
3) "Tom"
4) "5"
"Boss"
6) "10086"
redis > Zrange salary 200000 3000000 withs Cores   # test Case when a given interval does not exist in an ordered set
(empty list or set)
  
zrangebyscore key min Max [withscores] [LIMIT offset count]

The

Returns an ordered set of key, where all score values are between Min and Max (including equals min or Max) members. Ordered set members are arranged in ascending order of score values (from small to large). The
members with the same score value are arranged in dictionary order (lexicographical Order) (this property is provided by an ordered set and does not require additional calculations). The
Optional Limit parameter specifies the number and interval of results returned (like SELECT LIMIT offset, count in SQL), and note that when offset is large, the operation that locates offset may need to traverse the entire ordered set, which is the worst-case complexity of O (N) Time. The
Optional withscores parameter determines whether the result set returns only the members of an ordered set, or the ordered set members and their score values.
This option is available from the Redis 2.0 release.
Interval and infinity
min and Max can be-inf and +inf, so you can use commands such as Zrangebyscore without knowing the lowest and highest score values of the ordered set.
By default, interval values use a closed interval (less than or equal to or greater than or equal), and you can also use an optional open interval (less than or greater than) by adding to the parameter before it.
For example:
Zrangebyscore zset (1 5
Returns all members that meet the criteria 1 < score <= 5, and
Zrangebyscore Zset (5 (Ten
) returns all Eligible 5 < Score < 10 members.
Available Versions:
1.0.5+
time complexity:
O (log (N) +m), N is the cardinality of an ordered set, and M is the cardinality of the result set.
return value:
A list of ordered set members with an score value (optional) within the specified interval.

redis> Zadd Salary 2500 Jack                        # test Data
(integer) 0
redis> zadd salary Tom
(integer) 0
REDIS&G T Zadd Salary 12000 Peter
(integer) 0
redis> zrangebyscore salary-inf +inf               # show entire ordered set
1) "Jack"
2) " Tom "
3)" Peter "
redis> zrangebyscore salary-inf +inf withscores    # shows the entire ordered set and the member's score value
1)" Jack " c14/>2) "2500"
3) "Tom"
4) "5" "
Peter"
6) "12000"
redis> zrangebyscore Salary-inf Withscores    # Show All members of the payroll <=5000
1) "Jack"
2) "2500"
3) "Tom"
4) "
redis>" Zrangebyscore salary (400000            # shows members with wages greater than 5000 less than or equal to 400000
1) "Peter"
Zrank Key Member

Returns the rank of the member member in the ordered set key. Where ordered set members are arranged in ascending order of score values (from small to large).
The ranking is based on 0, meaning that the member with the lowest score value is ranked at 0.
Use the Zrevrank command to get the ranking of members in descending order of score values (from large to small).
Available Versions:
2.0.0+
complexity of Time:
O (log (N))
return Value:
Returns the rank of member if member is a member of the ordered set key.
If member is not a member of the ordered set key, nil is returned.

redis> zrange Salary 0-1 withscores        # Show All members and their score values
1) "Peter"
2) "3500"
3) "Tom"
4) "4000" 
  
   5) "Jack"
6) "
redis> Zrank salary Tom                     # shows Tom's salary rank, second
(integer) 1
  
Zrem Key member [member ...]

Removes one or more members from an ordered set key, and non-existent members are ignored.
An error is returned when key exists but is not an ordered set type.
Prior to Redis version 2.4, Zrem can only delete one element at a time.
Available Versions:
1.2.0+
complexity of Time:
O (M*log (N)), N is the cardinality of the ordered set, and M is the number of members that have been successfully removed.
return Value:
The number of members that were successfully removed, excluding ignored members.
Test data

redis> zrange page_rank 0-1 withscores
1) "Bing.com"
2) "8"
3) "Baidu.com"
4) "9"
5) "google.com "
6)" 10 "

Removing a single element

redis> Zrem page_rank google.com
(integer) 1
redis> zrange page_rank 0-1 withscores
1) "Bing.com"
2) "8"
3) "Baidu.com"
4) "9"

Removing multiple elements

redis> zrem page_rank baidu.com bing.com
(integer) 2
redis> zrange page_rank 0-1 withscores
(empty List or set)

Remove non-existent element

redis> zrem page_rank non-exists-element
(integer) 0
Zremrangebyrank key Start Stop

Removes the ordered set key, specifying all members within the rank (rank) interval.
The interval is indicated by the following standard parameter, start and stop, including start and stop.
The subscript parameter start and stop are based on 0, that is, 0 indicates the first member of an ordered set, 1 indicates the second member of an ordered set, and so on.
You can also use a negative subscript, which means 1 for the last member, 2 for the penultimate member, and so on.
Available Versions:
2.0.0+
complexity of Time:
O (log (N) +m), N is the cardinality of an ordered set, and M is the number of members removed.
return Value:
The number of members that were removed.

Redis> Zadd Salary $ jack
(integer) 1
redis> zadd salary Tom
(integer) 1
redis> Zadd sala RY 3500 Peter
(integer) 1
redis> zremrangebyrank salary 0 1       # Remove the Member
(integer) 1 redis in the subscript 0 to 2 interval
> Zrange Salary 0-1 withscores    # ordered set only one member left
1) "Tom"
2) "5000"
zremrangebyscore Key min Max

Remove ordered set key, where all score values are between Min and Max, including members equal to Min or max.
Starting from version 2.1.6, score values equal to Min or Max members can also be excluded, see Zrangebyscore command for details.
Available Versions:
1.2.0+
complexity of Time:
O (log (N) +m), N is the cardinality of an ordered set, and M is the number of members removed.
return Value:
The number of members that were removed.

redis> zrange Salary 0-1 withscores          # Show All members in an ordered set and their score values
1) "Tom"
2) "
3" "Peter"
4) "3500"
5) "Jack"
6) "
redis> zremrangebyscore salary 3500      # Remove all employees who are paid within 1500 to 3500
(integer) 2
  redis> zrange Salary 0-1 withscores          # remaining ordered set member
1) "Jack"
2) "5000"
zrevrange key start stop [Withscores]

Returns the member of the ordered set key, within the specified interval.
The positions of the members are arranged in descending order of score values (from large to small).
Members with the same score value are arranged in reverse order of the dictionary order (reverse lexicographical orders).
Other aspects of the Zrevrange command are the same as the Zrange command, except that the members are ordered in descending order of score values.
Available Versions:
1.2.0+
complexity of Time:
O (log (N) +m), N is the cardinality of the ordered set, and M is the cardinality of the result set.
return Value:
A list of ordered set members with the score value (optional) within the specified interval.

redis> zrange Salary 0-1 withscores        # Ascending arrangement
1) "Peter"
2) "3500"
3) "Tom"
4) "4000"
5) "Jack" c9/>6) "
redis> zrevrange salary 0-1 withscores     # Descending order
1)" Jack "
2)" 3 ""
Tom " C15/>4) "4000"
5) "Peter"
6) "3500"
Zrevrank Key Member

Returns the rank of the member member in the ordered set key. Where ordered set members are sorted by score values descending (from large to small).
The rankings are based on 0, which means that the members with the largest score value rank 0.
Use the Zrank command to get the ranking of members in increments of score values (from small to large).
Available Versions:
2.0.0+
complexity of Time:
O (log (N))
return Value:
Returns the rank of member if member is a member of the ordered set key.
If member is not a member of the ordered set key, nil is returned.

Redis 127.0.0.1:6379> zrange Salary 0-1 withscores     # test Data
1) "Jack"
2) "3" "
Peter"
4) "3500 "
5)" Tom "
6"
redis> Zrevrank salary Peter     # Peter's Salary row second
(integer) 1
redis> Zrevrank salary Tom       # Tom's highest salary
(integer) 0
Zscore Key Member

Returns the score value of the member member in the ordered set key.
Returns nil if the member element is not a member of the ordered set key, or the key does not exist.
Available Versions:
1.2.0+
complexity of Time:
O (1)
return Value:
The score value of the member member as a string representation.

redis> zrange Salary 0-1 withscores    # test Data
1) "Tom"
2) "3" "
Peter"
4) "3500"
5) "Jack" c6/>6)
redis> Zscore salary Peter              # Note The return value is the string
"3500"
zunionstore Destination Numkeys key [key ...] [WEIGHTS weight [weight ...] [AGGREGATE sum| min| MAX]

The

Computes the set of the given one or more ordered sets, where the number of the given key must be specified in the Numkeys parameter and the set (result set) is stored to destination.
By default, the score value of a member in the result set is the sum of the score values for that member under all given sets.
WEIGHTS
uses the WEIGHTS option, you can specify a multiplication factor (multiplication factor) for each given ordered set, and score values for all members of each given ordered set are passed to the aggregate function (aggregation function) is multiplied by the factor of the ordered set.
If the WEIGHTS option is not specified, the multiplication factor is set to 1 by default.
AGGREGATE
uses the AGGREGATE option, which lets you specify how the result set of the set is aggregated. The
parameter SUM, which is used by default, allows the sum of the score values of a member in all collections to be the score value of that member in the result set, and with the parameter min, you can use the minimum score value of a member in all collections as the score value for that member in the result set, and the parameter MAX is the maximum score value of a member in all collections as the score value of that member in the result set.
Available Versions:
2.0.0+
time complexity:
O (n) +o (M log (m)), N is the sum of the cardinality of the given ordered set, and M is the cardinality of the result set.
return value:
The cardinality of the result set saved to destination.

redis> zrange programmer 0-1 withscores
1) "Peter"
2) "3" "
Jack"
4) "3500"
5) "Tom"
6) "Redis>"
zrange manager 0-1 withscores
1) "Herry"
2) "5"
3) "Mary"
4) "3500"
) " Bob "
6)" 4000 "
redis> zunionstore Salary 2 programmer manager WEIGHTS 1 3   # Company decided to pay a raise ... Apart from programmers ...
(integer) 6
redis> zrange salary 0-1 withscores
1) "Peter"
2) "3" "
Jack"
4) " 3500 "
5)" Tom "
6)"
7) "Herry"
8) "6000"
9) "Mary") "
10500" "
Bob"
12) "12000"
zinterstore Destination Numkeys key [key ...] [WEIGHTS weight [weight ...] [AGGREGATE sum| min| MAX]

Computes the intersection of one or more ordered sets given, where the number of a given key must be specified with the Numkeys parameter and the intersection (result set) is stored to destination.
By default, the score value of a member in the result set is the sum of the score values for that member under all given sets.
For a description of the WEIGHTS and AGGREGATE options, see the Zunionstore command.
Available Versions:
2.0.0+
complexity of Time:
O (n*k) +o (M*log (M)), N is the smallest ordinal set of cardinality in a given key, K is the number of given ordered set, and M is the cardinality of the result set.
return Value:
The cardinality of the result set saved to destination.

Redis > Zadd mid_test "Li Lei"
(integer) 1
redis > Zadd mid_test "Han Meimei"
(integer) 1
redi s > Zadd mid_test 99.5 "Tom"
(integer) 1
redis > Zadd fin_test "Li Lei"
(integer) 1
redis > Z ADD fin_test "Han Meimei"
(integer) 1
redis > Zadd fin_test 99.5 "Tom"
(integer) 1
redis > ZI Nterstore sum_point 2 mid_test fin_test
(integer) 3
redis > Zrange sum_point 0-1 withscores     # Show all in ordered set Score Value
1) "Han Meimei"
2) "145"
3) "Li Lei"
4) "158"
5) "Tom"
6) "199"
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.