集与列表有2个主要区别:
- 套装不订购
- 设置只容纳一次
使用创建一个集
SADD <setkey> <value>
使用同一命令可将更多项目添加到集合中。
例子:
SADD names "Flavio"
SADD names "Roger"
SADD names "Tony" "Mark" "Jane"Get all the items in a set using SMEMBERS <setkey>
:

Find out if a value is in a set with SISMEMBER
:
SISMEMBER names "Flavio"
To know how many items are in a set, use SCARD
:
SCARD namesGet (without removing) an item from the set, randomly:
SRANDMEMBER namesExtract (and remove) an item from the set, casually ordered:
SPOP namesYou can extract multiple items at once:
SPOP names 2Remove an item from a set by value:
SREM names "Flavio"
Get the items contained in 2 different sets, exclusing elements only included in one with SINTER
:
SINTER set1 set2See all the sets commands here.
More redis tutorials:
- Introduction to Redis
- How to install Redis
- First steps with Redis
- Redis Lists
- Using Redis Sets
- How to use Redis Sorted Lists
- How to use Redis Hashes
- Redis Publish/subscribe