欢迎来到我的博客园

Redis set类型

Redis set类型

redis set类型是一组无序的唯一的String集合。我们可以利用set来

跟踪独立条目()

展现关联关系(例如共同好友)

正常的集合操作

 

set常用命令

sadd 命令

sadd命令会将一个元素加入到set中,如set不存在则创建该set。

127.0.0.1:6379> help sadd

  SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0
  group: set

127.0.0.1:6379> exists cr:by:sadd
(integer) 0
127.0.0.1:6379> sadd cr:by:sadd test0
(integer) 1
127.0.0.1:6379> exists cr:by:sadd
(integer) 1
127.0.0.1:6379> smembers cr:by:sadd
1) "test0"

127.0.0.1:6379> sadd cr:by:sadd test1 test2 test3
(integer) 3
127.0.0.1:6379> smembers cr:by:sadd
1) "test2"
2) "test1"
3) "test3"
4) "test0"

 

smembers命令

smembers命令可以获取一个set中的所有元素。如果不存在会返回empty array。

127.0.0.1:6379> help smembers

  SMEMBERS key
  summary: Get all the members in a set
  since: 1.0.0
  group: set

127.0.0.1:6379> smembers not:exists
(empty array)
127.0.0.1:6379> smembers cr:by:sadd
1) "test2"
2) "test1"
3) "test3"
4) "test0"

 

srem命令

srem命令会删除set中指定的元素。如果该元素不存在则返回0,存在返回删除的元素个数。

127.0.0.1:6379> help srem

  SREM key member [member ...]
  summary: Remove one or more members from a set
  since: 1.0.0
  group: set

127.0.0.1:6379> srem cr:by:sadd non:exists
(integer) 0
127.0.0.1:6379> srem cr:by:sadd test0
(integer) 1
127.0.0.1:6379> smembers cr:by:sadd
1) "test2"
2) "test1"
3) "test3"

127.0.0.1:6379> srem cr:by:sadd test1 test2 test3
(integer) 3
127.0.0.1:6379> smembers cr:by:sadd
(empty array)

 

spop命令

spop命令随机从set中删除几个元素,并返回删除的元素值。如果该set不存在,则返回nil

127.0.0.1:6379> help spop
  SPOP key [count]
  summary: Remove and return one or multiple random members from a set
  since: 1.0.0
  group: set
127.0.0.1:6379> sadd cr:by:sadd test0 test1 test2 test3 test4
(integer) 5
127.0.0.1:6379> smembers cr:by:sadd
1) "test2"
2) "test1"
3) "test3"
4) "test0"
5) "test4"
127.0.0.1:6379> spop cr:by:sadd
"test1"
127.0.0.1:6379> spop cr:by:sadd 2
1) "test3"
2) "test2"
127.0.0.1:6379> spop no:exists
(nil)

 

sismember命令

sismember命令判断set 是否存在该元素。set中存在该元素则返回1,如果不存在该set或该set中没有该元素,则返回0。

127.0.0.1:6379> help sismember
  SISMEMBER key member
  summary: Determine if a given value is a member of a set
  since: 1.0.0
  group: set

127.0.0.1:6379> smembers cr:by:sadd
1) "test0"
2) "test4"
127.0.0.1:6379> sismember cr:by:sadd test0
(integer) 1
127.0.0.1:6379> sismember cr:by:sadd test2
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379> smembers no:exists
(empty array)
127.0.0.1:6379> sismember no:exists nil
(integer) 0

 

scard命令

scard命令获取一个set中元素个数。如果set不存在返回0。如果set存在,则返回set中元素个数。

127.0.0.1:6379> help scard

  SCARD key
  summary: Get the number of members in a set
  since: 1.0.0
  group: set

127.0.0.1:6379> scard no:exists
(integer) 0
127.0.0.1:6379> scard cr:by:sadd
(integer) 2
127.0.0.1:6379> smembers cr:by:sadd
1) "test0"
2) "test4"
127.0.0.1:6379> smembers no:exists
(empty array)
127.0.0.1:6379>

 

srandmember命令

srandmember随机返回set中的某个或某count个元素

127.0.0.1:6379> help srandmember

  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0
  group: set

127.0.0.1:6379> smembers randset
1) "test3"
2) "test0"
3) "test5"
4) "test1"
5) "test2"
6) "test4"

127.0.0.1:6379> srandmember randset
"test3"
127.0.0.1:6379> srandmember randset
"test4"
127.0.0.1:6379> srandmember randset 2
1) "test0"
2) "test3"

127.0.0.1:6379> srandmember randset 2
1) "test0"
2) "test4"
127.0.0.1:6379>

  

smove命令

smove命令 将source set中的某个元素移动到dest set中。如果元素不存在与source set中,则返回0,没有做任何修改。

127.0.0.1:6379> help smove

  SMOVE source destination member
  summary: Move a member from one set to another
  since: 1.0.0
  group: set

127.0.0.1:6379> smembers source
1) "mem2"
2) "mem1"
3) "mem0"
4) "mem3"
127.0.0.1:6379> smembers dest
1) "mem5"
2) "mem4"
3) "mem6"
127.0.0.1:6379> smove source dest mem0
(integer) 1

