当前位置:网站首页>初始redis基础了解
初始redis基础了解
2022-04-21 18:17:00 【·梅花十三】
一、了解Redis
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询. Redis 内置了 复制(replication), LUA脚本(Lua scripting), LRU驱动事件(LRU eviction), 事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel) 和自动 分区(Cluster)提供高可用性(high availability)
二、掌握基础
1、redis默认有16个数据库 (默认第0个)
——如何查看?
- 进入/usr/local/etc 目录下: cd /usr/local/etc
- 查看redis配置文件查看:vim redis.conf
注:或者通过访达直接进入目录,用文本编辑器打开

可以通过select命令进行切换数据库
127.0.0.1:6379> select 3 # 切换到3号数据库
OK
127.0.0.1:6379[3]> set name wpf # set设置键值
OK
127.0.0.1:6379[3]> set age 21
OK
127.0.0.1:6379[3]> dbsize # 查看DB(库)大小
(integer) 2
127.0.0.1:6379[3]>
2、为什么redis的默认端口号是6379
6379在是手机九宫格按键上‘ MERZ ’对应的号码,而MERZ取自意大利歌女Alessia Merz的名字。MERZ长期以来被Redis作者antirez及其朋友当作愚蠢的代名词。后来Redis作者在开发Redis时就选用了这个端口
3、redis是单线程的!
Redis 是基于内存操作,Redis 性能瓶颈不是CPU,而是根据机器的内存和网络带宽,既然单线程容易实现,而且CPU不会成为瓶颈,所以就使用单线程了!
Redis是C语言写的,官方提供的数据为 100000+的QPS,完全不比同样使用 key-value的Memecache差!
单线程无法发挥多核CPU性能,可以通过在单机开多个Redis实例来完善!
4、redis为什么是单线程还这么快?
误区1:高性能的服务器一定是多线程?
误区2:多线程一定比单线程效率高!(忽略了CPU的上下文切换)
——速度快慢由高到低:CPU > 内存 > 硬盘
- 基于内存,内存的读写速度非常快;
- 单线程,省去了很多上下文切换线程的时间;(以及加锁消耗,和死锁问题)
- redis使用多路复用技术,可以处理并发的连接。非阻塞IO 内部实现采用epoll,采用了epoll+自己实现的简单的事件框架。epoll中的读、写、关闭、连接都转化成了事件,然后利用epoll的多路复用特性,绝不在io上浪费一点时间。
核心:Redis是将所有的数据全部放在内存中的,使用单线程去操作效率就是最高的!(多线程存在CPU上下文回切换:耗时的操作!),对于内存系统来说,如果没有上下文切换效率就是最高的!多次读写都在一个CPU上——最佳方案!
5、IO多路复用技术
redis 采用网络IO多路复用技术来保证在多连接的时候, 系统的高吞吐量。Redis使用的是非阻塞IO,IO多路复用,使用了单线程来轮询描述符,将数据库的开、关、读、写都转换成了事件,减少了线程切换时上下文的切换和竞争。
- 多路:指的是多个socket (网络) 连接
- 复用:指的是复用一个线程。
总结:复用技术可以让单个线程高效的处理多个连接请求(尽量减少网络IO的时间消耗),且Redis在内存中操作数据的速度非常快(内存中的操作不会成为这里的性能瓶颈),主要以上两点造就了Redis具有很高的吞吐量。
常见的命令
1、切换数据库
select [ index ]2、查看所有key
keys *3、获取单个key的值
get [key名]4、清除当前库所有键值
flushdb5、清除所有库键值
flushall6、判断key是否存在(等同于sql的 =,而非like;例如where name='张三')
exists [ key名 ]7、移除当前库的某个key(1表示当前库,测试中用select切换到库3,移除还是1而非3)
move [ key名称 ] 1 —— 语法:move [ key名称 ] db8、设置key的过期时间,单位为秒(例:expire name 10)
expire [ key名 ] [秒数]9、查看当前key的剩余过期时间
ttl [ key名 ]10、查看当前key的类型(string、int、map....)
type [ key名 ]
版权声明
本文为[·梅花十三]所创,转载请带上原文链接,感谢
https://blog.csdn.net/w15558056319/article/details/121162993
边栏推荐
- Live app source code, add picture display function on the basis of unable to display pictures
- How to use serialization and deserialization of JSON in golang
- Target penetration exercise 76-dc8
- 接口测试框架实战(二)| 接口请求断言
- [untitled]
- [牛客网刷题 Day4] JZ23 链表中环的入口结点
- Advanced formula 48 of C language: principle of function design
- Composant tooltip: déterminer si tooltip est affiché en fonction du débordement de contenu
- MySQL - remote connection to non local MySQL database server, Error 1130: host 192.168.3.100 is not allowed to connect to this MySQL s
- 泛型
猜你喜欢

靶机渗透练习77-DC9

最基本的JDBC模板及数据库乱码处理

Target penetration exercise 72-dc4

Eating this open source gadget makes MCU development as efficient as Arduino

Look at how the technology house saves Xueba machine. I repaired my laptop with 10 yuan

移植openharmony之添加wifi驱动

Just got the byte beat offer "floating"

你必须懂也可以懂的微服务系列三:服务调用
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

人员不足、供应链断裂,危机之下制造业该如何自救?
随机推荐
[牛客网刷题 Day4] JZ76 删除链表中重复的结点(递归)
Q:excel制作折线图
【论文精读】Stable Linear Structures and Seam Measurements for Parallax Image Stitching
Integer面试解析、equals与==的区别
How does IOT platform realize business configuration center
[7:00 pm tonight] discussion on the development and application scenarios of metartc
华为云GaussDB(for Influx)揭密第六期——数据分级存储
Target penetration exercise 79 venom
[untitled]
After reading this tutorial, you will have your own satellite (DIY full explanation)
靶机渗透练习76-DC8
Tooltip 组件:根据内容是否溢出判断是否显示 Tooltip
The upper computer is fun to play like this!
[pytorch image classification] RESNET network structure
Advanced formula 47 of C language: recursive function analysis
靶机渗透练习78-Thoth Tech
[npj | digital medicine] machine learning of medical images: failure of methodology and suggestions for the future
Eating this open source gadget makes MCU development as efficient as Arduino
After fastjson is automatically upgraded to fastjson2, the idea development environment is normally printed into a jar package and an error exception POM is reported after publishing the generated env
Shallow comparison between oceanbase and tidb - implementation plan