第四章:redis 数组结构的set和一些通用命令「建议收藏」

第四章:redis 数组结构的set和一些通用命令「建议收藏」第四章:redis 数组结构的set和一些通用命令

大家好,又见面了,我是你们的朋友全栈君。

存储set ,set不允许有重复元素;

设置 获取元素:

127.0.0.1:6379> sadd set1 a b c
(integer) 3
127.0.0.1:6379> SMEMBERS set1
1) "a"
2) "c"
3) "b"

插入相同元素只保留一个:

127.0.0.1:6379> sadd set1 a e f
(integer) 2
127.0.0.1:6379> smembers set1
1) "a"
2) "e"
3) "c"
4) "b"
5) "f"

删除多个元素:

127.0.0.1:6379> SMEMBERS set1
1) "c"
2) "b"
3) "e"
4) "f"
127.0.0.1:6379> serm set1 c f
(error) ERR unknown command 'serm'
127.0.0.1:6379> srem set1 c f
(integer) 2
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"

simember key 判断某个元素是否存在,返回1为存在,返回0 为不存在

127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
127.0.0.1:6379> SMEMBERS key e
(error) ERR wrong number of arguments for 'smembers' command
127.0.0.1:6379> SISMEMBER key e
(integer) 0
127.0.0.1:6379> SISMEMBER set1 e
(integer) 1
127.0.0.1:6379> SISMEMBER set1 r
(integer) 0

sdiff以前面的集合为参考,计算差集:

127.0.0.1:6379> sadd set2 b c d e
(integer) 4
127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
4) "b"
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
127.0.0.1:6379> SDIFF set2 set3
1) "d"
2) "e"
3) "b"
4) "c"
127.0.0.1:6379> SDIFF set3 set2
(empty list or set)
127.0.0.1:6379> SDIFF set2 set1
1) "d"
2) "c"

sinter计算交集 sunion 计算并集:

127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
4) "b"
127.0.0.1:6379> SINTER set1 set2
1) "e"
2) "b"
127.0.0.1:6379> SUNION set1 set2
1) "d"
2) "e"
3) "c"
4) "b"

scard计算元素总数:

127.0.0.1:6379> SCARD set1
(integer) 2
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"

srandmember随机取一个或多个数:

127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
4) "b"
127.0.0.1:6379> SRANDMEMBER set2
"e"
127.0.0.1:6379> SRANDMEMBER set2 2
1) "d"
2) "c"

sdiffstore把差集结果存储到一个新的集合中

127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
4) "b"
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
127.0.0.1:6379> SDIFFSTORE r set1 set2
(integer) 0
127.0.0.1:6379> SDIFFSTORE r set2 set1
(integer) 2
127.0.0.1:6379> SMEMBERS r
1) "d"
2) "c"

sunionstore把交集结果存储到一个新的集合中:

127.0.0.1:6379> SUNIONSTORE r2 set1 set2
(integer) 4
127.0.0.1:6379> SMEMBERS r2
1) "d"
2) "e"
3) "c"
4) "b"

下面我们说说sorted-set有序排序的数据结构,也就是集合元素是有序排列的:

zadd 添加元素,这里我们以成绩和姓名为例,这里按成绩的从小到高排列的:

127.0.0.1:6379> zadd sort2 2 xiaoming 3 xiaohua 1 xiaohong
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoming"
3) "xiaohua"

zadd添加元素,有则更新,无则添加,也就是没有重复的值:

127.0.0.1:6379> zadd sort2 2 xiaoming 3 xiaohua 1 xiaohong
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoming"
3) "xiaohua"
127.0.0.1:6379> ZADD sort2 6 xiaoming
(integer) 0
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohua"
3) "xiaoming"
127.0.0.1:6379> zadd sort2 4 xiaoliang
(integer) 1
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohua"
3) "xiaoliang"
4) "xiaoming"

zrem删除元素:

127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohua"
3) "xiaoliang"
4) "xiaoming"
127.0.0.1:6379> zrem sort2  xiaohua
(integer) 1
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoliang"
3) "xiaoming"

zcard查看元素个数:

127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoliang"
3) "xiaoming"
127.0.0.1:6379> ZCARD sort2 
(integer) 3

把分数和姓名都显示出来:

127.0.0.1:6379> ZRANGE sort2 0 -1 withscores
1) "xiaohong"
2) "1"
3) "xiaoliang"
4) "4"
5) "xiaoming"
6) "6"

zrevrange降序排列:

127.0.0.1:6379> ZREVRANGE sort2 0 -1 withscores
1) "xiaoming"
2) "6"
3) "xiaoliang"
4) "4"
5) "xiaohong"
6) "1"

新增两个元素,根据排名删除元素,这里删除前三个:

127.0.0.1:6379> ZADD sort2 2 xiaohuang
(integer) 1
127.0.0.1:6379> ZADD sort2 3 xiaozi
(integer) 1
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohuang"
3) "xiaozi"
4) "xiaoliang"
5) "xiaoming"
127.0.0.1:6379> ZREMRANGEBYRANK sort2 0 2
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaoliang"
2) "xiaoming"

zremrangebyscore 根据具体分数范围删除元素:

127.0.0.1:6379> ZRANGE sort2 0 -1 withscores
 1) "xiaohong"
 2) "1"
 3) "xiaohuang"
 4) "2"
 5) "xiaozi"
 6) "3"
 7) "xiaoliang"
 8) "4"
 9) "xiaoming"
10) "6"
127.0.0.1:6379> ZREMRANGEBYscore sort2 2 5
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoming"

再增加元素,然后根据分数范围查找:

127.0.0.1:6379> zadd sort2 2 xiaohuang 3 xiaozi 4 xiaoliang
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohuang"
3) "xiaozi"
4) "xiaoliang"
5) "xiaoming"
127.0.0.1:6379> ZRANGEBYSCORE sort2 2 5
1) "xiaohuang"
2) "xiaozi"
3) "xiaoliang"

limit类似分页查找:

127.0.0.1:6379>  ZRANGE sort2 0 -1 withscores
 1) "xiaohong"
 2) "1"
 3) "xiaohuang"
 4) "2"
 5) "xiaozi"
 6) "3"
 7) "xiaoliang"
 8) "4"
 9) "xiaoming"
10) "6"
127.0.0.1:6379> ZRANGEBYSCORE sort2 0 9 limit 0 2
1) "xiaohong"
2) "xiaohuang"
127.0.0.1:6379> ZRANGEBYSCORE sort2 0 9 withscores limit 0 2
1) "xiaohong"
2) "1"
3) "xiaohuang"
4) "2"

zincrby加分:

127.0.0.1:6379> ZINCRBY sort2 10 xiaohong
"11"
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohuang"
2) "xiaozi"
3) "xiaoliang"
4) "xiaoming"
5) "xiaohong"
127.0.0.1:6379> ZRANGE sort2 0 -1 withscores
 1) "xiaohuang"
 2) "2"
 3) "xiaozi"
 4) "3"
 5) "xiaoliang"
 6) "4"
 7) "xiaoming"
 8) "6"
 9) "xiaohong"
10) "11"

zcount计算某个评分范围内的个数:

127.0.0.1:6379> ZRANGE sort2 0 -1 withscores
 1) "xiaohuang"
 2) "2"
 3) "xiaozi"
 4) "3"
 5) "xiaoliang"
 6) "4"
 7) "xiaoming"
 8) "6"
 9) "xiaohong"
10) "11"
127.0.0.1:6379> ZCOUNT sort2 0 20
(integer) 5

redis的一些通用操作:

关键字查找key:

127.0.0.1:6379> keys s*
1) "sort2"
2) "set2"
3) "set1"
4) "sort"
5) "sort1"

查找s开头,后面只跟一个字符的key:

127.0.0.1:6379> set s1 aa
OK
127.0.0.1:6379> key s*
(error) ERR unknown command 'key'
127.0.0.1:6379> keys s?
1) "s1"

del删除元素:

127.0.0.1:6379> del sort s1
(integer) 2
127.0.0.1:6379> keys s*
1) "sort2"
2) "set2"
3) "set1"
4) "sort1"

rename重命名:

127.0.0.1:6379> set s2 ss
OK
127.0.0.1:6379> rename s2 s3
OK
127.0.0.1:6379> get s3
"ss"

expire设置有效时间,单位是秒:

127.0.0.1:6379> EXPIRE s3 12
(integer) 1
127.0.0.1:6379> s3 ttl
(error) ERR unknown command 's3'
127.0.0.1:6379> ttl s3
(integer) -2
127.0.0.1:6379> get s3
(nil)

type查看数据类型:

127.0.0.1:6379> type sort2
zset
127.0.0.1:6379> type name
string
127.0.0.1:6379> type l1
list

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106132.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号