當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、Redis簡(jiǎn)介
Spring Boot中除了對(duì)常用的關(guān)系型數(shù)據(jù)庫(kù)提供了優(yōu)秀的自動(dòng)化支持之外,對(duì)于很多NoSQL數(shù)據(jù)庫(kù)一樣提供了自動(dòng)化配置的支持,包括:Redis, MongoDB, Elasticsearch。這些案例整理好后,陸續(xù)都會(huì)上傳Git。
SpringBoot2 版本,支持的組件越來(lái)越豐富,對(duì)Redis的支持不僅僅是擴(kuò)展了API,更是替換掉底層Jedis的依賴,換成Lettuce。
本案例需要本地安裝一臺(tái)Redis數(shù)據(jù)庫(kù)。
二、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: 1000msjedis:pool:#最大連接數(shù)據(jù)庫(kù)連接數(shù),設(shè) 0 為沒有限制max-active: 8#最大等待連接中的數(shù)量,設(shè) 0 為沒有限制max-idle: 8#最大建立連接等待時(shí)間。如果超過(guò)此時(shí)間將接到異常。設(shè)為-1表示無(wú)限制。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 {@Resourceprivate StringRedisTemplate stringRedisTemplate ;@RequestMapping("/setGet")public String setGet (){stringRedisTemplate.opsForValue().set("cicada","smile");return stringRedisTemplate.opsForValue().get("cicada") ;}@Resourceprivate 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 過(guò)期后,返回的是字符串 '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) ;/*** 序列化配置*/@Beanpublic 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 {@Resourceprivate 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
總結(jié)
以上是生活随笔為你收集整理的SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Learning to see in t
- 下一篇: Spring、SpringMVC、Spr