redis set类型

Redis 的 set 是 string 类型的无序集合。set 的是通过 hash table 实现的,所以添加、删除和查找的复杂度都是 O(1)。set 集合类型除了基本的添加删除操作,其他有用的操作还包含集合的取并集(union),交集(intersection),差集(difference)。

1 sadd

向名称为 key 的 set 中添加元素

127.0.0.1:6379> sadd set hello

(integer) 1

127.0.0.1:6379> sadd set world

(integer) 1

127.0.0.1:6379> sadd set world

(integer) 0

查看set元素

127.0.0.1:6379> smembers set

1) "hello"

2) "world"

2 srem

删除名称为 key 的 set 中的元素 member

127.0.0.1:6379> smembers set

1) "hello"

2) "world"

127.0.0.1:6379> srem set world

(integer) 1

127.0.0.1:6379> smembers set

1) "hello"

3 spop

随机返回并删除名称为 key 的 set 中一个元素

127.0.0.1:6379> smembers set

1) "three"

2) "zero"

3) "two"

4) "one"

5) "four"

127.0.0.1:6379> spop set

"zero"

127.0.0.1:6379> spop set

"three"

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

4 sdiff

回所有给定 key 与第一个 key 的差集(第一个)

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> smembers set1

1) "three"

2) "two"

3) "one"

127.0.0.1:6379> sdiff set set1

1) "four"

127.0.0.1:6379> sdiff set1 set

1) "three"

5 sdiffstore

返回所有给定 key 与第一个 key 的差集,并将结果存为另一个 key

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> smembers set1

1) "three"

2) "two"

3) "one"

127.0.0.1:6379> sdiffstore set2 set set1

(integer) 1

127.0.0.1:6379> smembers set2

1) "four"

6 sinter

返回所有给定 key 的交集

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> smembers set1

1) "three"

2) "two"

3) "one"

127.0.0.1:6379> sinter set set1

1) "two"

2) "one"

7 sinterstore

返回所有给定 key 的交集,并将结果存为另一个 key

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> smembers set1

1) "three"

2) "two"

3) "one"

127.0.0.1:6379> sinterstore set2 set set1

(integer) 2

127.0.0.1:6379> smembers set2

1) "one"

2) "two"

8 sunion

返回所有给定 key 的并集

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> smembers set1

1) "three"

2) "two"

3) "one"

127.0.0.1:6379> sunion set set1

1) "two"

2) "one"

3) "four"

4) "three"

9 sunionstore

返回所有给定 key 的并集,并将结果存为另一个 key

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> smembers set1

1) "three"

2) "two"

3) "one"

127.0.0.1:6379> sunionstore set2 set set1

(integer) 4

127.0.0.1:6379> smembers set2

1) "two"

2) "one"

3) "four"

4) "three"

10 smove

从第一个 key 对应的 set 中移除 member 并添加到第二个对应 set 中

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> smove set set2 one

(integer) 1

127.0.0.1:6379> smembers set2

1) "one"

11 scard

返回名称为 key 的 set 的元素个数

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> scard set

(integer) 3

12 sismember

测试 member 是否是名称为 key 的 set 的元素

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> scard set

(integer) 3

127.0.0.1:6379> sismember set one

(integer) 1

127.0.0.1:6379> sismember set three

(integer) 0

13 srandmember

随机返回名称为 key 的 set 的一个元素,但是不删除元素

127.0.0.1:6379> smembers set

1) "four"

2) "one"

3) "two"

127.0.0.1:6379> srandmember set

"one"

你可能感兴趣的:(redis set类型)