This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging”

  • REmote DIctionary Server (Redis) is a Key Value storage system and one of the most famous NoSQL databases.

  • Redis is often used as a system Cache. It is widely used in the Internet industry, and it is a necessary skill to enter the Internet industry.

  • The Set of Redis is an unordered, nonrepeating collection of string.

  • The general idea of data operation of collection type is to determine the collection by key. Key is the identity of the collection. The element has no subscript, and only directly operates the business data and the number of data.

  • The Redis Set type can be compared to the Java Set type, but the two are different.

Let’s practice manipulating Set commands!

1, the sadd

Syntax: sadd key member [member…]

Function: add one or more member elements to a set key. Member elements that already exist in the set are ignored and will not be added.

Return: number of new elements added to the collection (excluding ignored elements).

2, smembers

Syntax: smembers key

Run the following command to obtain all the member elements in the key of a set. Keys that do not exist are regarded as empty sets.

Return value: returns the set of all elements of the specified set, non-existent key, returns an empty set.

3, sismember

Grammar: sismember key member

Function: Determine whether the member element is an element of the set key

Return values: member is a member of the collection, returns 1, others return 0.

4, scard

Syntax: scard key

Function: Get the number of elements in a collection

Return value: number, number of elements of key. Otherwise, return 0.

5, srem

Syntax: srem key member [member…]

Function: Remove one or more elements from a collection. Nonexistent elements are ignored.

Return value: number of successfully removed elements, excluding ignored elements.

6, srandmember

Syntax: srandmember key[count]

Function: only provide key, randomly return an element in the set, the element is not deleted, still in the set;

When count is provided, the count is positive, and the set containing the count number of elements is returned.

If count is negative, return a set of lengths of the absolute value of count. The elements in the set may be repeated multiple times.

Return value: one element or set of elements

7, spop

Syntax: spop key[count]

Function: randomly deletes one or count of elements from a collection.

Return value: deleted element, key does not exist or empty collection returns nil.

8, smove

Syntax: smove SRC dest member

Smove: removes member from SRC set smove: removes member from SRC set smove: removes member from SRC set smove: removes member from SRC set smove: removes member from SRC set smove: removes member from SRC set

Return value: 1 on success, 0 otherwise.

9 sdiff.

Syntax: sdiff key key [key…

Function: returns the difference set of the specified set, that is, the set of elements that are present in the first set but not in any other set.

Return value: returns a set of elements that are present in the first set and not present in any of the following sets, or an empty set if the elements in the first set are present in all of the following sets.

10, sinter

Syntax: sinter key key [key…

Function: returns the intersection of all elements in a specified set.

Return value: the set of intersection elements, or an empty set if none.

11, sunion

Syntax: sunion key key [key…

Function: return the union of the specified set, that is, a large set of all the elements of the specified set, if there are duplicate elements, keep one.

Return value: returns a large set of all set elements, or an empty set if none of the keys exist.