Redis集合运算

预置数据

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"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 《Redis 入门指南》(第二版) 第一章 Redis 是什么 Redis (REmote Dictionary ...
    EdenPP阅读 67,355评论 3 10
  • 今年首次雾霾红色预警,部分车辆停驶,国标三以上的车辆按单双号行驶,学校停课三天,原计划明天进行的义卖活动推迟到了月...
    青蔌阅读 165评论 0 0
  • 我是一个今年刚毕业的本科毕业生,我认为自己刚刚毕业,所以把姿态放的很低,领导要求的事我都会一丝不苟的完成,现在领导...
    88不99阅读 189评论 0 0
  • 文:剧不终 十一回婆婆家,一路舟车劳顿,刚进家门还没坐定,婆婆就提出来,“你们这次回来,我有个心愿,咱们这次可一定...
    剧不终阅读 509评论 0 0