The nature of a set : uniqueness , disorder , certainty
Note : in the string and link commands , you can access a certain number of characters in a string or several elements by using range
However , because of the unordered nature of the collection , some elements cannot be accessed by subscript or scope .
So want to see the elements, either randomly first , or select all
sadd key value1 value2 function: Add elements to the set key 127.0.0.1:6379>2127.0.0.1:6379> 1127.0.0.1:6379>0
smembers Key function: Returns all elements in the set 127.0.0.1:6379> smembers Gender1) "Male" 2) "female" 3) "Yao"
srem value1 value2 Effect: Removes the return value of the element set in the collection as value1 value2: The number of elements that were actually deleted after ignoring the nonexistent element 127.0.0.1:6379> Srem Gender Yao (integer) 1127.0.0.1:6379> Srem gender x C (integer) 0127.0.0.1:6379> Srem gender male x C (integer) 1
spop key//Lottery Effect: Returns and deletes 1 random elements in a key in the collection--the disorder 127.0.0.1:6379> Sadd gender a B c D E F (integer) 6127.0.0.1:6379> smembers gender 1) "D" 2) "C" 3) "a" 4) "F" 5) "female" 6) "B" 7) "E" 127.0.0.1:6379> Spop gender " a " 127.0.0.1:6379> smembers gender 1) "D" 2) "C" 3) "F" 4) "female" 5) "B" 6) "E" 127.0.0.1:6379> Spop gender " C "127.0.0.1:6379> Spop gender " F "127.0.0.1:6379> Spop gender "E"
srandmember key function: Returns the Set key, a random 1 element. 127.0.0.1:6379> smembers gender 1) "D" 2) "C" 3) "a" 4) "F" 5) "female" 6) "E" 7) "B" 127.0.0.1:6379> Srandmember gender " female "127.0.0.1:6379> Srandmember gender "F" 127.0.0.1:6379> Srandmember gender "C" 127.0.0.1:6379> Srandmember gender " C "127.0.0.1:6379> Srandmember gender "a" 127.0.0.1:6379> Srandmember gender " D "
sismember Key value function: Determines whether value is returned 1 in the key set, no return 0127.0.0.1:6379> smembers gender1) "D" 2) "C" 3) "a" 4) "F" 5) "female" 6) "E" 7) "B" 127.0.0.1:6379>0127.0.0.1:6379>1
SCard Key function: Returns the number of elements in the collection
127.0.0.1:6379> SCard Gender
(integer) 7
smove source dest Value: Removes the value from source and adds it to the Dest collection 127.0.0.1:6379 > Sadd Upper A B C (integer) 3127.0.0.1:6379> Sadd Lower a b C (integer) 3127.0.0.1:6379> Smove Upper Lower A (integer) 1127.0.0.1:6379> smembers Upper 1) "C" 2) "B "127.0.0.1:6379> smembers lower 1)" C "2)" a "3)" B "4)" a "
sinter key1 key2 Key3 effect: Find the intersection of Key1 Key2 Key3 three sets and return Sinterstore dest Key1 Key2 Key3 as Use: To find the intersection of Key1 Key2 Key3 three sets, and assign to Destsuion Key1 Key2. Keyn function: Find the Key1 key2 Keyn and return 127.0.0.1:6379> Sadd Lisi a B C d (integer) 4127.0.0.1:6379> Sadd Wang a C d E F (integer) 5127.0.0.1:6379> Sadd Poly a C D g (integer) 4127.0.0.1:6379> sinter Lisi Wang Poly 1) "D" 2) "C" 3) "a" 127.0.0.1:6379> Sinterstore common Lisi Wang Poly (integer) 3127.0.0.1:6379> smembers common 1) "D" 2) "C" 3) "a" 127.0.0.1:6379> sunion Lisi Wang Poly 1) "D" 2) "G" 3) "C" 4) "a" 5) "F" 6) "E" 7) "B"
Sdiff key1 key2 Key3 Effect: Find the difference between Key1 and key2 Key3 thatkey1-key2-127.0.0.1:6379> sdiff Lisi Wang Poly1) "B"
Redis's set operation