Summary of common commands for Redis ordered collection types _redis

Source: Internet
Author: User
Tags redis

An ordered set of types

Ordered set type, you should know from the name, in fact, is in the collection type added an order just. The ordered set type in the Redis, which is actually associated with a fraction for each element in the collection type, is actually ordered, and we get the collection and other operations based on the range of fractions. The elements of the collection are still not able to be the same, but the fractions can be the same.

The following lists the similarities between ordered collections and types and list types:

① both are orderly (nonsense!) )

② both can get a range of elements

The following list the differences:

The list of ① is realized by the chain table, the data near the sides is very fast, and the speed of getting the intermediate elements after too many elements is very slow; Ordered collection types are used with hash lists and jump tables (skip list), so read which part of the data is the same (time complexity is O (Logn)).

The ② list cannot simply adjust the position of an element, but an ordered set can (by changing the score).

③ ordered set than list fee memory (to store fractions, hashes, jump tables).

Ii. common commands for Redis ordered sets

Redis ordered set of commonly used commands are:,,,, zadd zrange zremrangebyrank zremrangebyscore zcard etc., these are used relatively high frequency, there are some parameters of the description. Let's take a look at the next.

1, Zadd command

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

If one member is already a member of an ordered set, then update the member score value and member Make sure that it is in the correct position by inserting the element again member .

score The value can be an integer value or a double-precision floating-point number.

Use the zadd command to add some elements to the ordered collection first, followed by the operation based on this collection.

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 tanteng.me
(integer) 7

As shown in the figure:

Ordered set of the most special place is score that there is a score can do a lot of things, this score not only can be understood as a score, and sometimes the time stamp, or the shape of the IP, can be flexible to use.

2, Zrange command

Grammar:ZRANGE key start stop [WITHSCORES]

zrangecommand is used to return 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" c10/>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 " c25/>5) "E"
6) "F"
7) "Tanteng.me"

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" C36/>7) "D"
 8) "7"
 9) "E" "8" one
) "F" "
9" "
tanteng.me
" 14 "10"

3, Zremrangebyrank command

Grammar: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.

4, Zremrangebyscore command

Grammar:ZREMRANGEBYSCORE key min max

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

127.0.0.1:6379> Zremrangebyscore Myzadd 7 8
(integer) 2

This removes the element d,e the score of 7 and 8.

ZREMRANGEBYRANKand ZREMRANGEBYSCORE literally very well understood, are all removed elements, one according to subscript, one according to the score.

5, Zcard command

Grammar: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.

6, Zcount command

Grammar:ZCOUNT key min max

ZCOUNTZCARDthe difference between commands and commands is that it ZCOUNT supports the number of elements 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.tanteng.me
www.yahoo.com www.xunlei.com (integer) 5

As shown in the figure:

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

127.0.0.1:6379> zcount new
(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.

Summarize

The above is about Redis ordered collection type commonly used in all the contents of the command, the article summarized or very detailed, I hope to learn or work for everyone to bring some convenient, if you have questions can also message exchange, thank you for your support cloud habitat community.

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.