首页 > 编程笔记 > Redis笔记 阅读:4

Redis集合操作命令大全(附带实例)

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

集合对象的编码可以是整数集合或者哈希表。

Redis 中集合是通过哈希表实现的,所以添加、删除、查找等操作的复杂度都是 O(1)。集合中最大的成员数为 2^32- 1。

Redis集合基本操作

1) 返回所有给定集合的并集

SUNION 命令的作用是返回给定集合的并集。不存在的集合被视为空集。

SUNION 命令的语法格式如下:
SUNION key key1 . keyn
SUNION 命令返回并集成员的列表。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "world"
(integer) 1
127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
127.0.0.1:6379> SADD myset2 "bar"
(integer) 1
127.0.0.1:6379> SUNION myset1 myset2
"bar"
"world"
"hello"

2) 获取集合的成员数

SCARD 命令的作用是返回集合中元素的数量。

SCARD 命令的语法格式如下:
SCARD key_name
SCARD 命令返回集合的数量。当集合 key 不存在时,返回 0。

实例:
127.0.0.1:6379> SADD myset "hello"# 在集合myset中添加hello
(integer) 1
127.0.0.1:6379> SADD myset "foo"  # 在集合myset中添加foo
(integer) 1
127.0.0.1:6379> SADD myset "hello"# 在集合myset中添加hello
(integer) 0
127.0.0.1:6379> SCARD myset       # 获取集合myset中元素的个数
(integer) 2

3) 返回集合中一个或多个随机元素

SRANDMEMBER 命令的作用是返回集合中的一个或多个随机元素。

SRANDMEMBER 命令的语法格式如下:
SRANDMEMBER key[count]
只提供集合名时,SRANDMEMBER 命令返回一个元素;如果集合为空,返回 nil。如果命令中设置了 count 参数,那么返回一个数组;如果集合为空,返回空数组。

SRANDMEMBER 命令接收的 count 参数如下:
SRANDMEMBER 命令和 SPOP 命令相似,只不过 SPOP 命令将随机元素从集合中移除并返回,而 SRANDMEMBER 仅仅返回随机元素,不对集合进行任何改动。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "world"
(integer) 1
127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
127.0.0.1:6379> SRANDMEMBER myset1
"bar"
127.0.0.1:6379> SRANDMEMBER myset1 2
"Hello"
"world"

4) 返回集合中的所有成员

SMEMBERS 命令的作用是返回集合中的所有成员。不存在的集合名被视为空集合。

SMEMBERS 命令的语法格式如下:
SMEMBERS key value
SMEMBERS 命令返回集合中的所有成员。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "world"
(integer) 1
127.0.0.1:6379> SMEMBERS myset1
"World"
"Hello"

5) 返回所有给定集合的交集

SINTER 命令的作用是返回所有给定集合的交集。不存在的集合名被视为空集。当给定集合当中有一个空集时,返回结果也为空集(根据集合运算定律)。

SINTER 命令的语法格式如下:
SINTER key key1 . keyn
SINTER 命令返回交集成员的列表。

实例:
127.0.0.1:6379> SADD myset "hello"
(integer) 1
127.0.0.1:6379> SADD myset "foo"
(integer) 1
127.0.0.1:6379> SADD myset "bar"
(integer) 1
127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
127.0.0.1:6379> SADD myset2 "world"
(integer) 1
127.0.0.1:6379> SINTER myset myset2
"hello"

6) 移除集合中的一个或多个成员元素

SREM 命令的作用是移除集合中的一个或多个成员元素,不存在的成员元素会被忽略。

SREM 命令的语法格式如下:
SREM key member1 … membern
SREM 命令返回被成功移除的元素的数量,该数不包括被忽略的元素的数量。当 key 不是集合类型时,返回一个错误信息。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "world"
(integer) 1
127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
127.0.0.1:6379> SREM myset1 "hello"
(integer) 1
127.0.0.1:6379> SREM myset1 "foo"
(integer) 0
127.0.0.1:6379> SMEMBERS myset1
"bar"
"world"

7) 将成员元素从源(source)集合移动到目的(destination)集合

SMOVE 命令的作用是将指定的成员元素从 source 集合移动到 destination 集合。SMOVE 命令的操作是原子性操作。

如果 source 集合不存在或不包含指定的成员元素,则 SMOVE 命令不执行任何操作,仅返回 0。否则,成员元素被从 source 集合中移除,并添加到 destination 集合中去。

当 destination 集合已经包含成员元素时,SMOVE 命令只是简单地将 source 集合中的成员元素删除。

当 source 或 destination 不是集合类型时,则返回一个错误信息。

SMOVE 命令的语法格式如下:
SMOVE source destination member
如果成员元素被成功移除,SMOVE 命令返回 1。如果成员元素不是 source 集合的成员,并且没有任何操作对 destination 集合执行,那么返回 0。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "world"
(integer) 1
127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
127.0.0.1:6379> SADD myset2 "foo"
(integer) 1
127.0.0.1:6379> SMOVE myset1 myset2 "bar"
(integer) 1
127.0.0.1:6379> SMEMBERS myset1
"World"
"Hello"
127.0.0.1:6379> SMEMBERS myset2
"foo"
"bar"

