set

  1. set 类似go的map,但是value值是struct{}。它具备以下特征:
    • 无序、元素不可重复、查找快、支持交集.并集.差集等功能

SADD

  1. 向set添加一个或多个成员。如果key不存在,则创建该key。

  2. 语法:

SADD key member [member ...]
  1. 示例:
redis> SADD myset "Hello"
(integer) 1
redis> SADD myset "World"
(integer) 1
redis> SADD myset "World"
(integer) 0
redis> SMEMBERS myset
1) "Hello"
2) "World"

SCARD

  1. 返回set中成员的数目。

  2. 语法:

SCARD key
  1. 示例:
redis> SADD myset "Hello"
(integer) 1
redis> SADD myset "World"
(integer) 1
redis> SCARD myset
(integer) 2

SDIFF

  1. 返回多个set的差值。

  2. 语法:

SDIFF key [key ...]
  1. 示例:
redis> SADD key1 "a"
(integer) 1
redis> SADD key1 "b"
(integer) 1
redis> SADD key1 "c"
(integer) 1
redis> SADD key2 "c"
(integer) 1
redis> SADD key2 "d"
(integer) 1
redis> SADD key2 "e"
(integer) 1
redis> SDIFF key1 key2
1) "a"
2) "b"

SDIFFSTORE

  1. 将多个set的差异存储在一个键中。

  2. 语法:

SDIFFSTORE destination key [key ...]
  1. 示例:
redis> SADD key1 "a"
(integer) 1
redis> SADD key1 "b"
(integer) 1
redis> SADD key1 "c"
(integer) 1
redis> SADD key2 "c"
(integer) 1
redis> SADD key2 "d"
(integer) 1
redis> SADD key2 "e"
(integer) 1
redis> SDIFFSTORE key key1 key2
(integer) 2
redis> SMEMBERS key
1) "a"
2) "b"

SINTER

  1. 返回多个set的交点。

  2. 语法:

SINTER key [key ...]
  1. 示例:
redis> SADD key1 "a"
(integer) 1
redis> SADD key1 "b"
(integer) 1
redis> SADD key1 "c"
(integer) 1
redis> SADD key2 "c"
(integer) 1
redis> SADD key2 "d"
(integer) 1
redis> SADD key2 "e"
(integer) 1
redis> SINTER key1 key2
1) "c"

SINTERCARD

  1. 返回多个set相交的成员数。

  2. 语法:

SINTERCARD numkeys key [key ...] [LIMIT limit]
  1. 示例:
redis> SADD key1 "a"
(integer) 1
redis> SADD key1 "b"
(integer) 1
redis> SADD key1 "c"
(integer) 1
redis> SADD key1 "d"
(integer) 1
redis> SADD key2 "c"
(integer) 1
redis> SADD key2 "d"
(integer) 1
redis> SADD key2 "e"
(integer) 1
redis> SINTER key1 key2
1) "c"
2) "d"
redis> SINTERCARD 2 key1 key2
(integer) 2
redis> SINTERCARD 2 key1 key2 LIMIT 1
(integer) 1

SINTERSTORE

  1. 在一个key中存储多个set的相交。

  2. 语法:

SINTERSTORE destination key [key ...]
  1. 示例:
redis> SADD key1 "a"
(integer) 1
redis> SADD key1 "b"
(integer) 1
redis> SADD key1 "c"
(integer) 1
redis> SADD key2 "c"
(integer) 1
redis> SADD key2 "d"
(integer) 1
redis> SADD key2 "e"
(integer) 1
redis> SINTERSTORE key key1 key2
(integer) 1
redis> SMEMBERS key
1) "c"

SISMEMBER

  1. 确定成员是否属于set。

  2. 语法:

SISMEMBER key member
  1. 示例:
redis> SADD myset "one"
(integer) 1
redis> SISMEMBER myset "one"
(integer) 1
redis> SISMEMBER myset "two"
(integer) 0

SMEMBERS

  1. 返回set的所有成员。

  2. 语法:

SMEMBERS key
  1. 示例:
redis> SADD myset "Hello"
(integer) 1
redis> SADD myset "World"
(integer) 1
redis> SMEMBERS myset
1) "Hello"
2) "World"

SMISMEMBER

  1. 确定多个成员是否属于一个set。

  2. 语法:

SMISMEMBER key member [member ...]
  1. 示例:
redis> SADD myset "one"
(integer) 1
redis> SADD myset "one"
(integer) 0
redis> SMISMEMBER myset "one" "notamember"
1) (integer) 1
2) (integer) 0

SMOVE

  1. 将一个成员从一个set移动到另一个set。

  2. 语法:

SMOVE source destination member
  1. 示例:
redis> SADD myset "one"
(integer) 1
redis> SADD myset "two"
(integer) 1
redis> SADD myotherset "three"
(integer) 1
redis> SMOVE myset myotherset "two"
(integer) 1
redis> SMEMBERS myset
1) "one"
redis> SMEMBERS myotherset
2) "three"
3) "two"

SPOP

  1. 从set中删除一个或多个随机成员后返回它们。如果最后一个成员被弹出,则删除该set。

  2. 语法:

SPOP key [count]
  1. 示例:
redis> SADD myset "one"
(integer) 1
redis> SADD myset "two"
(integer) 1
redis> SADD myset "three"
(integer) 1
redis> SPOP myset
"one"
redis> SMEMBERS myset
1) "two"
2) "three"
redis> SADD myset "four"
(integer) 1
redis> SADD myset "five"
(integer) 1
redis> SPOP myset 3
3) "two"
4) "three"
5) "four"
redis> SMEMBERS myset
6) "five"

SRANDMEMER

  1. 从set中获取一个或多个随机成员

  2. 语法:

SRANDMEMBER key [count]
  1. 示例:
redis> SADD myset one two three
(integer) 3
redis> SRANDMEMBER myset
"two"
redis> SRANDMEMBER myset 2
1) "two"
2) "three"
redis> SRANDMEMBER myset -5
3) "three"
4) "one"
5) "three"
6) "two"
7) "three"

SREM

  1. 从set中删除一个或多个成员。如果最后一个成员被删除,则删除该set。

  2. 语法:

SREM key member [member ...]
  1. 示例:
redis> SADD myset "one"
(integer) 1
redis> SADD myset "two"
(integer) 1
redis> SADD myset "three"
(integer) 1
redis> SREM myset "one"
(integer) 1
redis> SREM myset "four"
(integer) 0
redis> SMEMBERS myset
1) "two"
2) "three"

SSCAN

  1. 遍历set的成员。

  2. 语法:

SSCAN key cursor [MATCH pattern] [COUNT count]

SUNION

  1. 返回多个set的并集。

  2. 语法:

SUNION key [key ...]
  1. 示例:
redis> SADD key1 "a"
(integer) 1
redis> SADD key1 "b"
(integer) 1
redis> SADD key1 "c"
(integer) 1
redis> SADD key2 "c"
(integer) 1
redis> SADD key2 "d"
(integer) 1
redis> SADD key2 "e"
(integer) 1
redis> SUNION key1 key2
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"

SUNIONSTORE

  1. 将多个set的并集存储在一个key中。

  2. 语法:

SUNIONSTORE destination key [key ...]
  1. 示例:
redis> SADD key1 "a"
(integer) 1
redis> SADD key1 "b"
(integer) 1
redis> SADD key1 "c"
(integer) 1
redis> SADD key2 "c"
(integer) 1
redis> SADD key2 "d"
(integer) 1
redis> SADD key2 "e"
(integer) 1
redis> SUNIONSTORE key key1 key2
(integer) 5
redis> SMEMBERS key
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"