Redis ordered set common commands

Source: Internet
Author: User
Tags redis

Summary of Redis ordered set of commonly used commands are: Zadd,zrange,zremrangebyrank,zremrangebyscore,zcard, these are the use of relatively high frequency, there are some parameters of the description.


Zadd command

Adds one or more member elements and their score values to the ordered set key.

If a member is already an ordered set of members, then update the score value of this one and make sure that it is in the correct position by reinsert the membership element.

The score value can be an integer value or a double-precision floating-point number.
Use the Zadd command to bulk add some elements to an ordered set, followed by operations based on this set.

Vim

127.0.0.1:6379> Zadd Myzadd 1 a 2 B 3 C 4 D 5 e 6 F 7 D 8 E 9 F Ten 111cn.net
(integer) 7


Ordered set of the most special place lies in score, with a score can do a lot of things, this score not only can be understood as a score, sometimes also save time stamp, or the shape of the IP, can be flexibly used.

Zrange command

Syntax: Zrange key start stop [Withscores]

The Zrange command returns the elements of a given interval in an ordered set. Start starting from 0, stop is the end subscript, if a negative number is the beginning of the countdown,-1 The first, minus 2-the second last ...

Look at the following operation at a glance.


127.0.0.1:6379> zrange Myzadd 0 1
1) "A"
2) "B"
127.0.0.1:6379> Zrange Myzadd 0 5
1) "A"
2) "B"
3) "C"
4) "D"
5) "E"
6) "F"
127.0.0.1:6379> Zrange Myzadd 1 5
1) "B"
2) "C"
3) "D"
4) "E"
5) "F"
127.0.0.1:6379> Zrange Myzadd 0-1
1) "A"
2) "B"
3) "C"
4) "D"
5) "E"
6) "F"
7) "111cn.net"

Plus withscores parameter display score:

127.0.0.1:6379> zrange Myzadd 0-1 withscores
1) "A"
2) "1"
3) "B"
4) "2"
5) "C"
6) "3"
7) "D"
8) "7"
9) "E"
10) "8"
One) "F"
12) "9"
"111cn.net"
14) "10"
Zremrangebyrank command

Syntax: Zremrangebyrank key start stop

Removes all members within a specified rank (rank) interval in an ordered set. Start starts at 0, and the stop can be a negative number, representing the upside.

127.0.0.1:6379> Zremrangebyrank Myzadd 0 2
(integer) 3
Executing this command deletes the first three element a,b,c.

Zremrangebyscore command

Syntax: Zremrangebyscore key min Max

Removes all score values between Min and Max (including members equal to Min or max) in the ordered set key.


127.0.0.1:6379> Zremrangebyscore Myzadd 7 8
(integer) 2
This removes the element d,e the score of 7 and 8.

Zremrangebyrank and Zremrangebyscore are literally well understood, are all removed elements, one according to subscript, one according to the score.

Zcard command

Syntax: Zcard key

Returns the cardinality of an ordered set.


127.0.0.1:6379> Zcard Myzadd
(integer) 2
After the previous delete element operation, there are now only 2 elements left.

Zcount command

Syntax: Zcount key min Max

The difference between the Zcount command and the Zcard command is that Zcount supports the number of elements that are returned based on the range.

Because the collection elements are almost erased, add a set of elements again:

127.0.0.1:6379> zadd New www.baidu.com 900 www.111cn.net www.yahoo.com www.xunlei.com
(integer) 5
As shown in the figure:

Newzadd

Now returns the number of members with a score between 80 and 100.


127.0.0.1:6379> zcount New 80 100
(integer) 3
Returns 3.

Inside the Redis, there is also a parameter inf,-inf that represents the first member, and +inf represents the last one.


127.0.0.1:6379> Zcount New +inf
(integer) 4
Here is a return of 4 numbers, +inf here means no limit to the maximum score.

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.