127.0.0.1:6379> smembers dest
1) "mem5"
2) "mem4"
3) "mem0"
4) "mem6"
127.0.0.1:6379> smembers source
1) "mem2"
2) "mem1"
3) "mem3"

127.0.0.1:6379> smove source dest non-ele
(integer) 0
127.0.0.1:6379>

 

sdiff命令

获取当前set中在其他set不存在的元素,如果当前set为空,则返回empty array,如果当前set中元素在其他set中存在,也返回empty array。如果有不在其他set中存在的元素,则返回该元素。

127.0.0.1:6379> help sdiff

  SDIFF key [key ...]
  summary: Subtract multiple sets
  since: 1.0.0
  group: set

127.0.0.1:6379> smembers set1
1) "mem4"
2) "mem2"
3) "mem1"
4) "mem3"
127.0.0.1:6379> smembers set2
1) "mem5"
2) "mem4"
3) "mem6"
4) "mem3"
127.0.0.1:6379> smembers set3
1) "mem8"
2) "mem2"
3) "mem1"
4) "mem7"
127.0.0.1:6379> sdiff set1 set2
1) "mem2"
2) "mem1"
127.0.0.1:6379> sdiff set1 set2 set3
(empty array)

127.0.0.1:6379> smembers set0
(empty array)
127.0.0.1:6379> sdiff set1 set0
1) "mem2"
2) "mem4"
3) "mem1"
4) "mem3"
127.0.0.1:6379> sdiff set0 set1
(empty array)

sdiffstore命令

sdiffstore命令先对几个集合进行sdiff操作,然后将结果存入到新的set中,如果set已经存在,会被覆盖掉。

127.0.0.1:6379> sadd diffset12 1 2
(integer) 2
127.0.0.1:6379> smembers diffset12
1) "1"
2) "2"
127.0.0.1:6379> sdiff set1 set2
1) "mem2"
2) "mem1"
127.0.0.1:6379> sdiffstore diffset12 set1 set2
(integer) 2
127.0.0.1:6379> smembers diffset12
1) "mem2"
2) "mem1"
127.0.0.1:6379>

 

sinter命令

sinter命令返回几个集合中同时存在的元素,如果没有返回empty array.

127.0.0.1:6379> smembers set1
1) "mem4"
2) "mem2"
3) "mem1"
4) "mem3"
127.0.0.1:6379> smembers set2
1) "mem5"
2) "mem4"
3) "mem6"
4) "mem3"
127.0.0.1:6379> smembers set3
1) "mem5"
2) "mem2"
3) "mem1"
4) "mem6"
127.0.0.1:6379> smembers set4
1) "mem5"
2) "mem1"
127.0.0.1:6379> sinter set1 set2 set4
(empty array)
127.0.0.1:6379> sinter set1 set2 set3
(empty array)
127.0.0.1:6379> sinter set1 set3 set4
1) "mem1"
127.0.0.1:6379> sinter set2 set3 set4
1) "mem5"

sinterstore命令

sinterstore命令先对几个集合进行inter join操作,然后将结果存入到新set中,如果set存在会被覆盖。

127.0.0.1:6379> help sinterstore
  SINTERSTORE destination key [key ...]
  summary: Intersect multiple sets and store the resulting set in a key
  since: 1.0.0
  group: set
127.0.0.1:6379> sadd interset:1:2 1 2
(integer) 2
127.0.0.1:6379> smembers interset:1:2
1) "1"
2) "2"
127.0.0.1:6379> sinter set1 set2
1) "mem4"
2) "mem3"
127.0.0.1:6379> sinterstore interset:1:2 set1 set2
(integer) 2
127.0.0.1:6379> smembers interset:1:2
1) "mem4"
2) "mem3"

sunion命令

sunion命令返回多个集合中所有的去重的元素。

127.0.0.1:6379> help sunion

  SUNION key [key ...]
  summary: Add multiple sets
  since: 1.0.0
  group: set

127.0.0.1:6379> smembers set1
1) "mem4"
2) "mem2"
3) "mem1"
4) "mem3"
127.0.0.1:6379> smembers set2
1) "mem5"
2) "mem4"
3) "mem6"
4) "mem3"
127.0.0.1:6379> smembers set3
1) "mem5"
2) "mem2"
3) "mem1"
4) "mem6"
127.0.0.1:6379> sunion set1 set2 set3
1) "mem3"
2) "mem4"
3) "mem2"
4) "mem5"
5) "mem1"
6) "mem6"

sunionset命令

sunionset命令会对几个集合进行union操作,然后将结果存放到新的set中,如果set存在,则覆盖。

127.0.0.1:6379> sadd unionset:1:2 1 2
(integer) 2
127.0.0.1:6379> smembers unionset
(empty array)
127.0.0.1:6379> smembers unionset:1:2
1) "1"
2) "2"
127.0.0.1:6379> sunion set1 set2
1) "mem3"
2) "mem4"
3) "mem2"
4) "mem5"
5) "mem1"
6) "mem6"
127.0.0.1:6379> sunionstore unionset:1:2 set1 set2
(integer) 6
127.0.0.1:6379> smembers unionset:1:2
1) "mem3"
2) "mem4"
3) "mem2"
4) "mem5"
5) "mem1"
6) "mem6"

 

posted @ 2022-08-11 22:15  panzq  阅读(436)  评论(0编辑  收藏  举报