预置数据
127.0.0.1:6379> sadd user1:follow 'user2'
(integer) 1
127.0.0.1:6379> sadd user1:follow 'user3'
(integer) 1
127.0.0.1:6379> sadd user1:follow 'user5'
(integer) 1
127.0.0.1:6379> smembers user1:follow
1) "user5"
2) "user3"
3) "user2"
127.0.0.1:6379> sadd user2:follow 'user1'
(integer) 1
127.0.0.1:6379> sadd user2:follow 'user4'
(integer) 1
127.0.0.1:6379> sadd user2:follow 'user5'
(integer) 1
127.0.0.1:6379> smembers user2:follow
1) "user5"
2) "user1"
3) "user4"
删除集合中的某个元素(SREM)
127.0.0.1:6379> smembers user4:follow
1) "user1"
2) "user4"
127.0.0.1:6379> srem user4:follow user3
(integer) 0
127.0.0.1:6379> srem user4:follow user1
(integer) 1
127.0.0.1:6379> smembers user4:follow
1) "user4"
127.0.0.1:6379>
计算出两个集合的交集(SINTER)
127.0.0.1:6379> sinter user1:follow user2:follow
1) "user5"
将一个元素从一个集合移动到另一个集合(SMOVE)
127.0.0.1:6379> smove user2:follow user1:follow 'user6'
(integer) 0
127.0.0.1:6379> smembers user1:follow
1) "user5"
2) "user3"
3) "user2"
127.0.0.1:6379> smembers user2:follow
1) "user5"
2) "user1"
3) "user4"
127.0.0.1:6379> smove user2:follow user1:follow 'user5'
(integer) 1
127.0.0.1:6379> smembers user1:follow
1) "user5"
2) "user3"
3) "user2"
127.0.0.1:6379> smembers user2:follow
1) "user1"
2) "user4"
127.0.0.1:6379> smove user2:follow user1:follow 'user4'
(integer) 1
127.0.0.1:6379> smembers user1:follow
1) "user5"
2) "user3"
3) "user2"
4) "user4"
127.0.0.1:6379> smembers user2:follow
1) "user1"
127.0.0.1:6379> sinter user1:follow user2:follow
(empty list or set)
返回所有集合的并集(SUNION)
127.0.0.1:6379> sunion user1:follow user2:follow
1) "user3"
2) "user5"
3) "user2"
4) "user4"
5) "user1"
返回集合的基数(SCARD)
127.0.0.1:6379> scard user1:follow
(integer) 4
127.0.0.1:6379> scard user2:follow
(integer) 1
将两个集合的交集成为另一个key的值(SINTERSTORE)
127.0.0.1:6379> sadd user3:follow 'user2'
(integer) 1
127.0.0.1:6379> sadd user3:follow 'user4'
(integer) 1
127.0.0.1:6379> smembers user3:follow
1) "user2"
2) "user4"
127.0.0.1:6379> sinterstore user4:follow user1:follow user3:follow
(integer) 2
127.0.0.1:6379> smembers user4:follow
1) "user2"
2) "user4"
移除集合中的一个随机元素(SPOP)
127.0.0.1:6379> spop user1:follow
"user4"
127.0.0.1:6379> smembers user1:follow
1) "user5"
2) "user3"
3) "user2"
127.0.0.1:6379> spop user1:follow
"user5"
127.0.0.1:6379> smembers user1:follow
1) "user3"
2) "user2"
将集合的并集作为另一个键的值(SUNIONSTORE)
127.0.0.1:6379> sunionstore user5:follow user1:follow user2:follow user3:follow
(integer) 4
127.0.0.1:6379> smembers user5:follow
1) "user3"
2) "user1"
3) "user2"
4) "user4"
计算两个集合的差集(SDIFF)
127.0.0.1:6379> smembers user1:follow
1) "user3"
2) "user2"
127.0.0.1:6379> sdiff user5:follow user1:follow
1) "user1"
2) "user4"
判断一个元素是否为某个集合内的元素(ISMEMBER)
127.0.0.1:6379> smembers user1:follow
1) "user3"
2) "user2"
127.0.0.1:6379> sismember user1:follow user1
(integer) 0
127.0.0.1:6379> sismember user1:follow user3
(integer) 1
返回集合中的一个随机元素(SRANDMEMBER)
127.0.0.1:6379> srandmember user1:follow
"user2"
计算两个集合的差集并将结果作为值赋值给一个键(SDIFFSTORE)
127.0.0.1:6379> smembers user1:follow
1) "user3"
2) "user2"
127.0.0.1:6379> smembers user5:follow
1) "user3"
2) "user1"
3) "user2"
4) "user4"
127.0.0.1:6379> smembers user4:follow
1) "user2"
2) "user4"
127.0.0.1:6379> sdiffstore user4:follow user5:follow user1:follow
(integer) 2
127.0.0.1:6379> smembers user4:follow
1) "user1"
2) "user4"