Redis第7️⃣课 Set
2019-04-29 本文已影响0人
小超_8b2f
特点
无序,不重复,集合间操作
API
命令 | 解释 |
---|---|
sadd key member [member ...] | 向集合key添加member如果已经存在则添加失败 |
srem key member [member ...] | 将集合key中的member删除,如果不存在则删除失败 |
spop key [count] | 从集合中弹出count个元素 |
scard key | 求集合中元素的个数 |
sismember key member | 判断member是否是集合中元素 |
srandomember key [count] | 随机获取集合中count个元素 |
smembers key | 获取所有集合中元素 |
spop key | 从集合中随机弹出一个元素 |
sinter key [key ...] | 取集合间的交集 |
sdiff key [key ...] | 取集合间的不同元素 |
sunion key [key ...] | 取集合间的并集 |
sdiffstore destination key [key...] | 返回给定所有集合的差集并存储在 destination 中 |
sinterstore targetSet key [key...] | 返回给定所有集合的交集并存储在 targetSet 中 |
sunionstore result key [key...] | 所有给定集合的并集存储在 result 集合中 |
smove from to member | 将 member 元素从 from 集合移动到 to 集合 |
sscan key cursor [MATCH pattern] [COUNT count] | 迭代集合中的元素 |
127.0.0.1:6379> sadd set01 it news his sports
(integer) 4
127.0.0.1:6379> smembers set01
1) "sports"
2) "news"
3) "it"
4) "his"
127.0.0.1:6379> spop set01 news
(error) ERR value is not an integer or out of range
127.0.0.1:6379> spop set01
"his"
127.0.0.1:6379> smembers set01
1) "sports"
2) "news"
3) "it"
127.0.0.1:6379> scard set01
(integer) 3
127.0.0.1:6379> sismember set01 xiao
(integer) 0
127.0.0.1:6379> sadd set02 java c++ php c python
(integer) 5
127.0.0.1:6379> srandmember set02 2
1) "java"
2) "php"
127.0.0.1:6379> srandmember set02 2
1) "java"
2) "c++"
集合间的操作:
127.0.0.1:6379> sadd set01 hello world helloworld java python
(integer) 5
127.0.0.1:6379> sadd set02 java php python c c++ go
(integer) 6
127.0.0.1:6379> sinter set01
1) "java"
2) "hello"
3) "helloworld"
4) "world"
5) "python"
127.0.0.1:6379> sinter key01 key02
(empty list or set)
127.0.0.1:6379> sinter set01 set02
1) "java"
2) "python"
127.0.0.1:6379> sdiff set01 set02
1) "hello"
2) "helloworld"
3) "world"
127.0.0.1:6379> sunion set01 set02
1) "java"
2) "python"
3) "php"
4) "c++"
5) "go"
6) "world"
7) "hello"
8) "helloworld"
9) "c"
127.0.0.1:6379>
1.给用户添加标签
sadd users:1:tags tag1 tag2 tag5
sadd users:2:tags tag1 tag4 tag5
......
sadd users:k:tags tag4 tag7 tag9
2.给标签添加用户
sadd tags:1:users user1 user2 user3
sadd tags:2:users user4 user4 user6
......
sadd tags:k:users user1 user3 user5
sinter set01 set021,2放在一个事物里
抽奖系统
image.pngTips
sadd :标签
spop / srandmember : 随机项
sadd + sinter :关系图