SpringBoot集成Redis數(shù)據(jù)庫,實(shí)現(xiàn)緩存管理
一、Redis簡介
Spring Boot中除了對常用的關(guān)系型數(shù)據(jù)庫提供了優(yōu)秀的自動(dòng)化支持之外,對于很多NoSQL數(shù)據(jù)庫一樣提供了自動(dòng)化配置的支持,包括:Redis, MongoDB, Elasticsearch。這些案例整理好后,陸續(xù)都會(huì)上傳Git。
SpringBoot2 版本,支持的組件越來越豐富,對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、簡單測試案例
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、序列化測試
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的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)LeetCode(組合總和)
這篇文章主要介紹了Java實(shí)現(xiàn)LeetCode(組合總數(shù)),本文通過使用java實(shí)現(xiàn)leetcode的組合總數(shù)題目和實(shí)現(xiàn)思路分析,需要的朋友可以參考下2021-06-06劍指Offer之Java算法習(xí)題精講鏈表與字符串及數(shù)組
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03scala+redis實(shí)現(xiàn)分布式鎖的示例代碼
這篇文章主要介紹了scala+redis實(shí)現(xiàn)分布式鎖的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06SpringBoot啟動(dòng)訪問localhost:8080報(bào)錯(cuò)404的解決操作
這篇文章主要介紹了SpringBoot啟動(dòng)訪問localhost:8080報(bào)錯(cuò)404的解決操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Java設(shè)計(jì)模式之代理模式詳細(xì)解讀
這篇文章主要介紹了Java設(shè)計(jì)模式的代理模式,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)Java設(shè)計(jì)模式的小伙伴有很大的幫助,感興趣的小伙伴可以參考一下2021-08-08Spring MVC學(xué)習(xí)教程之RequestMappingHandlerMapping匹配
這篇文章主要給大家介紹了關(guān)于Spring MVC學(xué)習(xí)教程之RequestMappingHandlerMapping匹配的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11