Unlike the list type in the previous article, duplicate elements are not allowed in the set collection, so the set type can quickly determine whether an element exists.
Application scene: Take out two QQ number in the common number of friends, to determine whether an IP in the whitelist and so on.
The following are some common operations commands for unordered collection types (set):
1. "Sadd key Vaule" Inserts an element into the collection and returns 0 if the value value already exists in the collection, and is not inserted repeatedly.
2. "Sinter key1 Key2 ... Keyn" takes out the intersection of N Keys. For example Key1 inside has the value a,b,c,d,e,key2 inside has the D,e,f,sinter key1 Key2 returns D,E.
3. "Sunion key1 Key2 ... Keyn" takes out the set of N key. For example Key1 inside has the value a,b,c,d,e,key2 inside has the d,e,f,sunion key1 Key2 returns A,B,C,D,E,F.
4. "Sdiff key1 Key2" takes out the difference set between key n keys. For example Key1 inside has the value a,b,c,d,e,key2 inside has the D,e,f,sdiff key1 Key2 returns a,b,c;
5. "Smembers key" returns all elements in the key collection, and the result is unordered.
6. "Sismember key value" to see if value is in the key collection. The existence returns 1, does not exist returns 0.
7. "SCard Key" returns how many elements are in the collection.
8. "Smove key1 key2 value" moves value from Key1 to Key2.
9. "Srem key value1 value2 ... valuen" deletes some elements from the key collection.
Visible from the above, the rational use of the set type, the efficiency can achieve geometric multiplication!
Through this write operation we can achieve SNS friend recommendation and blog tag function
1:sadd
Adds an element to the set of name key (unique)
Example: Sadd myset1 One----> add element to collection Myset1 one
Sadd myset1 two
View: smembers Myset1
2:srem
Deletes an element in a set with the name Key
Example: Sadd myset2 one
Srem Myset2 one---> Delete one element
3:spop
Randomly return and delete a name called The elements of the set secondary of the key
Example: Spop myset3---> Random popup deleted element
4:sdiff
Returns the difference set for all given keys and first key
myset1=1,2 myset2=3,2--Returns 1
Example: Sdiff myset1 myset2----> who is in front of who is the standard
4:sdiffstore
Returns the difference between all the given key and the first key, and saves the result in a new key
Example: Sdiffstore myset4 myset2 myset3----> The difference set between Myset2 and Myset3 pops up and saves to MYSET4
5:sinter
Returns the intersection of all the given keys (intersection: The same elements inside)
Two,one three,one--->one
Example: Sinter myset2 myset3 the intersection between--->
6:sinterstore
Returns the intersection of all the given key and the first key, and saves the result in a new key
Example: Sinterstore myset4 myset2 myset3----> Save the intersection of Myset2 and Myset3 to MYSET4
7:sunion
Returns the set of all the given keys
Example: Sunion myset1---> Fetch collection
Myset2
Returns the set of all the given key and saves it to the new key
Example: Sunionstore myset4 myset1 Myset2---> Fetch and save to MYSET4
9:smove
Removes an element from the corresponding set in the first key and adds it to the second corresponding key
Example: Smove myset2 myset7 Three---> Move elements in Myset2 to myset7 inside
10:scard
Returns the number of elements of a set with the name Key
Example: SCard myset8----> Return elements
11:sismember
Test whether an element is an element in a set named Key
Example: Sismember Myset2 Two---> Test two is the element in Myset2
12:srandmember (unordered)
randomly returns an element of the set with the name key, but does not delete the element
Example: Srandmember Myset3---> Random removal of myset3 elements