Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Redis Collection (Set)


May 16, 2021 Redis


Table of contents


Redis Collection (Set)

Redis's Set is a disordered collection of string types. C ollection members are unique, which means that duplicate data cannot occur in the collection.

Collections in Redis are implemented through hash tables, so add, delete, and find are all O(1).

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

Instance

redis 127.0.0.1:6379> SADD w3ckey redis
(integer) 1
redis 127.0.0.1:6379> SADD w3ckey mongodb
(integer) 1
redis 127.0.0.1:6379> SADD w3ckey mysql
(integer) 1
redis 127.0.0.1:6379> SADD w3ckey mysql
(integer) 0
redis 127.0.0.1:6379> SMEMBERS w3ckey

1) "mysql"
2) "mongodb"
3) "redis"

In the above example, we insert three elements into a collection named w3ckey through the SADD command.


Redis collection command

The following table lists the basic commands for the Redis collection:

Serial number Commands and descriptions
1 SADD key member1 [member2]
Add one or more members to the collection
2 SCARD key
Gets the number of members of the collection
3 SDIFF key1 [key2]
Returns the difference set for all given collections
4 SDIFFSTORE destination key1 [key2]
Returns the difference set for all given collections and stores them in destination
5 SINTER key1 [key2]
Returns the intersection of all given collections
6 SINTERSTORE destination key1 [key2]
Returns the intersection of all given collections and is stored in destination
7 SISMEMBER key member
Determines whether the member element is a member of the collection key
8 SMEMBERS key
Returns all members of the collection
9 SMOVE source destination member
Move the member element from the source collection to the destination collection
10 SPOP key
Remove and return a random element from the collection
11 SRANDMEMBER key [count]
Returns one or more random numbers in the collection
12 SREM key member1 [member2]
Remove one or more members from the collection
13 SUNION key1 [key2]
Returns the set of all given collections
14 SUNIONSTORE destination key1 [key2]
The collection of all given collections is stored in the destination collection
15 SSCAN key cursor [MATCH pattern] [COUNT count]
The element in the iterative collection