对于springboot集成redis,我做了笔记,分享给有须要的小伙伴们,视频看的能源节点王鹤老师讲的springboot
能源节点王鹤老师解说的springboot教程,由浅入深,带你体验Spring Boot的极速开发过程,内容丰盛,涵盖了SpringBoot开发的方方面面,并且同步更新到Spring Boot 2.x系列的最新版本。
视频链接:https://www.bilibili.com/vide...
redis能帮咱们扩散掉数据库的压力,有了它能更好的反对并发性能!
能够这样了解redis位于数据库和springboot框架之间,起到数据缓存的作用。
在idea当中曾经集成了redis的插件配置
创立实现后会失去idea的驱动以及工具接口。
之后就要在本地启动redis服务,这个过程就如同相似启动mysql服务。
咱们能够到redis的官网下载,依据本人的零碎抉择x64or x32
windows
援用
https://github.com/tporadowsk...
援用
Linux
援用
https://redis.io/download
之后在本地须要开启redis服务
应用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
测试类
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;@SpringBootTestclass DemoApplicationTests { @Autowired private RedisTemplate<String, String> redisTemplate; @Test void contextLoads() { redisTemplate.opsForValue().set("myKey3", "4564"); System.out.println(redisTemplate.opsForValue().get("myKey3")); }}
因为框架曾经增加了redis 所以只须要将 redisTemplate 注入到Bean当中就能够调用接口对redis进行数据缓存。
当页面查问数据时首先去缓存当中查找数据, 如果没有数据再向数据库申请资源,因为redis的存储类型,存取速度很快,能在肯定水平上减缓数据库的压力。晋升并发性能,加固网站的稳定性。
咱们能够起线程池对接口进行并发测试,查看是否合乎逻辑,必要的加上锁。