九、Redis集合类型参考记录

作者: 温新

分类: 【Redis】

阅读: 2032

时间: 2020-09-01 15:15:54

集合(Set)中每个元素都是不同的,且没有顺序。一个集合类型(Set)键可以存储至多232-1个字符串。

集合类型的常用操作是向集合中加入或删除元素、判断某个元素是否存在等,由于集合类型在Redis内部是使用值为空的哈希表实现的,因此这些操作的时间复杂度都是O(1)。最为方便的是集合类型之间可以进行并集、交集、差集运算。

集合类型与列表类型的对比

集合类型基本命令

sadd 向集合中添加一个或多个元素

语法:sadd key member1 [member2 ...]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sadd letters a</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(103, 110, 149)"># 因为a已经存在,所以不会添加</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sadd letters a b c</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span></span>

集合中不能有相同的元素,加入的元素若有重复的,就会忽略这个重复的元素。该命令返回成功加入的元素数量。

smembers 获取集合中所有元素

语法:smembers key

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> smembers letters</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"c"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"b"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"a"</span></span>

srem 从集合中删除一个或多个元素

语法:srem key member1 [member2 ...]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(103, 110, 149)">#  由于元素d不在集合中,所以只删除一个存在的元素</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> srem letters c d</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span></span>

sismember 判断元素是否在集合中

语法:sismember key member

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sismember letters e</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">0</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sismember letters a</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span></span>

集合间的运算

sdiff 对多个集合执行差集运算

语法:sdiff key1 [key2 ...]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sadd setA <span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sadd setB <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">4</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sdiff setA setB</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"1"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sdiff setB setA</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"4"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sadd setC <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sdiff setA setB setC</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"1"</span></span>

setA setB setC差集计算:先算setA - setB,再计算与setC的差集

sinter 对多个集合执行交集运算

语法:sinter key1 [key2 ...]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sinter setA setB</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"2"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"3"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sinter setA setB setC</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"2"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"3"</span></span>

sunion 对多个集合执行并集运算

语法:sunion key1 [key2 ...]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sunion setA setB</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"1"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"2"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"3"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">4</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"4"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sunion setA setB setC</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"1"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"2"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"3"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">4</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"4"</span></span>

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

语法:sdiffsore destination key1 [key2 ...]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sdiffstore new_set setA setB</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span></span>

将给定集合之间的差集存储在指定的集合中。如果指定的集合 key 已存在,则会被覆盖。

sinterstore 返回所有集合的交集并存储在destination中

语法:sinterstore destination key1 [key2 ...]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sinterstore new_set1 setA setB</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> smembers new_set1</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"2"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"3"</span></span>

sunionstore 所有给定集合的并集存储在destinationkh

语法:sunionstore destination key1 [key2 ...]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sunionstore setUnionJi setA setB</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">4</span></span>

其他命令

scard 获取集合中成员数量

语法:scard key

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> scard setA</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span></span>

smove 将member元从source集合移到destination集合

语法:smove source destination member

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sadd name lisi wangwu</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sadd age <span style="box-sizing: border-box;color: rgb(255, 83, 112)">17</span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">18</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> smove age name <span style="box-sizing: border-box;color: rgb(255, 83, 112)">17</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> smembers name</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"lisi"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"17"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"wangwu"</span></span>

spop 移除并返回集合中的一个随机元素

语法:spop key [count]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> spop name</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(195, 232, 141)">"lisi"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> spop name</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(195, 232, 141)">"wangwu"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(103, 110, 149)"># 移除3个元素</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> spop new_set1 <span style="box-sizing: border-box;color: rgb(255, 83, 112)">3</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"2"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"3"</span></span>

srandmember 返回集合中一个或多个随机数

语法:srandmember key [count]

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> srandmember setC</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(195, 232, 141)">"2"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> srandmember setC</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(195, 232, 141)">"3"</span></span>

若count为整数,且小于集合基数,那么返回一个包含count个元素的数组,数组中的元素各不相同,若count大于等于集合基数,那么返回整个集合;

若count为负数,那么返回一个数组,数组中的元素可能会重复出现多次,数组长度为count的绝对值

sscan 迭代集合中的元素

语法:sscan key cursor [MATCH paatrn] [COUNT count]

cursor 游标

pattern 匹配模式

count 指定从数据集里返回多少元素,默认值为 10

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> del name</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">(integer) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">127</span>.0.0.1:6379> sscan name <span style="box-sizing: border-box;color: rgb(255, 83, 112)">0</span> match li*</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"0"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(255, 83, 112)">1</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"lisi"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">   <span style="box-sizing: border-box;color: rgb(255, 83, 112)">2</span>) <span style="box-sizing: border-box;color: rgb(195, 232, 141)">"lisan"</span></span>


我是夕阳何处寻,期待和优秀的你一起同行!

小白

2020年09月01日

请登录后再评论