8) 向集合添加一个或多个成员元素

SADD 命令的作用是将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。

假如集合名不存在,则创建一个只包含添加的成员元素的集合。

SADD 命令的语法格式如下:
SADD key_name value1 . valueN
SADD 命令返回被添加到集合中的新成员元素的数量,该数不包括被忽略的元素的数量。当集合名不是集合类型时,返回一个错误信息。

实例:
127.0.0.1:6379> SADD myset "hello"
(integer) 1
127.0.0.1:6379> SADD myset "foo"
(integer) 1
127.0.0.1:6379> SADD myset "hello"
(integer) 0
127.0.0.1:6379> SMEMBERS myset
"hello"
"foo"

9) 判断成员元素是否为集合的成员

SISMEMBER 命令的作用是判断成员元素是否为集合的成员。

SISMEMBER 命令的语法格式如下:
SISMEMBER key value
如果成员元素是集合的成员,SISMEMBER 命令返回 1。如果成员元素不是集合的成员,或集合 key 不存在,则返回 0。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SISMEMBER myset1 "hello"
(integer) 1
127.0.0.1:6379> SISMEMBER myset1 "world"
(integer) 0

10) 返回所有给定集合的差集并存储在destination集合中

SDIFFSTORE 命令的作用是将给定集合之间的差集存储在 destination 集合,即指定的集合中。如果 destination 集合中 key 已存在,则其元素会被覆盖。

SDIFFSTORE 命令的语法格式如下:
SDIFFSTORE destination_key key1 … keyn
SDIFFSTORE 命令返回 destination 集合中的元素数量。

实例:
127.0.0.1:6379> SADD myset "hello"
(integer) 1
127.0.0.1:6379> SADD myset "foo"
(integer) 1
127.0.0.1:6379> SADD myset "bar"
(integer) 1
127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
127.0.0.1:6379> SADD myset2 "world"
(integer) 1
127.0.0.1:6379> SDIFFSTORE destset myset myset2
(integer) 2
127.0.0.1:6379> SMEMBERS destset
"foo"
"bar"

11) 返回所有给定集合的差集

SDIFF 命令的作用是返回给定集合之间的差集。不存在的集合 key 将被视为空集。

SDIFF 命令的语法格式如下:
SDIFF first_key other_key1 … other_keyn
SDIFF 命令返回包含差集成员元素的列表。

实例:
127.0.0.1:6379> SADD myset "hello"
(integer) 1
127.0.0.1:6379> SADD myset "foo"
(integer) 1
127.0.0.1:6379> SADD myset "bar"
(integer) 1
127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
127.0.0.1:6379> SADD myset2 "world"
(integer) 1
127.0.0.1:6379> SDIFF myset myset2
"foo"
"bar"

12) 迭代集合键中的成员元素

SSCAN 命令的作用是迭代集合键中的元素。

SSCAN 命令的语法格式如下:
SSCAN key [MATCH pattern] [COUNT count]
SSCAN 命令返回数组列表。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "hi"
(integer) 1
127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
127.0.0.1:6379> sscan myset1 0 MATCH h*
"0"
"hello"
"h1"

13) 返回所有给定集合的交集并存储在destination集合中

SINTERSTORE 命令的作用是将给定集合之间的交集存储在 destination 集合,即指定的集合中。如果指定的集合已经存在,则其成员元素被覆盖。

SINTERSTORE 命令的语法格式如下:
SINTERSTORE destination_key key key1 … keyn
SINTERSTORE 命令返回交集成员的列表。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "foo"
(integer) 1
127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
127.0.0.1:6379> SADD myset2 "world"
(integer) 1
127.0.0.1:6379> SINTERSTORE myset myset1 myset2
(integer) 1
127.0.0.1:6379> SMEMBERS myset
"hello"

14) 将所有给定集合的并集存储在destination集合中

SUNIONSTORE 命令的作用是将给定集合的并集存储在 destination 集合,即指定的集合中。

SUNIONSTORE 命令的语法格式如下:
SUNIONSTORE destination key key1 … keyn
SUNIONSTORE 命令返回结果集中的元素数量

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "world"
(integer) 1
127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
127.0.0.1:6379> SADD myset2 "bar"
(integer) 1
127.0.0.1:6379> SUNIONSTORE myset myset1 myset2
(integer) 1
127.0.0.1:6379> SMEMBERS myset
"bar"
"world"
"hello"

15) 移除并返回集合中的一个随机成员元素

SPOP 命令的作用是移除并返回集合中的一个随机成员元素。

SPOP 命令的语法格式如下:
SPOP key
SPOP 命令返回被移除的随机成员元素。当集合不存在或是空集时,则返回 nil。

实例:
127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
127.0.0.1:6379> SADD myset1 "world"
(integer) 1
127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
127.0.0.1:6379> SPOP myset1
"bar"
127.0.0.1:6379> SMEMBERS myset1
"hello"
"world"

相关文章