Redis set常用命令

redis set 和java的set集合功能差不多的

集合(Set) 的主要功能就是求并集、交集、差集。

A = {'a', 'b', 'c'}
B = {'a', 'e', 'i', 'o', 'u'}

inter(x, y): 交集,在集合x和集合y中都存在的元素。
inter(A, B) = {'a'}

union(x, y): 并集,在集合x中或集合y中的元素,如果一个元素在x和y中都出现,那只记录一次即可。
union(A,B) = {'a', 'b', 'c', 'e', 'i', 'o', 'u'}

diff(x, y): 差集,在集合x中而不在集合y中的元素。
diff(A,B) = {'b', 'c'}

card(x): 基数,一个集合中元素的数量。
card(A) = 3

空集: 基数为0的集合。

sadd(key, member)

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

smembers(key)

返回名称为key的set的所有元素

127.0.0.1:6379> sadd users u1
(integer) 1
127.0.0.1:6379> sadd users u2
(integer) 1
127.0.0.1:6379> sadd users u3 u4
(integer) 2
127.0.0.1:6379> smembers users
1) "u2"
2) "u4"
3) "u1"
4) "u3"

srem(key, member) :

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

127.0.0.1:6379> smembers users
1) "u2"
2) "u4"
3) "u1"
4) "u3"
127.0.0.1:6379> srem users u1
(integer) 1
127.0.0.1:6379> srem users u3 u4
(integer) 2
127.0.0.1:6379> smembers users
1) "u2"

sismember(key, member) :

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

127.0.0.1:6379> smembers users
1) "u2"
127.0.0.1:6379> sismember users u2
(integer) 1
127.0.0.1:6379> sismember users u1
(integer) 0

scard(key) :

返回名称为key的set的基数,一个集合中元素的数量。

127.0.0.1:6379> smembers users
1) "u2"
127.0.0.1:6379> scard users
(integer) 1

smove(srckey, dstkey, member) :

将member元素从source集合移动到destination集合。

127.0.0.1:6379> smembers users
1) "u2"
2) "u4"
3) "u1"
4) "u3"
127.0.0.1:6379> smembers blacklist
(empty list or set)
127.0.0.1:6379> smove users blacklist u1
(integer) 1
127.0.0.1:6379> smembers users
1) "u2"
2) "u4"
3) "u3"
127.0.0.1:6379> smembers blacklist
1) "u1"

srandmember(key) :

随机返回名称为key的set的一个元素

127.0.0.1:6379> smembers users
1) "u2"
2) "u4"
3) "u3"
127.0.0.1:6379> srandmember users
"u3"
127.0.0.1:6379> srandmember users 2
1) "u2"
2) "u3"

spop(key) :

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

127.0.0.1:6379> smembers users
1) "u2"
2) "u4"
3) "u3"
127.0.0.1:6379> spop users
"u3"
127.0.0.1:6379> smembers users
1) "u2"
2) "u4"

sinter(key1, key2,…key N) :

求交集。

127.0.0.1:6379> smembers group1
1) "3"
2) "2"
3) "4"
4) "1"
5) "a"
127.0.0.1:6379> smembers group2
1) "b"
2) "a"
3) "1"
4) "c"
127.0.0.1:6379> sinter group1 group2
1) "a"
2) "1"

sinterstore(dstkey, (key2,…key N)) :

求交集并将交集保存到dstkey的集合

127.0.0.1:6379> sinterstore group3 group1 group2
(integer) 2
127.0.0.1:6379> smembers group1
1) "3"
2) "4"
3) "1"
4) "2"
5) "a"
127.0.0.1:6379> smembers group2
1) "b"
2) "a"
3) "1"
4) "c"
127.0.0.1:6379> smembers group3
1) "a"
2) "1"

sunion(key1, (keys)) :

求并集

127.0.0.1:6379> sunion group1 group2
1) "3"
2) "4"
3) "1"
4) "2"
5) "b"
6) "a"
7) "c"

sunionstore(dstkey, (keys)) :

求并集并将并集保存到dstkey的集合

127.0.0.1:6379> sunionstore group4 group1 group2
(integer) 7
127.0.0.1:6379> smembers group4
1) "3"
2) "4"
3) "1"
4) "2"
5) "b"
6) "a"
7) "c"

sdiff(key1, (keys)) :

求差集

127.0.0.1:6379> smembers group1
1) "3"
2) "4"
3) "1"
4) "2"
5) "a"
127.0.0.1:6379> smembers group2
1) "b"
2) "a"
3) "1"
4) "c"
127.0.0.1:6379> sdiff group1 group2
1) "2"
2) "3"
3) "4"

sdiffstore(dstkey, (keys)) :

求差集并将差集保存到dstkey的集合

127.0.0.1:6379> sdiffstore group5 group1 group2
(integer) 3
127.0.0.1:6379> smembers group5
1) "2"
2) "3"
3) "4"

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