当前位置:网站首页>第四章:redis 数组结构的set和一些通用命令「建议收藏」
第四章:redis 数组结构的set和一些通用命令「建议收藏」
2022-08-05 10:04:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
存储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
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106132.html原文链接:https://javaforall.cn
边栏推荐
- What is SPL?
- Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
- 什么是CRM决策分析管理?
- 我们的Web3创业项目,黄了
- uniapp 连接ibeacon
- 阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
- What is the function of the regular expression replaceFirst() method?
- 仿SBUS与串口数据固定转换
- Jenkins manual (2) - software configuration
- 偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
猜你喜欢
Dry goods!Generative Model Evaluation and Diagnosis
Custom filters and interceptors implement ThreadLocal thread closure
项目成本控制如何帮助项目成功?
电竞、便捷、高效、安全,盘点OriginOS功能的关键词
MySQL transactions
How to realize the short press and long press detection of the button?
CPU的亲缘性affinity
IDEA执行Test操作导致数据插入时出现了重复数据
技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
随机推荐
Dry goods!Generative Model Evaluation and Diagnosis
基于MindSpore高效完成图像分割,实现Dice!
攻防世界-PWN-new_easypwn
Pytorch Deep Learning Quick Start Tutorial -- Mound Tutorial Notes (3)
【zeno】为zeno增加子模块/新节点的最小化的例子
PAT Grade B-B1020 Mooncake(25)
STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
The difference between find, matches, lookingAt matching strings in matcher
What is SPL?
shell脚本实例
无题一
无题二
PAT Level B - B1021 Single Digit Statistics (15)
MySQL事务
一文道清什么是SPL
阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
手把手教你纯c实现异常捕获try-catch组件
Tanabata romantic date without overtime, RPA robot helps you get the job done
自定义过滤器和拦截器实现ThreadLocal线程封闭