随笔 - 2649  文章 - 2452  评论 - 0  阅读 - 80424

Redis 集合(Set)

Redis 集合(Set)

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

  - 集合对象的编码可以是 intset 或者 hashtable。

  - 

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

  - 集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。

  - ### 实例

  - ```
    redis 127.0.0.1:6379> SADD runoobkey redis
    (integer) 1
    redis 127.0.0.1:6379> SADD runoobkey mongodb
    (integer) 1
    redis 127.0.0.1:6379> SADD runoobkey mysql
    (integer) 1
    redis 127.0.0.1:6379> SADD runoobkey mysql
    (integer) 0
    redis 127.0.0.1:6379> SMEMBERS runoobkey
    
    1) "mysql"
    2) "mongodb"
    3) "redis"
    ```

  - 在以上实例中我们通过 **SADD** 命令向名为 **runoobkey** 的集合插入的三个元素。

  - ------

  - ## Redis 集合命令

  - 下表列出了 Redis 集合基本命令:

  - | 序号 | 命令及描述                                                   |
    | :--- | :----------------------------------------------------------- |
    | 1    | [SADD key member1 [member2\]](https://www.runoob.com/redis/sets-sadd.html) 向集合添加一个或多个成员 |
    | 2    | [SCARD key](https://www.runoob.com/redis/sets-scard.html) 获取集合的成员数 |
    | 3    | [SDIFF key1 [key2\]](https://www.runoob.com/redis/sets-sdiff.html) 返回第一个集合与其他集合之间的差异。 |
    | 4    | [SDIFFSTORE destination key1 [key2\]](https://www.runoob.com/redis/sets-sdiffstore.html) 返回给定所有集合的差集并存储在 destination 中 |
    | 5    | [SINTER key1 [key2\]](https://www.runoob.com/redis/sets-sinter.html) 返回给定所有集合的交集 |
    | 6    | [SINTERSTORE destination key1 [key2\]](https://www.runoob.com/redis/sets-sinterstore.html) 返回给定所有集合的交集并存储在 destination 中 |
    | 7    | [SISMEMBER key member](https://www.runoob.com/redis/sets-sismember.html) 判断 member 元素是否是集合 key 的成员 |
    | 8    | [SMEMBERS key](https://www.runoob.com/redis/sets-smembers.html) 返回集合中的所有成员 |
    | 9    | [SMOVE source destination member](https://www.runoob.com/redis/sets-smove.html) 将 member 元素从 source 集合移动到 destination 集合 |
    | 10   | [SPOP key](https://www.runoob.com/redis/sets-spop.html) 移除并返回集合中的一个随机元素 |
    | 11   | [SRANDMEMBER key [count\]](https://www.runoob.com/redis/sets-srandmember.html) 返回集合中一个或多个随机数 |
    | 12   | [SREM key member1 [member2\]](https://www.runoob.com/redis/sets-srem.html) 移除集合中一个或多个成员 |
    | 13   | [SUNION key1 [key2\]](https://www.runoob.com/redis/sets-sunion.html) 返回所有给定集合的并集 |
    | 14   | [SUNIONSTORE destination key1 [key2\]](https://www.runoob.com/redis/sets-sunionstore.html) 所有给定集合的并集存储在 destination 集合中 |
    | 15   | [SSCAN key cursor [MATCH pattern\] [COUNT count]](https://www.runoob.com/redis/sets-sscan.html) 迭代集合中的元素 |
posted on   AtlasLapetos  阅读(1)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示