Redis Sets无序集合 存储操作方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis Sets无序集合 存储操作方法相关的知识,希望对你有一定的参考价值。
欢迎大家加入 459479177QQ群进行交流
这次介绍的是Sets无序集合
老生常谈,使用redis-cli进入我们的redis服务
[[email protected] ~]# redis-cli 127.0.0.1:6379>
1>sadd
方法:sadd key value [value...]
描述:往key添加一个或多个value,如果value元素存在则忽略
127.0.0.1:6379> smembers skey1 (empty list or set) 127.0.0.1:6379> sadd skey1 a (integer) 1 127.0.0.1:6379> smembers skey1 1) "a" 127.0.0.1:6379> sadd skey1 a aa aaa (integer) 2 127.0.0.1:6379> smembers skey1 1) "a" 2) "aa" 3) "aaa"
2>scard
方法:scard key
描述:统计key有多少个元素,如果key不存在则返回0
127.0.0.1:6379> smembers skey1 1) "a" 2) "aa" 3) "aaa" 127.0.0.1:6379> smembers skey2 (empty list or set) 127.0.0.1:6379> scard skey1 (integer) 3 127.0.0.1:6379> scard skey2 (integer) 0
3>sdiff
方法:sdiff key1 key2 skey3...
描述:sdiff以显示第一个key差异元素,如果第一个key为空集合不存在则为空
127.0.0.1:6379> smembers skey1 1) "a" 2) "aa" 3) "aaa" 127.0.0.1:6379> smembers skey2 1) "22" 2) "a" 3) "1" 4) "11" 127.0.0.1:6379> smembers skey3 1) "22" 2) "a" 3) "2" 127.0.0.1:6379> sdiff skey1 skey2 skey3 1) "aa" 2) "aaa" 127.0.0.1:6379> sdiff skey2 skey1 skey3 1) "1" 2) "11" 127.0.0.1:6379> sdiff skey10 skey2 skey1 skey3 \\这里的skey10不存在 (empty list or set)
4>sdiffstore
方法:sdiffsotre destinationkey key1 key2
描述:destination为后面key1 key2 ...差异存储的指定集合,如果存在则覆盖,以key1为输入元素标本。
127.0.0.1:6379> smembers skey1 1) "a" 2) "aa" 3) "aaa" 127.0.0.1:6379> smembers skey2 1) "22" 2) "a" 3) "1" 4) "11" 127.0.0.1:6379> smembers skey10 (empty list or set) 127.0.0.1:6379> sdiffstore skey10 skey1 skey2 (integer) 2 127.0.0.1:6379> smembers skey1 1) "a" 2) "aa" 3) "aaa" 127.0.0.1:6379> smembers skey2 1) "22" 2) "a" 3) "1" 4) "11" 127.0.0.1:6379> smembers skey10 \\destination存储了以skey1对比skey2的差异元素 1) "aa" 2) "aaa"
5>sinter
方法:sinter key1 key2
描述:取key1与key2交集的成员,如果其中一个key为空,则显示为空
127.0.0.1:6379> smembers skey11 (empty list or set) 127.0.0.1:6379> smembers skey1 1) "a" 2) "aa" 3) "aaa" 127.0.0.1:6379> smembers skey2 1) "22" 2) "a" 3) "1" 4) "11" 127.0.0.1:6379> sinter skey1 skey2 1) "a" 127.0.0.1:6379> sinter skey1 skey11 (empty list or set)
6>sinterstore
方法:sinterstore destinationkey key1 key2...
描述:destination为后面key1 key2 ...交集存储的指定集合,如果存在则覆盖
127.0.0.1:6379> smembers skey1 1) "a" 2) "aa" 3) "aaa" 127.0.0.1:6379> smembers skey2 1) "22" 2) "a" 3) "1" 4) "11" 127.0.0.1:6379> smembers skey3 1) "22" 2) "a" 3) "2" 127.0.0.1:6379> smembers skey10 \\这里的元素有俩个aa跟aaa 1) "aa" 2) "aaa" 127.0.0.1:6379> smembers skey11 (empty list or set) 127.0.0.1:6379> sinterstore skey11 skey1 skey2 skey3 (integer) 1 127.0.0.1:6379> smembers skey11 1) "a" 127.0.0.1:6379> sinterstore skey10 skey1 skey2 skey3 \\使用存在的会覆盖,结构就剩下a (integer) 1 127.0.0.1:6379> smembers skey10 1) "a"
7>sismember
方法:sismeber key value
描述:判断vale是否为key元素,如果是则返回1,否则返回0
127.0.0.1:6379> smembers skey10 1) "a" 127.0.0.1:6379> sismember skey10 a (integer) 1 127.0.0.1:6379> sismember skey10 aaa (integer) 0
8>smembers
方法:smembers key {官方给的是smembers key value是错的}
127.0.0.1:6379> smembers skey10 a \\这段是官方给的方法 (error) ERR wrong number of arguments for ‘smembers‘ command \\直接提示报错 127.0.0.1:6379> smembers skey10 \\正确的方法 1) "a" 127.0.0.1:6379> smembers skey2 1) "22" 2) "a" 3) "1" 4) "11"
9>smove
方法:smove sourcekey destinationkey member
描述:如果sourcekey为空或不存在,不做任何操作!仅返回0,否则member从sourcekey移除并添加destinationkey中。如果destinationkey存在sourcekey的member,那么将仅仅从sourcekey删除member
127.0.0.1:6379> smembers skey1 1) "a" 2) "aa" 3) "aaa" 127.0.0.1:6379> smembers skey2 1) "22" 2) "a" 3) "1" 4) "11" 127.0.0.1:6379> smove skey1 skey2 a \\俩边都存在a,skey1则删除a,而skey2不做任何修改 (integer) 1 127.0.0.1:6379> smembers skey1 1) "aa" 2) "aaa" 127.0.0.1:6379> smembers skey2 1) "22" 2) "a" 3) "1" 4) "11" 127.0.0.1:6379> smove skey1 skey2 555 \\如果不存在或为空,仅返回0 (integer) 0 127.0.0.1:6379> smove skey1 skey22 aa \\如果skey22不存在,则新建并添加aa至skey22 (integer) 1 127.0.0.1:6379> smembers skey1 1) "aaa" 127.0.0.1:6379> smembers skey22 1) "aa" 127.0.0.1:6379> smove skey1 skey2 aaa (integer) 1 127.0.0.1:6379> smembers skey1 (empty list or set) 127.0.0.1:6379> smembers skey2 1) "1" 2) "11" 3) "a" 4) "22" 5) "aaa"
10>spop
方法:spop key
描述:随机弹出一个member并显示member
127.0.0.1:6379> smembers skey2 1) "1" 2) "11" 3) "a" 4) "22" 5) "aaa" 127.0.0.1:6379> spop skey2 "11" 127.0.0.1:6379> smembers skey2 1) "a" 2) "22" 3) "1" 4) "aaa" 127.0.0.1:6379> spop skey2 "aaa" 127.0.0.1:6379> smembers skey2 1) "a" 2) "22" 3) "1"
11>srandmember
方法:srandmember key [count]
描述:如果count为正数,小于key基数则指定显示count元素的数字,如果大于则全部显示,如果count为负数,则返回指定count元素的数字并且重复显示当中元素。
127.0.0.1:6379> srandmember skey2 10 \\正数 1) "22" 2) "a" 3) "1" 127.0.0.1:6379> srandmember skey2 1 1) "a" 127.0.0.1:6379> smembers skey2 1) "a" 2) "22" 3) "1" 127.0.0.1:6379> srandmember skey2 -1 \\负数 1) "a" 127.0.0.1:6379> srandmember skey2 -2 1) "1" 2) "a" 127.0.0.1:6379> srandmember skey2 -10 1) "1" 2) "22" 3) "1" 4) "1" 5) "22" 6) "1" 7) "a" 8) "1" 9) "22" 10) "1"
12>srem
方法:srem key member1 member2...
描述:移除一个或多个集合元素,不存在则忽略
127.0.0.1:6379> smembers skey2 1) "a" 2) "22" 3) "1" 127.0.0.1:6379> srem skey2 a 22 (integer) 2 127.0.0.1:6379> smembers skey2 1) "1"
13>sunion
方法: sunion key1 key2 key3...
描述:显示指定集合的并集,key不存在视为空
127.0.0.1:6379> smembers skey1 \\空不存在或者空 (empty list or set) 127.0.0.1:6379> smembers skey2 1) "1" 127.0.0.1:6379> smembers skey3 1) "22" 2) "a" 3) "2" 127.0.0.1:6379> sunion skey1 skey2 skey3 1) "a" 2) "22" 3) "1" 4) "2"
14>sunionstore
方法:sunionstore destinationkey key1 key2..
描述:显示指定集合的并集,并且把结果存储至destinationkey。
127.0.0.1:6379> smembers skey4 (empty list or set) 127.0.0.1:6379> smembers skey3 1) "22" 2) "a" 3) "2" 127.0.0.1:6379> smembers skey2 1) "1" 127.0.0.1:6379> smembers skey1 (empty list or set) 127.0.0.1:6379> sunionstore skey4 skey1 skey2 skey3 (integer) 4 127.0.0.1:6379> smembers skey4 1) "a" 2) "22" 3) "1" 4) "2"
15>sscan
方法:sscan key [match pattern] [count count] {官方的介绍} 是不是很晕?我这边讲下 sscan key 下标(start) match h* (匹配的比如h开头的) count 编号(end就是指smembers看到1)2)3),这样清楚吗,如果count为1,且1)没有匹配到h*,那么会往下匹配至少显示一个,然后就会正常匹配)
描述:用于迭代集合键中元素。
127.0.0.1:6379> smembers skey1 1) "233" 2) "hwww" 3) "hjaj" 4) "hh999" 5) "999" 6) "99kajjw" 7) "h888" 8) "99dd" 127.0.0.1:6379> sscan skey1 0 match h* count 1 1) "4" 2) 1) "hwww" 127.0.0.1:6379> sscan skey1 0 match h* count 2 1) "4" 2) 1) "hwww" 127.0.0.1:6379> sscan skey1 0 match h* count 3 \\一直匹配到这里都是只有一个h*匹配到的 1) "1" 2) 1) "hwww" 127.0.0.1:6379> sscan skey1 0 match h* count 4 \\匹配第4)时就会正常匹配,如果有就出现,没有就显示之前匹配的个数,请仔细观察下面的匹配情况 1) "5" 2) 1) "hwww" 2) "hjaj" 127.0.0.1:6379> sscan skey1 0 match h* count 5 1) "3" 2) 1) "hwww" 2) "hjaj" 3) "hh999" 127.0.0.1:6379> sscan skey1 0 match h* count 6 1) "3" 2) 1) "hwww" 2) "hjaj" 3) "hh999" 127.0.0.1:6379> sscan skey1 0 match h* count 7 1) "0" 2) 1) "hwww" 2) "hjaj" 3) "hh999" 4) "h888" 127.0.0.1:6379> sscan skey1 0 match h* count 8 1) "0" 2) 1) "hwww" 2) "hjaj" 3) "hh999" 4) "h888"
至此,Redis Sets无序集合就介绍完毕啦~
欢迎大家加入 459479177QQ群进行交流
本文出自 “12087328” 博客,请务必保留此出处http://12097328.blog.51cto.com/12087328/1856569
以上是关于Redis Sets无序集合 存储操作方法的主要内容,如果未能解决你的问题,请参考以下文章
直接在安装了redis的Linux机器上操作redis数据存储类型--对Sorted-Sets操作
redis数据结构之无序集合(set)有序集合(zset)基数(HyperLogLog)