SpringBoot集成Redis數(shù)據(jù)庫,實(shí)現(xiàn)緩存管理
一、Redis簡(jiǎn)介
Spring Boot中除了對(duì)常用的關(guān)系型數(shù)據(jù)庫提供了優(yōu)秀的自動(dòng)化支持之外,對(duì)于很多NoSQL數(shù)據(jù)庫一樣提供了自動(dòng)化配置的支持,包括:Redis, MongoDB, Elasticsearch。這些案例整理好后,陸續(xù)都會(huì)上傳Git。
SpringBoot2 版本,支持的組件越來越豐富,對(duì)Redis的支持不僅僅是擴(kuò)展了API,更是替換掉底層Jedis的依賴,換成Lettuce。
本案例需要本地安裝一臺(tái)Redis數(shù)據(jù)庫。
二、Spring2.0集成Redis
1、核心依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、配置文件
# 端口 server: port: 8008 spring: application: # 應(yīng)用名稱 name: node08-boot-redis # redis 配置 redis: host: 127.0.0.1 #超時(shí)連接 timeout: 1000ms jedis: pool: #最大連接數(shù)據(jù)庫連接數(shù),設(shè) 0 為沒有限制 max-active: 8 #最大等待連接中的數(shù)量,設(shè) 0 為沒有限制 max-idle: 8 #最大建立連接等待時(shí)間。如果超過此時(shí)間將接到異常。設(shè)為-1表示無限制。 max-wait: -1ms #最小等待連接中的數(shù)量,設(shè) 0 為沒有限制 min-idle: 0
這樣Redis的環(huán)境就配置成功了,已經(jīng)可以直接使用封裝好的API了。
3、簡(jiǎn)單測(cè)試案例
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@RestController
public class RedisController {
@Resource
private StringRedisTemplate stringRedisTemplate ;
@RequestMapping("/setGet")
public String setGet (){
stringRedisTemplate.opsForValue().set("cicada","smile");
return stringRedisTemplate.opsForValue().get("cicada") ;
}
@Resource
private RedisTemplate redisTemplate ;
/**
* 設(shè)置 Key 的有效期 10 秒
*/
@RequestMapping("/setKeyTime")
public String setKeyTime (){
redisTemplate.opsForValue().set("timeKey","timeValue",10, TimeUnit.SECONDS);
return "success" ;
}
@RequestMapping("/getTimeKey")
public String getTimeKey (){
// 這里 Key 過期后,返回的是字符串 'null'
return String.valueOf(redisTemplate.opsForValue().get("timeKey")) ;
}
}
4、自定義序列化配置
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
/**
* Redis 配置
*/
@Configuration
public class RedisConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class) ;
/**
* 序列化配置
*/
@Bean
public RedisTemplate<String, Serializable> redisTemplate
(LettuceConnectionFactory redisConnectionFactory) {
LOGGER.info("RedisConfig == >> redisTemplate ");
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
5、序列化測(cè)試
import com.boot.redis.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@RestController
public class SerializeController {
@Resource
private RedisTemplate redisTemplate ;
@RequestMapping("/setUser")
public String setUser (){
User user = new User() ;
user.setName("cicada");
user.setAge(22);
List<String> list = new ArrayList<>() ;
list.add("小學(xué)");
list.add("初中");
list.add("高中");
list.add("大學(xué)");
user.setEducation(list);
redisTemplate.opsForValue().set("userInfo",user);
return "success" ;
}
@RequestMapping("/getUser")
public User getUser (){
return (User)redisTemplate.opsForValue().get("userInfo") ;
}
}
三、源代碼地址
GitHub地址:知了一笑
https://github.com/cicadasmile/spring-boot-base
碼云地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base
以上就是SpringBoot集成Redis數(shù)據(jù)庫,實(shí)現(xiàn)緩存管理的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot集成Redis的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java使用mapstruct實(shí)現(xiàn)對(duì)象拷貝
MapStruct可以簡(jiǎn)化對(duì)象之間的映射,本文就來介紹一下Java使用mapstruct實(shí)現(xiàn)對(duì)象拷貝,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
spring?boot?實(shí)現(xiàn)一個(gè)?禁止重復(fù)請(qǐng)求的方法
這篇文章主要介紹了spring?boot?實(shí)現(xiàn)一個(gè)?禁止重復(fù)請(qǐng)求,當(dāng)重復(fù)請(qǐng)求該方法時(shí),會(huì)返回"Duplicate?request",避免重復(fù)執(zhí)行相同的操作,需要的朋友可以參考下2024-03-03
Java數(shù)據(jù)結(jié)構(gòu)之二叉排序樹的實(shí)現(xiàn)
二叉排序樹(Binary Sort Tree),又稱二叉查找樹(Binary Search Tree),亦稱二叉搜索樹。本文詳細(xì)介紹了二叉排序樹的原理,并且提供了Java代碼的完全實(shí)現(xiàn)。需要的可以參考一下2022-01-01
Springboot如何使用Map將錯(cuò)誤提示輸出到頁面
這篇文章主要介紹了Springboot如何使用Map將錯(cuò)誤提示輸出到頁面,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java代碼執(zhí)行shell命令的實(shí)現(xiàn)
這篇文章主要介紹了Java代碼執(zhí)行shell命令的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
springboot config 攔截器使用方法實(shí)例詳解
本文介紹Spring-Boot中使用攔截器的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
Spring?@Cacheable指定失效時(shí)間實(shí)例
這篇文章主要介紹了Spring?@Cacheable指定失效時(shí)間實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
java計(jì)算日期相差天數(shù)的4種簡(jiǎn)單方法舉例
最近在工作中遇見一個(gè)小需求,要求計(jì)算兩個(gè)日期之間相差幾天,下面這篇文章主要給大家介紹了關(guān)于java計(jì)算日期相差天數(shù)的4種簡(jiǎn)單方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06

