首页 > 数据库 >Redis数据类型之无序集合

Redis数据类型之无序集合

时间:2022-10-16 21:23:07浏览次数:54  
标签:127.0 0.1 数据类型 Redis 无序 6379 key integer SMEMBERS

目录

Redis数据类型之无序集合

Redis 的 Set 是 string 类型的无序集合。集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。集合中最大的成员数为 2^32 - 1 (4294967295, 每个集合可存储40多亿个成员)。

查看命令帮助

127.0.0.1:6379> help @set

  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

增加数据

给集合新增成员,若集合不存在则创建集合并新增成员

SADD

127.0.0.1:6379> SADD myset 1
(integer) 1
127.0.0.1:6379> SADD myset 2
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "1"
2) "2"

查看数据

SMEMBERS

127.0.0.1:6379> SMEMBERS myset
1) "1"
2) "2"

删除数据

SREM

127.0.0.1:6379> SMEMBERS myset
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> SREM myset 3 4
(integer) 2
127.0.0.1:6379> SMEMBERS myset
1) "1"
2) "2"
3) "5"

移动数据

SMOVE

127.0.0.1:6379> SADD myset1 1 2 3 4 5 6
(integer) 6
127.0.0.1:6379> SADD myset2 noe two three
(integer) 3
127.0.0.1:6379> SMOVE myset1 myset2 4
(integer) 1
127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "5"
5) "6"
127.0.0.1:6379> SMEMBERS myset2
1) "4"
2) "three"
3) "two"
4) "noe"

返回集合中成员的个数

SCARD

127.0.0.1:6379> SMEMBERS myset2
1) "4"
2) "three"
3) "two"
4) "noe"
127.0.0.1:6379> SCARD myset2
(integer) 4

随机返回指定个数的数据

SRANDMEMBER

127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "5"
5) "6"
127.0.0.1:6379> SRANDMEMBER myset1 2
1) "6"
2) "5"
127.0.0.1:6379> SRANDMEMBER myset1 3
1) "2"
2) "1"
3) "3"
127.0.0.1:6379> SRANDMEMBER myset1 3
1) "2"
2) "5"
3) "3"

判断对象是否存在于集合中

SISMEMBER

127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "5"
5) "6"
127.0.0.1:6379> SISMEMBER myset1 3
(integer) 1
127.0.0.1:6379> SISMEMBER myset1 9
(integer) 0

随机返回并删除一个成员

SPOP

127.0.0.1:6379> SADD myset3 q w e r t f g h
(integer) 8
127.0.0.1:6379> SMEMBERS myset3
1) "f"
2) "w"
3) "g"
4) "r"
5) "t"
6) "h"
7) "q"
8) "e"
127.0.0.1:6379> SPOP myset3
"q"
127.0.0.1:6379> SMEMBERS myset3
1) "f"
2) "w"
3) "g"
4) "r"
5) "t"
6) "h"
7) "e"

返回多个集合的交集

SINTER

127.0.0.1:6379> SADD myset4 1 2 3 4 5
(integer) 5
127.0.0.1:6379> SADD myset5 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SADD myset6 5 6 7 8 9
(integer) 5
127.0.0.1:6379> SINTER myset4 myset5 myset6
1) "5"

将多个集合的交集结果保存为一个新的集合

SINTERSTORE

127.0.0.1:6379> SADD myset2 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SADD myset3 5 6 7 8 9
(integer) 5
127.0.0.1:6379> SINTERSTORE mysetnew myset2 myset3
(integer) 3
127.0.0.1:6379> SMEMBERS mysetnew
1) "5"
2) "6"
3) "7"
127.0.0.1:6379> SMEMBERS myset2
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379> SMEMBERS myset3
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"

返回多个集合的并集

SUNION

127.0.0.1:6379> SADD myset4 1 2 3 4 5
(integer) 5
127.0.0.1:6379> SADD myset5 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SADD myset6 5 6 7 8 9
(integer) 5
127.0.0.1:6379> SUNION myset4 myset5 myset6
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"

