当前位置:操作系统 > Unix/Linux >>

Redis系列-存储篇set主要操作函数小结

Redis系列-存储篇set主要操作函数小结
 
 最近,总是以“太忙“为借口,很久没有blog了,凡事贵在恒,希望我能够坚持不懈,毕竟在blog的时候,也能提升自己。废话不说了,直奔主题”set“
redis set 是string类型对象的无序集合,set不管存储多少对象,对存储对象的add,remove和test操作的时间复杂度是O(1)。set最多能包含 232 - 1 个member。
  www.zzzyk.com  
1、增加
 
语法:sadd key member[member...]
 
        解释:对特定key的set增加一个或多个值,返回是增加元素的个数。注意:对同一个member多次add,set中只会保留一份。  www.zzzyk.com  
 
[plain] 
[root@xsf001 ~]# redis-cli   
redis 127.0.0.1:6379> sadd stu zhangsan lisi wangwu #新增  
(integer) 3  
redis 127.0.0.1:6379> smembers stu    #得到set的所有member  
1) "wangwu"  
2) "lisi"  
3) "zhangsan"  
redis 127.0.0.1:6379> sadd stu zhangsan #增加存在的member  
(integer) 0  
redis 127.0.0.1:6379> smembers stu  
1) "wangwu"  
2) "lisi"  
3) "zhangsan"  
redis 127.0.0.1:6379> sadd tech wangwu liming joe  
(integer) 3  
redis 127.0.0.1:6379> sadd tech jim  
(integer) 1  
redis 127.0.0.1:6379> smembers tech  
1) "jim"  
2) "liming"  
3) "wangwu"  
4) "joe"  
  2、查询
 
a)smembers
 
语法:smembers key
 
解释:获取set中的所有member
 
[plain] 
redis 127.0.0.1:6379> smembers stu  
1) "wangwu"  
2) "lisi"  
3) "zhangsan"  
redis 127.0.0.1:6379> smembers tech  
1) "jim"  
2) "liming"  
3) "wangwu"  
4) "joe"  
 
b)sismember
语法:sismember key member
 
解释:判断值是否是set的member。如果值是set的member返回1,否则,返回0
 
[plain] 
redis 127.0.0.1:6379> sismember tech jim #jim 是set的member  
(integer) 1  
redis 127.0.0.1:6379> sismember tech jim001 #jim001 不是set的member  
(integer) 0  
 c)scard
语法:scard key
 
解释:返回set的member个数,如果set不存在,返回0
 
[plain] 
redis 127.0.0.1:6379> scard tech  # tech 存在  
(integer) 4  
redis 127.0.0.1:6379> scard stud #stud 不存在  
(integer) 0  
redis 127.0.0.1:6379> scard stu  
(integer) 4  
 d)srandmember
语法:srandmember key
 
解释:从set中返回一个随机member
 
[plain] 
redis 127.0.0.1:6379> srandmember stu  
"zhangsan"  
redis 127.0.0.1:6379> srandmember stu  
"zhangsan"  
redis 127.0.0.1:6379> srandmember stu  
"wangwu"  
redis 127.0.0.1:6379> srandmember stu  
"zhangsan01"  
 
3、删除
a)spop
 
语法:spop key
 
解释:移除并返回一个随机member
 
[plain] 
redis 127.0.0.1:6379> smembers stu #pop前  
1) "zhangsan01"  
2) "wangwu"  
3) "lisi"  
4) "zhangsan"  
redis 127.0.0.1:6379> spop stu  #移除一个随机member  
"lisi"  
redis 127.0.0.1:6379> smembers stu #pop后  
1) "zhangsan01"<span style="white-space:pre">   </span>  
2) "wangwu"  
3) "zhangsan"  
 b)srem
语法:srem key member [member ...]
 
解释:移除一个或多个member
 
[plain] 
redis 127.0.0.1:6379> smembers tech  
1) "jim"  
2) "liming"  
3) "wangwu"  
4) "joe"  
redis 127.0.0.1:6379> srem tech jim   #移除jim  
(integer) 1  
redis 127.0.0.1:6379> smembers tech     
1) "liming"  
2) "wangwu"  
3) "joe"  
redis 127.0.0.1:6379> srem tech liming joe #移除多个member  
(integer) 2  
redis 127.0.0.1:6379> smembers tech  
1) "wangwu"  
 c)smove
语法:smove source destination member
 
解释:将source中的member移动到destination
 
[plain] 
redis 127.0.0.1:6379> smembers tech   #smove前  
1) "wangwu"  
redis 127.0.0.1:6379> smembers stu  
1) "zhangsan01"  
2) "wangwu"  
3) "zhangsan"  
redis 127.0.0.1:6379> smove stu tech zhangsan  #将zhangsan 从stu移动到tech  
(integer) 1  
redis 127.0.0.1:6379> smembers stu #smove后  
1) "zhangsan01"  
2) "wangwu"  
redis 127.0.0.1:6379> smembers tech  
1) "wangwu"  
2) "zhangsan"  
 
4、其他
 
a)并集
 
语法:sunion key[key...]
 
解释:多个set的并集
 
[plain] 
redis 127.0.0.1:6379> smembers stu  
1) "zhangsan01"  
2) "wangwu"  
redis 127.0.0.1:6379> sunion stu  
1) "zhangsan01"  
2) "wangwu"  
redis 127.0.0.1:6379> smembers tech  
1) "wangwu"  
2) "zhangsan"  
redis 127.0.0.1:6379> sunion stu tech  
1) "zhangsan01"  
2) "wangwu"  
3) "zhangsan"  
b)把并集结果存储到set
 
语法:sunionstore destination key [key ...]
 
解释:求多个set并集,并把结果存储到destination 
 
[plain] 
redis 127.0.0.1:6379> sunionstore same stu tech #把stu tech并集结果存储在union  
(integer) 3  
redis 127.0.0.1:6379> smembers union  
1) "zhangsan01"  
2) "wangwu"  
3) "zhangsan"  
 c)交集
 
语法:sinter key[key...]
 
解释:多个set的交集
 
[plain] 
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,