【redis】set命令简述、及基于set随机抽取的抽奖

set是redis数据类型的一种。

set的命令如下

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

  SINTERCARD numkeys key [key ...] [LIMIT limit]
  summary: Intersect multiple sets and return the cardinality of the result
  since: 7.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

  SMISMEMBER key member [member ...]
  summary: Returns the membership associated with the given elements for a set
  since: 6.2.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:sadd key value1 value2 value3…,向key对应的set中添加value1,value2…
smembers :smembers key,返回key对应的set集合中的所有元素
srem:srem key value1 value2,删除key对应的set集合中的value1, value2

127.0.0.1:6379> sadd k1 a a b c d e e ee
(integer) 6
127.0.0.1:6379> smembers k1
1) "b"
2) "c"
3) "e"
4) "ee"
5) "a"
6) "d"
127.0.0.1:6379> srem k1 b c
(integer) 2
127.0.0.1:6379> smembers k1
1) "e"
2) "ee"
3) "a"
4) "d"

交集、并集、差集

sinter:sinter key1 key2,返回key1和key2的交集
sinterstore:sinterstore destkey key1 key2 ,将key1 key2的交集保存在destkey中。

127.0.0.1:6379> sadd k2 e  a s ee c
(integer) 5
127.0.0.1:6379> sadd k3 ee a k j
(integer) 4
127.0.0.1:6379> sinter k2 k3
1) "a"
2) "ee"
127.0.0.1:6379> sinterstore k4 k2 k3
(integer) 2
127.0.0.1:6379> smembers k4
1) "ee"
2) "a"


sunion:sunion key1 key2,返回key1和key2的并集
sunionstore:sunionstore destkey key1 key2,将key1 key2的并集保存在destkey中

127.0.0.1:6379> sunion k2 k3
1) "e"
2) "s"
3) "c"
4) "a"
5) "j"
6) "ee"
7) "k"
127.0.0.1:6379> sunionstore k5 k2 k3
(integer) 7
127.0.0.1:6379> smembers k5
1) "e"
2) "s"
3) "c"
4) "a"
5) "j"
6) "ee"
7) "k"

sdiff:sdiff key1 key2,返回key1 key2对应的两个set中key1中存在的不同的部分。想要哪个key中的不同部分,就将其保存在前面。
sdiffstore:sdiffstore destkey key1 key2,同上,将key1中的差集保存在destkey中

127.0.0.1:6379> smembers k2
1) "e"
2) "s"
3) "c"
4) "a"
5) "ee"
127.0.0.1:6379> smembers k3
1) "j"
2) "a"
3) "ee"
4) "k"
127.0.0.1:6379> sdiff k2 k3
1) "c"
2) "s"
3) "e"
127.0.0.1:6379> sdiff k3 k2
1) "j"
2) "k"

随机集合

srandommember:srandommember key num,如果num大于0,返回小于等于num个数的子集合,子集合的元素不能重复,如果集合元素的个数小于num,那么返回的子集合元素的个数会小于num,既集合元素的个数;如果num小于0,返回等于num个数的子集合,元素可以重复出现

127.0.0.1:6379> srandmember k1 -10
 1) "ee"
 2) "ee"
 3) "ee"
 4) "a"
 5) "ee"
 6) "a"
 7) "ee"
 8) "ee"
 9) "a"
10) "a"
127.0.0.1:6379> SRANDMEMBER k1 10
1) "ee"
2) "a"

srandommember可以用于同时抽取一批用户,set中的元素应为用户id之类的唯一标识,可以满足重复用户/用户不能重复的场景

spop:spop key,弹出并返回key对应的集合中的一个随机元素

127.0.0.1:6379> spop k2
"ee"
127.0.0.1:6379> spop k2
"c"
127.0.0.1:6379> spop k2
"s"

spop命令可以用于间隔一段时间规律的抽取的情景,如挨个抽取一位用户,或每抽取一次用户就停止一段时间。这种场景srandommember无法满足。

你可能感兴趣的:(Redis,redis,java,前端)