Summary of common commands for Redis collection types

  • 2020-05-17 06:53:32
  • OfStack

Introduction to collection types

The collection type is also a type that represents a higher value of redis1. Because of the set type of Redis, we can easily perform the difference set operation, intersection operation, and union operation in Redis.

First, let's introduce the difference between a collection type and a list type. Anyone who has studied object-oriented languages should be able to guess the difference between these types.

The collection type and the list type can still store 2^32-1 string

The collection type is unordered, the list type is ordered

(3) the collection type is unique to 1, the value of the list type is not unique to 1

Let's take a look at grammar 1.

1. Add delete element command


sadd key member [member ...]
srem key member [member ...]

sadd Commands are used to add elements to a collection, and of course, based on the previous learning experience, if the collection does not exist, it will be created automatically. But one of the things to note here is that if the element is already there, it will ignore it instead of overwriting it. The return value is the number of elements that were successfully added (ignoring elements does not count).

srem The command is also 1, delete the element, if the element exists will be deleted successfully, the return value is the number of elements successfully deleted.

Get all the elements in the set


smembers key

3. Determine whether an element is in the set


sismember key member

The time complexity of this judgement operation is O(1), and the command always returns results very quickly regardless of the number of elements in the collection. It returns 1 if it exists, 0 if it doesn't exist, or if it doesn't have this key.

4. Operation between sets


sdiff key [key ...]
sinter key [key ...]
sunion key [key ...]

These are the three commands that make Redis work!

Let's prepare the point 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:6379> 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 it sdiff Command, this command is actually a difference set operation.

The difference operation between the set A and the set B is expressed as A-B, which represents the set of all elements belonging to A and not belonging to B. This command supports passing in more than one key at a time, meaning that A and B do the difference set first, and then the result does the difference set with C.


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) let's introduce it sinter Command, which is used to perform intersection operations on multiple collections. The intersection of set A and set B is represented as A∩B, that is, the set of all elements belonging to A and B. This command also supports passing in multiple keys at the same time, is also 1, 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 union operation command sunion . The union of the sets A and B means that they are both members of A and of B. Multiple keys 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 set


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. Again, the set does not exist and returns 0.

6. Perform set operations and store the results


smembers key
0

This is after all the set commands store It's clear what it means to store. destination That's the destination, that's the key name that we're going to store.

Ex. :


smembers key
1

7. Randomly obtain the elements in the set


smembers key
2

This command is used to get 1 element from the collection randomly, and the count parameter is used to get multiple elements from the 1 word, which has different meanings according to the positive of count.

(1) when count is positive, get count non-repeating elements. If count is greater than the number of all values, return all elements.

(2) when the value is negative, |count| elements are obtained, but they may be the same.

In fact, this random is not very random, because in fact, the collection of redis USES a hash storage structure, so interested readers can dig into it one more time on 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. Pop an element from the collection


smembers key
4

We've seen lpop and rpop before, which pop up from the list, but the collection is out of order, so spop pops up randomly. (the pop-up element is removed from the original collection.)


smembers key
5

Let's give an example of a specific application of a set.

We store our articles with the tag tag, which we use for intermediate table associations in relational databases. However, in Redis, we can handle it conveniently.

For each article, we use 1 post: article ID:tags The key of tags, of type set, stores the tags to which one article belongs. In this way, we need to associate the article table, label table and intermediate relational table in the relational database, which is easy to deal with in Redis.

Sometimes we also need to get all the articles for the specified tag, and we also need to add a class 1 key, tag: tag name :posts , which stores the ID collection of articles for each tag, so that we don't need to associate as many tables as a relational database every time we want to get the list of articles that belong to a certain tag, but we can get them directly through the key.

When we want to get articles that, for example, belong to both the java and redis tags, we just need to be right tag:java:posts and srem0 OK for the intersection, isn't that convenient?

conclusion

The above is the entire content of this article, I hope to help you with your study or work, if you have any questions, you can leave a message to communicate.


Related articles: