Redis最初设计和Mysql数据库一样是用来存储数据的
但是它的性能特别高,因为不基于磁盘,它的数据也可以持久化到磁盘。
面试题Redis的特性:

如果不写ip地址和端口默认是127.0.0.1和6379


Redis官网可查看所有指令的信息:Commands | Docshttps://redis.io/docs/latest/commands/
Redis数据类型:

String类型的key和value最大都是512M.

setnx 如果不存在就新增,setex 如果存在就更改。
spring boot集成Redis:
引入包:

RedisTemplate自动装配到springBoot的IOC容器,就可以直接用了:

配置文件里再加个配置:

再设置一下序列化:

Java中Redis的客户端:

Jedis是基于指令去封装的,(netty要学好)
Redis里面不会保证强一致性。
一个简单的例子:

上面代码会发生缓存击穿问题:假如同时过来十万个请求,Redis里面的key刚好过期了或者被删了,十万个请求在Redis中都没有查到数据,十万个请求都并发地到达DB,这就是缓存击穿。
解决办法:

这样只有第一个请求会去请求DB,后面的请求都是从Redis里面拿,图示:

String数据类型的应用场景:

存储Hash数据类型:


SpringBoot引入Redission:
先引入依赖包

创建Redission连接:


List:


上图最后一句表示,如果什么都弹不出来了,就阻塞10s(blpop或brpop:阻塞弹出)
Redission客户端是基于netty做的,封装的都是Redis的指令(把Redis的底层指令封装了下)
List数据模型:

作为阻塞队列用:

用命令行添加元素的时候,会自动消费消息

Set数据类型

用set做抽奖功能:

Redis客户端:
