Redis Detailed: Sets data types and operations

Source: Internet
Author: User

Set is a set, and we are similar to the concept of set in mathematics, the operation of the set has added delete elements, there are multiple sets of orthogonal poor operation, the operation of key is understood as the name of the collection.

Series Articles:

Redis Detailed: Strings data types and operations

Redis Detailed: Hashes data types and operations

Redis Detailed: Lists data types and operations

Redis's set is an unordered collection of type string. The set element can contain a maximum of (2 32-square) elements.

Set is implemented by hash table, so the complexity of adding, deleting, and finding is O (1). The hash table is automatically resized as it is added or removed. It is important to note that when adjusting the hash table size, it is necessary to synchronize (acquire write lock) to block other read and write operations, and it may be possible to use the skip list instead of the jump table, which is already used in the sorted set. For the Set collection type in addition to the basic add-delete operation, other useful operations include the collection's union, intersection (intersection), and difference set (difference). Through these actions can easily realize the SNS in the Friend referral and blog tag function. Set-related commands are detailed below:

  1, Sadd

To add an element to a set named key:

Redis127.0.0.1:6379>Sadd MySet  "Hello"
(integer)  1
Redis 127.0.0.1:6379>  Sadd myset  "World"
(integer)  1
Redis 127.0.0.1:6379>  sadd myset  "World"
( Integer)  0
Redis  127.0.0.1:6379> smembers myset
1)    "World"
2)   "Hello"
Redis 127.0.0.1:6379>

In this case, we added three elements to MySet, but since the third element is the same as the second element, the third element was not added successfully, and finally we used smembers to see all the elements in the MySet.

 2, Srem

Delete the element member in the set named key:

Redis127.0.0.1:6379>Sadd Myset2"One"
(integer)1
Redis127.0.0.1:6379>Sadd Myset2"Both"
(integer)1
Redis127.0.0.1:6379>Sadd Myset2  "three"
(integer)  1
Redis 127.0.0.1:6379>  Srem myset2  "one"
(integer)  1
Redis 127.0.0.1:6379>  srem myset2  "Four"
( Integer)  0
Redis  127.0.0.1:6379> smembers myset2
1)    "three"
2)   "I"
Redis 127.0.0.1:6379>

In this example, we have added three elements to Myset2 and then called Srem to delete one and four, but this Srem command failed because the element does not have an four.

 3, Spop

Randomly returns and deletes an element in a set with the name key:

Redis127.0.0.1:6379>Sadd Myset2"One"
(integer)1
Redis127.0.0.1:6379>Sadd Myset2"Both"
(integer)1
Redis127.0.0.1:6379>Sadd Myset2  "three"
(integer)  1
Redis 127.0.0.1:6379>  Srem myset2  "one"
(integer)  1
Redis 127.0.0.1:6379>  srem myset2  "Four"
( Integer)  0
Redis  127.0.0.1:6379> smembers myset2
1)    "three"
2)   "I"
Redis 127.0.0.1:6379>

In this example, we have added three elements to Myset3 and then called Spop to randomly delete an element, and we can see that the three element has been deleted.

4, Sdiff

Returns the difference set between all given key and the first key:

Redis127.0.0.1:6379>Sadd Myset2"One"
(integer)1
Redis127.0.0.1:6379>Sadd Myset2"Both"
(integer)1
Redis127.0.0.1:6379>Sadd Myset2  "three"
(integer)  1
Redis 127.0.0.1:6379>  Srem myset2  "one"
(integer)  1
Redis 127.0.0.1:6379>  srem myset2  "Four"
( Integer)  0
Redis  127.0.0.1:6379> smembers myset2
1)    "three"
2)   "I"
Redis 127.0.0.1:6379>

In this example, we can see that the elements in Myset2 are different from the Myset3 in the three, so only three is detected, not three and one, because one is the element of Myset3.

We can also take Myset2 and Myset3 in a different order to see the results:

Redis127.0.0.1:6379> Sdiff myset3 Myset2
1) "one"
Redis 127.0.0.1:6379>

This result shows only the elements in the Myset3 and the different elements in the Myset2.

  5, Sdiffstore

Returns the difference between all the given key and the first key, and saves the result as another key:

