Redis data type is set command sorting and example, redisset

Source: Internet
Author: User
Tags redis version

Redis data type is set command sorting and example, redisset

The data type is set. Ensure that the data in the set is unique. Scenario: generate an order number. Because the order number must be unique, you must set it as a unique index in the database. However, you can use redis and set to create a daily order set. For example, If Customer A's order number is 201803041 and Customer B has the same Order Number concurrently, but customer A inserts the set, and customer B inserts the set, 0 is returned, indicating that the set is repeated and needs to be generated again, to some extent, the database unique verification is reduced.

# Add SADD key member [member...] to the set

Add one or more specified member elements to the key of the set. the specified member element is ignored if it already exists in the set key. if the set key does not exist, create a new set key and add the member element to the set key.

If the key type is not a set, an error is returned.

127.0.0.1:6379> SADD set 1 2 3 3 4 4(integer) 4127.0.0.1:6379> SMEMBERS set1) "1"2) "2"3) "3"4) "4" 
# If you want to know the size of the set, run the scardSCARD key command.
127.0.0.1:6379> SCARD set(integer) 4

  

# If you want to compare a set. Command: sdiff, sdiffstore, sinter, sinterstore, sunion, sunionstoreSDIFF key [key...]

Returns the element of the difference set between a set and a given set.

127.0.0.1: 6379> SADD set1 a (integer) 1127.0.0.1: 6379> SADD set2 a (integer) 1 # compare two identical or empty collections 127.0.0.1: 6379> SDIFF set1 set2 (empty list or set) #127.0.0.1: 6379> SADD set1 B (integer) 1127.0.0.1: 6379> SDIFF set1 set21) "B"

  

SDIFFSTORE destination key [key...]

This command is similar to SDIFF, except that it does not return a result set, but stores the results indestinationCollection.

IfdestinationIf it already exists, overwrite it. It can be understood that the result of the poorer set is saved to the new target set.

127.0.0.1:6379> SDIFF set1 set21) "b"127.0.0.1:6379> SDIFFSTORE set3 set1 set2(integer) 1127.0.0.1:6379> SMEMBERS set31) "b"
SINTER key [key...]

Returns the intersection of all the members of the specified set.

key1 = {a,b,c,d}key2 = {c}key3 = {a,c,e}SINTER key1 key2 key3 = {c}
127.0.0.1:6379> SINTER set1 set21) "a"
SINTERSTORE destination key [key...]

Similar to sdiffstore. Writes the intersection to a new set. If the set exists, overwrite it.

 

SUNION key [key...]

Returns all members in the Union of multiple given sets.

For example:

key1 = {a,b,c,d}key2 = {c}key3 = {a,c,e}SUNION key1 key2 key3 = {a,b,c,d,e}
# If you need to determine whether the value is in the Set, run the following command: sismemberSISMEMBER key member.

Returns whether the member is a member of the stored set key.

# Return values

  • If the member element is a member of the set key, 1 is returned.
  • If the member element is not a member of the key or the set key does not exist, 0 is returned.

 

# Return all elements of the key set. It is used in the above instance. SMEMBERS key

Returns all elements in the key set.

The function of this command is the same as that of the SINTER command using a parameter.

 

SMOVE source destination member

Move member from the source set to the destination set. For other clients, the specific time element will appear as a member of the source or destination set.

If the source set does not exist or does not contain the specified element, the smove command does not perform any operation and returns 0. otherwise, the object will be removed from the source set and added to the destination set. If this element already exists in the destination set, the smove command will only remove this element from the source set. if source and destination are not set types, an error is returned.

127.0.0.1:6379> SADD test4 1 2(integer) 2127.0.0.1:6379> SADD test5 3(integer) 1127.0.0.1:6379> SMOVE test4 test5 2(integer) 1127.0.0.1:6379> SMEMBERS test41) "1"127.0.0.1:6379> SMEMBERS test51) "2"2) "3"
# If you want to bring up values in the set, you can use spop. SPOP key [count]

Is the value in the set popped up at a random position. The count is related to the redis version.

127.0.0.1:6379> SADD test6 1 2 3 4 6 5 7 8 9 10(integer) 10127.0.0.1:6379> SPOP test6 31) "1"2) "5"3) "10"127.0.0.1:6379> SMEMBERS test61) "2"2) "3"3) "4"4) "6"5) "7"6) "8"7) "9"
# Return the specified number of SRANDMEMBER keys in the set randomly [count]

If only the key parameter is provided, an element in the key set is returned randomly.

Redis starts from 2.6 and can accept the count parameter. If count is an integer smaller than the number of elements, an array containing different elements of count is returned, if count is an integer greater than the number of elements in the set, only all elements in the entire set are returned. When count is a negative number, an array of Number elements containing the absolute value of count is returned. If the absolute value of count is greater than the number of elements, an element appears multiple times in the returned result set.

When only the key parameter is provided, this command is similar to the SPOP command. The difference is that the SPOP command removes selected random elements from the set, while the SRANDMEMBER command only returns the note element, do not perform any operations.

127.0.0.1:6379> SADD set7 1 2 3 4 5(integer) 5127.0.0.1:6379> SRANDMEMBER set7"4"127.0.0.1:6379> SRANDMEMBER set7 31) "1"2) "2"3) "4"127.0.0.1:6379> SRANDMEMBER set7 101) "1"2) "2"3) "3"4) "4"5) "5"127.0.0.1:6379> SRANDMEMBER set7 -101) "4"2) "2"3) "1"4) "2"5) "5"6) "4"7) "1"8) "3"9) "3"10) "1"
# Remove a value from the set, SREM key, member [member...]

Remove the specified element from the key set. If the specified element is not an element in the key set, ignore it. If the key set does not exist, it is considered as an empty set. This command returns 0.

If the key type is not a set, an error is returned.

127.0.0.1:6379> SMEMBERS set71) "1"2) "2"3) "3"4) "4"5) "5"127.0.0.1:6379> SREM set7 2(integer) 1127.0.0.1:6379> SMEMBERS set71) "1"2) "3"3) "4"4) "5"

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.