Set (Set)

The Set of Redis is an unordered Set of String. Set members are unique, which means that there can be no duplicate data in the set.

The encoding of a collection object can be intSet or HashTable.

The collection in Redis is implemented by hash table, so the complexity of adding, deleting and searching is O(1).

The largest number of members in a set is 2^32-1 (4294967295, each set can store more than 4 billion members).

Using the instance

Set the value of the collection: sadd

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > sadd myset world (integer) 1 127.0.0.1:6379> sadd mySet Hello (INTEGER) 1 127.0.0.1:6379> sadd mySet Hello # Failed to repeat add (INTEGER) 0Copy the code

View all values of the collection: sMembers, where 1 exists and 0 does not exist

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > smembers myset 1) "world" 2) "hello"Copy the code

Determine whether a value is in the set: sisMember, if 1 exists, if 0 does not exist

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > sismember myset hello 1 # (integer) 127.0.0.1:6379> sismember myset hi (INTEGER) 0 # Does not existCopy the code

View the number of values in the collection: scard

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > scard myset (integer) 2Copy the code

Removes the specified value: Srem from the collection

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > smembers myset 1) "hi" 2) 3) "world" "Hello" 127.0.0.1:6379> srem myset hi (INTEGER) 1 127.0.0.1:6379> smembers myset 1) "world" 2)Copy the code

Randomly remove the value spop from the set

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > smembers myset # to check the collection of all values (1) "wan" (2) "World" 3) "hello" 4) "li" 127.0.0.1:6379> spop myset 1 "World" 2) "hello" 3) "li" 127.0.0.1:6379> spop mySet 2 1) "hello"Copy the code

Extracts random elements from the collection, but does not remove: SrandMember

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > sadd myset hello1 (integer) 1 127.0.0.1:6379> sadd mySet hello2 (INTEGER) 1 127.0.0.1:6379> sadd mySet hello3 (INTEGER) 1 127.0.0.1:6379> sadd mySet Hello4 (integer) 1 127.0.0.1:6379> sadd mySet hello5 (integer) 1 127.0.0.1:6379> sadd mySet Hello6 (integer) 1 127.0.0.1:6379> sadd mySet hello7 (INTEGER) 1 127.0.0.1:6379> sadd mySet hello8 (INTEGER) 1 127.0.0.1:6379> sadd mySet Hello9 (INTEGER) 1 127.0.0.1:6379> smembers myset 1) "hello7" 2) "hello5" 3) "hello3" 4) "hello4" 5) "hello6" 6) "Hello1" 7) "hello8" 8) "hello2" 9) "hello9" 127.0.0.1:6379> srandmember myset 127.0.0.1:6379> srandMember myset "hello3" 127.0.0.1:6379> srandMember myset "hello9" 127.0.0.1:6379> srandMember myset "hello3" 127.0.0.1:6379> srandMember myset "hello9" 127.0.0.1:6379> srandMember mySet "Hello3" 127.0.0.1:6379> srandmember myset 1) "hello7" 2) "hello8" 127.0.0.1:6379> srandMember Myset 2 1) "hello5" 2) "hello2" 127.0.0.1:6379> srandmember myset 2 1) "hello6" 2) "hello2" 127.0.0.1:6379> srandmember myset 2 1) "hello3" 2) "hello8"Copy the code

To move a specified element from one set to another: smove

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > sadd myset1 hello1 (integer) 1 127.0.0.1:6379> sadd myset2 hello2 (INTEGER) 1 127.0.0.1:6379> smove myset1 myset2 hello1 (integer) 1 127.0.0.1:6379> smembers myset2 1) "hello1" 2) "hello2" 127.0.0.1:6379> smembers myset1 (empty array)Copy the code

Sdiff and Sunion and sinter of two sets

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > sadd characters (integer) 1 a 127.0.0.1:6379> sadd set1 b (integer) 1 127.0.0.1:6379> sadd set1 c (integer) 1 127.0.0.1:6379> sadd set2 c (integer) 1 127.0.0.1:6379> sadd set2d (INTEGER) 1 127.0.0.1:6379> sadd set2e (INTEGER) 1 # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = difference set = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = unique element in the set # characters 127.0.0.1:6379 > sdiff characters set2 1) "b" 1) "e" 2) "a" # set set2 unique element 127.0.0.1:6379> sdiff set2 set1 # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = two collections and set = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > sunion characters set2 (1) "a" (2) "c" 3) "b" 4) 5) "e" "d" 127.0.0.1:6379 > sunion set2 characters 1) "c" "d" 2) 3) "e" 4) 5) "a" "b" # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = the intersection of two sets = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > sinter characters set2 1) "c"Copy the code

For more commands, see:redis.io/commands

\