关于springboot集成redis,我做了笔记,分享给有需要的小伙伴们,视频看的动力节点王鹤老师讲的,
动力节点王鹤老师讲解的springboot教程,由浅入深,带你体验Spring Boot的极速开发过程,内容丰富,涵盖了SpringBoot开发的方方面面,并且同步更新到Spring Boot 2.x系列的最新版本。
视频链接:
https://www.bilibili.com/video/BV1XQ4y1m7ex
redis能帮我们分散掉数据库的压力,有了它能更好的支持并发性能!
可以这样理解redis位于数据库和springboot框架之间,起到数据缓存的作用。
在idea当中已经集成了redis的插件配置
image.png
image.png
创建完成后会得到idea的驱动以及工具接口。
之后就要在本地启动redis服务,这个过程就好像类似启动mysql服务。
我们可以到redis的官网下载,根据自己的系统选择x64or x32
windows
https://github.com/tporadowski/redis/releases
Linux
之后在本地需要开启redis服务
image.png
使用Java进行链接,向redis当中缓存数据
配置 - -application.yml
spring:
redis:
host: 127.0.0.1
port: 6379
jedis:
pool:
max-active: 8
max-wait: -1ms
max-idle: 500
min-idle: 0
lettuce:
shutdown-timeout: 0ms
image.gif
测试类
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void contextLoads() {
redisTemplate.opsForValue().set("myKey3", "4564");
System.out.println(redisTemplate.opsForValue().get("myKey3"));
}
}
image.gif
由于框架已经添加了redis 所以只需要将 redisTemplate 注入到Bean当中就可以调用接口对redis进行数据缓存。
当页面查询数据时首先去缓存当中查找数据, 如果没有数据再向数据库请求资源,因为redis的存储类型,存取速度很快,能在一定程度上减缓数据库的压力。提升并发性能,加固网站的稳定性。
我们可以起线程池对接口进行并发测试,查看是否符合逻辑,必要的加上锁。