Common commands for Redis collection types Summary _redis

Source: Internet
Author: User
Tags redis tag name

Introduction to collection types

The collection type is also a type that embodies redis a higher value. Because of the Redis collection type, we can easily perform difference set operations, intersection operations, and set operations in Redis.

First we introduce the difference between collection type and list type, in fact, students who have studied object-oriented language should all be able to guess what these types are different.

① collection type and list type can still store 2^32-1 strings

The ② collection type is unordered and the list type is ordered

The ③ collection type is unique, and the value of the list type is not unique

Let's look at the syntax together.

1, add the Delete element command

Sadd key member [member ...]
Srem Key member [member ...]

saddCommands are used to add elements to a collection, and of course, based on previous learning experience, if the collection does not exist, it is definitely created automatically. But here's a little bit of note that if an element already exists, then he ignores it and does not overwrite it. The return value is the number of elements that were successfully added (ignored elements are not counted).

sremThe same is true for commands, delete elements, and if the element exists to delete the success, the return value is the number of elements that were successfully deleted.

2, get all the elements in the collection

Smembers Key

3, determine whether the element is in the collection

Sismember Key Member

The time complexity of this judgment operation is O (1), and the command always returns results very quickly, regardless of the number of elements in the collection. There is a return of 1, when there is no or no this key is returned 0.

4. Operation between sets

Sdiff key [key ...]
Sinter key [key ...]
Sunion key [Key ...]

These three orders can make Redis!

Let's prepare some test data first.

127.0.0.1:6379> sadd SetA 1 2 3
(integer) 3
127.0.0.1:6379> sadd SETB 2 3 4
(integer) 3
127.0.0.1:6 379> Sadd setc 3 4 5
(integer) 3
127.0.0.1:6379> smembers SetA 
1) "1"
2) "2"
3) "3"
127.0.0.1:6379>

(1) Let's introduce the sdiff command, which is actually a difference set operation.

The difference operation of set A and set B is expressed as a-b, representing a set of all elements that belong to a and that do not belong to B. This command also supports incoming multiple keys, meaning that A and B do the difference set operation first, then the result and C do the difference set operation.

127.0.0.1:6379> Sdiff SetA setb
1) "1"
127.0.0.1:6379> sdiff setb setc
1) "2"
127.0.0.1:6379> Sdiff SetA Setb setc
1) "1"

(2) Next we introduce the sinter command, which is used to perform intersection operations on multiple sets. The intersection of set A and set B is expressed as A∩B, which is the set of all elements that belong to a and belong to B. This command also supports the introduction of multiple keys at the same time, the same, step-by-step to do the intersection operation.

127.0.0.1:6379> sinter SetA setb
1 "2"
2) "3" 127.0.0.1:6379> sinter setA setb setc
1) "3"

(3) Finally, we introduce the command of the set operation sunion . The combination of set A and set B means a set that belongs to both A and B. Multiple key passes are also supported.

127.0.0.1:6379> sunion SetA setb
1 "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> sunion SetA Setb setc
1) "1"
2) "2" 3
) "3" 4
) "4"
5 "" 5 "

5, get the number of elements in the collection

SCard key
127.0.0.1:6379> scard setA
(integer) 3
127.0.0.1:6379> scard setb
(integer) 3

This command is used to get the number of elements in the collection. Likewise the set does not exist to return 0.

6. Perform the set operation and store the results

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

Here, after all the set commands are added store , it is clear that the meaning of the storage. destinationis the destination, which is the key name we want to store.

Cases:

127.0.0.1:6379> sunionstore setall SetA setb setc
(integer) 5
127.0.0.1:6379> smembers setall
1) "1" c10/>2) "2"
3) "3"
4) "4" 5 "5"

7, random access to the elements of the collection

Srandmember key [Count]

This command is used to randomly get an element from the collection, and the Count parameter is used to obtain multiple elements, depending on the positive or negative value of the count.

(1) When Count is a positive number, gets count of the elements that are not duplicates, and returns all the elements if count is greater than all the values.

(2) A negative value gets the |count| element, but it may be the same.

In fact, this random is not very random, because in fact the collection of Redis is a hash of the storage structure, interested readers can be in-depth study of their own.

127.0.0.1:6379> srandmember SetA 1
1) "1"
127.0.0.1:6379> srandmember SetA 2
1) "3"
2) "2"
127.0.0.1:6379> srandmember SetA 5
1) "1"
2) "2" 3
) "3"
127.0.0.1:6379> srandmember
setA-2 1) "3"
2) "3"

8. Eject an element from the collection

Spop Key

We've seen Lpop and Rpop before, which is popped from the list, but the collection is unordered, so spop is randomly ejected. (The popup element is deleted, not in the original collection)

127.0.0.1:6379> spop SetA 1
1) "2"
127.0.0.1:6379> smembers setA
1) "1"
2) "3"

Let's take an example of a set of specific applications.

When we store this article, we have a tag tag, which we need to use the Intermediate Table Association when we are in the relational database. But in the Redis, we can handle it conveniently.

For each article, we use a post: The article ID:tags 's key, type for the collection, store an article belongs to tags, so that the relationship between the database we need to relate the article table, label table, intermediate relational table three tables complex operation, in the Redis is very good treatment.

Sometimes we also need to get all the articles for the specified label, we also need to add a class of keys, that is tag: the tag name :posts of the collection type key, store each tag's article ID collection, so that each time we want to get all of the list of articles that belong to a certain tag, we do not have to be associated with so many tables like relational databases , but directly through the key can be directly obtained.

And when we want to get things like the Java and Redis tags at the same time, we just need to tag:java:posts tag:redis:posts do the intersection operation on the OK, is not very convenient?

Summarize

The above is the entire content of this article, I hope to be able to learn or work to bring certain help, if you have questions you can message exchange.

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.