Redis Learning (ii)

Source: Internet
Author: User

Collection

Add/Remove elements

Sadd Key Member1 Member2 ...

Srem Key Member1 Member2 ...

Get all the elements in the collection

Smembers Key

127.0.0.1:6380>sadd car benz Baoma Xiandai (integer)3127.0.0.1:6380>smembers Car1)"Baoma"2)"Xiandai"3)"Benz"127.0.0.1:6380>Srem Car Benz (integer)1127.0.0.1:6380>smembers Car1)"Baoma"2)"Xiandai"127.0.0.1:6380>

Determines whether an element is in the collection, returns 1 if it exists, returns 0 if it does not exist

Sismember Key Member

127.0. 0.1:6380>1127.0. 0.1:6380>0127.0. 0.1:6380

Operations between collections

Sdiff key1 Key2 ... i.e. A-b={x|x∈a and x∈/b}

SINTER key1 Key2 ... i.e. A∩b={x|x∈a and X∈b}

Sunion key1 Key2 ... i.e. Aub={x|x∈a or x∈b}

127.0.0.1:6380> Sadd a 1 2 3 4(integer)4127.0.0.1:6380> Sadd B 3 4 5 6(integer)4127.0.0.1:6380>Sdiff a B1)"1"2)"2"127.0.0.1:6380>Sinter a B1)"3"2)"4"127.0.0.1:6380>Sunion a B1)"1"2)"2"3)"3"4)"4"5)"5"6)"6"127.0.0.1:6380>

Perform set operations and store the results

Sdiffstore destination key [key ...]
Sinterstore destination key [key ...]
Sunionstore destination key [key ...]

The only difference between the Sdiffstore command and the Sdiff command function is that the former does not return the result of the operation directly, but instead stores the result in the destination key.

The Sdiffstore command is often used in scenarios where multiple-step set operations are required, such as the need to calculate the difference first and then compute the intersection of the result and other keys.

Gets the number of elements in the collection:

SCard Key

Randomly get the elements in the collection

Srandmember key [Count]

(1) When Count is a positive number, srandmember randomly obtains count non-repeating elements from the collection. If the value of count is greater than the number of elements in the collection, Srandmember returns all the elements in the collection. (2) When count is a negative number, srandmember randomly obtains |count| elements from the collection, which are likely to be the same.

" 4 " " 2 "127.0.0.1:6380> srandmember a"3"

POPs an element from the collection

SPOP KEY This random pop-up

127.0.0.1:6380>Smembers a1)"1"2)"2"3)"3"4)"4"127.0.0.1:6380>Spop a"1"127.0.0.1:6380>Spop a"3"127.0.0.1:6380>Smembers a1)"2"2)"4"127.0.0.1:6380>

Ordered collection

An ordered collection type is somewhat similar in some ways to a list type.
(1) both are orderly.
(2) Both can obtain a certain range of elements.
But there is a big difference between the two, which makes their application scenarios different.
(1) List types are implemented through linked lists, obtaining data near both ends is extremely fast, and when the elements increase, access to intermediate data is slower, so it is more suitable to implement such as "new" or "log" so that rarely access intermediate elements
Application.
(2) An ordered collection type is implemented using a hash table and a skip list, so even reading the data in the middle section is fast (the time complexity is O (log (N))).
(3) The position of an element cannot be easily adjusted in the list, but an ordered set can be (by changing the fraction of this element).
(4) Ordered collections are more memory-intensive than list types. The Ordered collection type is considered to be the most advanced type of Redis's 5 data types, and can be understood in comparison with list types and collection types when learning.

add element

Zadd key Scord member [Scord member ...]

Modify the score of an element

Zadd Key Scord Member

Get the fraction of an element

Zscore Key Member

127.0.0.1:6380> Zadd Cheeron, Math, English,3127.0.0.1:6380> zadd cheeron中文版 (integer) 0
    127.0.0.1:6380> zscore cheeron math"all"127.0.0.1:6380> Zscore Cheeron 中文版

Get a list of elements ranked in a range

Zrange key start stop [Withscores]

Zrevrange key start stop [Withscores]

The Zrange command returns all elements (containing elements at both ends) of an index from start to stop, in order of element fractions from small to large. The Zrange command is very similar to the Lrange command, such as that the index is starting at 0, and a negative number represents a forward lookup (1 representing the last element).

" 中文版 " " Chinese " " Math " " Chinese " " Math "127.0.0.1:6380> zrange cheeron 2or set)

You can add the Withscores parameter at the end of the Zrange command if you need to get the fraction of the element at the same time

127.0.0.1:6380> zrange cheeron 0-1 withscores"中文版"" " " Chinese " "  the " " Math " "  - "

Gets the element with the specified fraction range

Zrangebyscore key min Max [withscores] [LIMIT offset Count]
Zrangebyscore command parameters are many, but they are well understood. This command returns the elements between Min and Max (including Min and Max) in the order of element fractions from small to large:

Added "(" means not included.

" 中文版 " " Chinese " " Math " " 中文版 " " Chinese "

Redis Learning (ii)

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.