Redis入门之Set

Redis入门之Set

Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。

Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。

  • inter 交集
  • union 并集
  • diff 差集

具体指令

序号命令描述
1SADD key member1 [member2]向集合添加一个或多个成员
2SCARD key获取集合的成员数
3SDIFF key1 [key2]返回第一个集合与其他集合之间的差异。
4SDIFFSTORE destination key1 [key2]返回给定所有集合的差集并存储在 destination 中
5SINTER key1 [key2]返回给定所有集合的交集
6SINTERSTORE destination key1 [key2]返回给定所有集合的交集并存储在 destination 中
7SISMEMBER key member判断 member 元素是否是集合 key 的成员
8SMEMBERS key返回集合中的所有成员
9SMOVE source destination member将 member 元素从 source 集合移动到 destination 集合
10SPOP key移除并返回集合中的一个随机元素
11SRANDMEMBER key [count]返回集合中一个或多个随机数
12SREM key member1 [member2]移除集合中一个或多个成员
13SUNION key1 [key2]返回所有给定集合的并集
14SUNIONSTORE destination key1 [key2]所有给定集合的并集存储在 destination 集合中
15SSCAN key cursor [MATCH pattern] [COUNT count]迭代集合中的元素

随机用法

  • SRANDMEMBER key [count]

正数:取出一个去重的结果集(不能超过已有集)

负数:取出一个带重复的结果集,一定满足你要的数量

如果:0,不返回

  • spop

取出1个

使用场景

抽奖

其他


  SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0

  SCARD key
  summary: Get the number of members in a set
  since: 1.0.0

  SDIFF key [key ...]
  summary: Subtract multiple sets
  since: 1.0.0

  SDIFFSTORE destination key [key ...]
  summary: Subtract multiple sets and store the resulting set in a key
  since: 1.0.0

  SINTER key [key ...]
  summary: Intersect multiple sets
  since: 1.0.0

  SINTERSTORE destination key [key ...]
  summary: Intersect multiple sets and store the resulting set in a key
  since: 1.0.0

  SISMEMBER key member
  summary: Determine if a given value is a member of a set
  since: 1.0.0

  SMEMBERS key
  summary: Get all the members in a set
  since: 1.0.0

  SMOVE source destination member
  summary: Move a member from one set to another
  since: 1.0.0

  SPOP key [count]
  summary: Remove and return one or multiple random members from a set
  since: 1.0.0

  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0

  SREM key member [member ...]
  summary: Remove one or more members from a set
  since: 1.0.0

  SSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate Set elements
  since: 2.8.0

  SUNION key [key ...]
  summary: Add multiple sets
  since: 1.0.0

  SUNIONSTORE destination key [key ...]
  summary: Add multiple sets and store the resulting set in a key
  since: 1.0.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多多洛码代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值