關(guān)于springboot集成redis定铜,我做了筆記矮锈,分享給有需要的小伙伴們豺旬,視頻看的動(dòng)力節(jié)點(diǎn)王鶴老師講的钠惩,
動(dòng)力節(jié)點(diǎn)王鶴老師講解的springboot教程,由淺入深族阅,帶你體驗(yàn)Spring Boot的極速開發(fā)過程篓跛,內(nèi)容豐富,涵蓋了SpringBoot開發(fā)的方方面面坦刀,并且同步更新到Spring Boot 2.x系列的最新版本愧沟。
視頻鏈接:
https://www.bilibili.com/video/BV1XQ4y1m7ex
redis能幫我們分散掉數(shù)據(jù)庫的壓力,有了它能更好的支持并發(fā)性能鲤遥!
可以這樣理解redis位于數(shù)據(jù)庫和springboot框架之間沐寺,起到數(shù)據(jù)緩存的作用。
在idea當(dāng)中已經(jīng)集成了redis的插件配置
創(chuàng)建完成后會(huì)得到idea的驅(qū)動(dòng)以及工具接口渴频。
之后就要在本地啟動(dòng)redis服務(wù)芽丹,這個(gè)過程就好像類似啟動(dòng)mysql服務(wù)。
我們可以到redis的官網(wǎng)下載卜朗,根據(jù)自己的系統(tǒng)選擇x64or x32
windows
https://github.com/tporadowski/redis/releases
Linux
之后在本地需要開啟redis服務(wù)
使用Java進(jìn)行鏈接场钉,向redis當(dāng)中緩存數(shù)據(jù)
配置 - -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;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void contextLoads() {
redisTemplate.opsForValue().set("myKey3", "4564");
System.out.println(redisTemplate.opsForValue().get("myKey3"));
}
}
由于框架已經(jīng)添加了redis 所以只需要將 redisTemplate 注入到Bean當(dāng)中就可以調(diào)用接口對(duì)redis進(jìn)行數(shù)據(jù)緩存宇植。
當(dāng)頁面查詢數(shù)據(jù)時(shí)首先去緩存當(dāng)中查找數(shù)據(jù)指郁, 如果沒有數(shù)據(jù)再向數(shù)據(jù)庫請(qǐng)求資源疫粥,因?yàn)閞edis的存儲(chǔ)類型梗逮,存取速度很快绣溜,能在一定程度上減緩數(shù)據(jù)庫的壓力底哗。提升并發(fā)性能艘虎,加固網(wǎng)站的穩(wěn)定性。
我們可以起線程池對(duì)接口進(jìn)行并發(fā)測試候生,查看是否符合邏輯唯鸭,必要的加上鎖。