Redis command o...login
Redis command operation Chinese manual
author:php.cn  update time:2022-04-12 14:07:28

Redis Set


Redis’ Set is an unordered collection of string type. Set members are unique, which means that duplicate data cannot appear in the set.

In Redis, collections are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).

The maximum number of members in a collection is 232 - 1 (4294967295, each collection can store more than 4 billion members).

Example

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 inserted three elements into the collection named w3ckey through the SADD command.


Redis collection commands

The following table lists the basic commands of Redis collection:

Serial number Command and description
1SADD key member1 [member2]
Add one or more members to the collection
2SCARD key
Get the number of members of the set
3SDIFF key1 [key2]
Return all given sets The difference set
4SDIFFSTORE destination key1 [key2]
Returns the difference set of all given sets and stores it in destination
5SINTER key1 [key2]
Returns the intersection of all given sets
6SINTERSTORE destination key1 [key2]
Return the intersection of all given sets and store it in destination
7SISMEMBER key member
Determine whether the member element is a set key Members of
8SMEMBERS key
Returns all members in the set
9SMOVE source destination member
Move the member element from the source collection to the destination collection
10SPOP key
Remove and return one of the collections Random element
11SRANDMEMBER key [count]
Returns one or more random numbers in the set
12SREM key member1 [member2]
Remove one or more members in the set
13SUNION key1 [key2]
Return the union of all given sets
14SUNIONSTORE destination key1 [key2]
The union of all given sets is stored in the destination set
15SSCAN key cursor [MATCH pattern] [COUNT count]
Iterate over the elements in the collection

php.cn