Redis set (collection) related commands

Redis is an unordered collection of the Set of type String, the element must be unique.

Redis is achieved through the collection of the hash table, so add, delete, search complexity is O (1).

Set the maximum number of elements is 2 32  - 1 (40 billion).

 

 

1, sadd key value1 value2 ...... // add one or more elements to the collection. sadd that is set add, key is the name of the collection. Returns the number of elements in the add.

 

2, scard key // returns the number of elements of the set

 

3, smembers key // returns a collection of all the elements

 

4, sismember key elementValue // elementValue determine whether this element of the set. 1 has returned, no or 0 is returned key does not exist.

 

5, spop key // removes and returns a random element in the set

 

6, srandmember key // returns a random element in the set, it does not remove the element. srandmember that is set random member.

You can take an optional parameter: srandmember key count // count is an integer that specifies the number of elements is returned.

count> 0: return count random elements, these elements are different from each other. After a random determination to return the element, randomly from the remaining elements.

count <0: Returns the count of the absolute value of a random elements, which may be the same. A random every time from the entire collection.

When the absolute value of the count is greater than the number of elements equal to the set, return the set of all the elements.

 

7, srem key value1 value2 .... // remove a collection of one or more elements, if the collection is not the element, the element is ignored.

 

8, smove sourceKey destKey elementValue // the elements from a collection to another collection. (Cut)

 

 

 

9, sscan key cursor match pattern count num // iteration elements in the collection. cursor specifying operation for each element.

2 optional parameters:

  • match pattern specified configuration mode, only the interior elements in line with the pattern
  • count num specifies the number of elements to iterate

 

 

 

 

10, sinter key1 key2 .... // return the intersection of the sets

11, sinterstore destKey key1 key2 ..... // Store more, seeking key1, key2 ..... the intersection of the sets, and the intersection of the set of elements stored in destKey, returns the number of elements in the intersection

 

The same usage as well:

  • sunion union
  • sdiff difference set

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11079191.html