Redis127.0.0.1:6379> smembers myset2
1)   "three "
2)  " "," Redis  127.0.0.1:6379> smembers myset3
1)   "one"
2)   "one"
redis< Span class= "Apple-converted-space" > 127.0.0.1:6379> sdiffstore Myset4 myset2 myset3
(integer)  1
Redis 127.0.0.1:6379> smembers myset4
1) Span class= "Apple-converted-space" >  "three"
Redis  127.0.0.1:6379>

  6, Sinter

Returns the intersection of all given keys:

Redis127.0.0.1:6379> smembers Myset2
1) "three"
2) "both"
Redis 127.0.0.1:6379> smembers myset3
1) "both"
2) "one"
Redis 127.0.0.1:6379> sinter Myset2 myset3
1) "both"
Redis 127.0.0.1:6379>

As can be seen from the results of this example, the intersection of Myset2 and Myset3 is detected.

  7, Sinterstore

Returns the intersection of all given keys and saves the result as another key

Redis127.0.0.1:6379> smembers myset2
1)   "three "
2)  " "," Redis  127.0.0.1:6379> smembers myset3
1)   "one"
2)   "one"
redis< Span class= "Apple-converted-space" > 127.0.0.1:6379>  Sinterstore myset5 myset2 myset3
(integer)  1
Redis 127.0.0.1:6379> smembers myset5
1) Span class= "Apple-converted-space" >  "both"
Redis 127.0.0.1:6379

As can be seen from the results of this example, the intersection of Myset2 and Myset3 is saved in Myset5.

  8, Sunion

Returns the set of all the given keys

Redis127.0.0.1:6379> smembers Myset2
1) "three"
2) "both"
Redis 127.0.0.1:6379> smembers myset3
1) "both"
2) "one"
Redis 127.0.0.1:6379> sunion Myset2 myset3
1) "three"
2) "one"
3) "both"
Redis 127.0.0.1:6379>

The results of this example show that the Myset2 and Myset3 are detected.

  9, Sunionstore

Returns the set of all the given keys and saves the result as another key

Redis127.0.0.1:6379>Smembers Myset2
1)"Three"
2)  "both"
Redis  127.0.0.1:6379> smembers myset3
1)   "one"
2)   "one"
redis< Span class= "Apple-converted-space" > 127.0.0.1:6379>  Sunionstore myset6 myset2 myset3
(integer)  3
Redis 127.0.0.1:6379> smembers myset6
1) Span class= "Apple-converted-space" >  "three"
2)   "one"
3) Span class= "Apple-converted-space" >  "both"
Redis 127.0.0.1:6379

The result of this example shows that the Myset2 and Myset3 are saved in Myset6.

 10, Smove

Remove the member from the set corresponding to the first key and add it to the second corresponding set

Redis127.0.0.1:6379> smembers myset2
1)   "three "
2)  " "," Redis  127.0.0.1:6379> smembers myset3
1)   "one"
2)   "one"
redis< Span class= "Apple-converted-space" > 127.0.0.1:6379> smove Myset2 myset7 three
(integer)  1
Redis 127.0.0.1:6379> smembers myset7
1) Span class= "Apple-converted-space" >  "three"
Redis  127.0.0.1:6379>

As you can see from this example, Myset2 's three is moved to Myset7.

11, SCard

Returns the number of elements of a set with the name key

Redis127.0.0.1:6379> SCard Myset2
(integer) 1
Redis 127.0.0.1:6379>

As you can see from this example, the number of members of Myset2 is 1

  12, Sismember

Test whether the member is a set element with the name key

Redis127.0.0.1:6379> smembers Myset2
1) "both"
Redis 127.0.0.1:6379> sismember Myset2
(integer) 1
Redis 127.0.0.1:6379> sismember Myset2 One
(integer) 0
Redis 127.0.0.1:6379>

In this example, you can see that both are members of the Myset2 and one is not.

  13, Srandmember

Randomly returns an element of a set named key, but does not delete the element

Redis127.0.0.1:6379> smembers Myset3
1) "both"
2) "one"
Redis 127.0.0.1:6379> srandmember myset3
"Both"
Redis 127.0.0.1:6379> srandmember myset3
"One"
Redis 127.0.0.1:6379>

Redis Detailed: Sets data types and operations

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.