将多个集合的并集的结果保存为一个新的集合

SUNIONSTORE

127.0.0.1:6379> SADD myset2 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SADD myset3 5 6 7 8 9
(integer) 5
127.0.0.1:6379> SUNIONSTORE mysetnew myset2 myset3
(integer) 7
127.0.0.1:6379> SMEMBERS mysetnew
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
6) "8"
7) "9"
127.0.0.1:6379> SMEMBERS myset2
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379> SMEMBERS myset3
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"

返回多个集合的差集

以最左边的为主集合,返回差集

SDIFF

127.0.0.1:6379> SMEMBERS myset2
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379> SMEMBERS myset3
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
127.0.0.1:6379> SDIFF myset2 myset3
1) "3"
2) "4"
127.0.0.1:6379> SDIFF myset3 myset2
1) "8"
2) "9"

将多个集合的差集的结果保存为一个新的集合

SDIFFSTORE

127.0.0.1:6379> SDIFF myset2 myset3
1) "3"
2) "4"
127.0.0.1:6379> SDIFFSTORE mysetnew myset2 myset3
(integer) 2
127.0.0.1:6379> SMEMBERS mysetnew
1) "3"
2) "4"

标签:127.0,0.1,数据类型,Redis,无序,6379,key,integer,SMEMBERS
From: https://www.cnblogs.com/c-moon/p/16797211.html

相关文章

  • spark通过pipline方式批量插入redis集群方式
    spark通过pipline方式批量插入redis集群网上资料比较少,但是有一大堆都是单机的方式,spring倒是也有写入redis集群的实现代码,以下整理了spark通过pipline批量写入的方式,速度......
  • 17.Redis之分布式锁
    参考1:https://www.cnblogs.com/wangyingshuo/p/14510524.html参考2: https://blog.csdn.net/Me_xuan/article/details/124418176参考3:https://blog.csdn.net/a745233700......
  • 18.Redis常见的面试题
    参考1:https://lebron.blog.csdn.net/article/details/121456167参考2:https://lebron.blog.csdn.net/article/details/120817994......
  • 3.数据类型扩展及面试题讲解
    数据类型扩展及面试题讲解整数扩展://进制 二进制 十进制 八进制0 十六进制inti=10;inti2=010;//八进制0inti3=0x10;//十六进制0x0~9A~F16浮点......
  • redis的发布订阅模式
    redis的发布订阅模式redis发布订阅(pub/sub)是一种消息通信模式,消息的发布者不会将消息发送给特定的订阅者,而是通过消息通道(频道)广播出去,让订阅该消息主题(频道)的订阅......
  • Python面试-数据类型面试题
    1.元祖类面试题tup1=(10)tup2=(10,)tup3=()print(type(tup1),type(tup2),type(tup3))"""输出结果:<class'int'><class'tuple'><class'tuple'>""" ......
  • Redis 实现分布式锁
    Redis实现分布式锁JVM层面的加锁(synchronized,ReentraLock) 单机版的锁分布式微服务架构中,为了避免各个微服务之间发生冲突和数据故障从而引入一种锁--分布式锁......
  • redis: hash类型
    Hash类型,也叫散列,其value是一个无序字典,类似于Java中的HashMap结构。String结构是将对象序列化为JSON字符串后存储,当需要修改对象某个字段时很不方便:  Hash结构可以......
  • 第4章 C++ STL无序关联式容器总结
    除了序列式容器和关联式容器之外,C++11标准库又引入了一类容器,即无序关联式容器。 无序关联式容器,又称哈希容器。和关联式容器一样,此类容器存储的也是键值对元素;不同之......
  • redis:set类型
    Redis的Set结构与Java中的HashSet类似,可以看做是一个value为null的HashMap。因为也是一个hash表,因此具备与HashSet类似的特征:无序元素不可重复查找快支持交集......