當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、Redis簡介
Spring Boot中除了對常用的關系型數據庫提供了優秀的自動化支持之外,對于很多NoSQL數據庫一樣提供了自動化配置的支持,包括:Redis, MongoDB, Elasticsearch。這些案例整理好后,陸續都會上傳Git。
SpringBoot2 版本,支持的組件越來越豐富,對Redis的支持不僅僅是擴展了API,更是替換掉底層Jedis的依賴,換成Lettuce。
本案例需要本地安裝一臺Redis數據庫。
二、Spring2.0集成Redis
1、核心依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency>2、配置文件
# 端口 server:port: 8008 spring:application:# 應用名稱name: node08-boot-redis# redis 配置redis:host: 127.0.0.1#超時連接timeout: 1000msjedis:pool:#最大連接數據庫連接數,設 0 為沒有限制max-active: 8#最大等待連接中的數量,設 0 為沒有限制max-idle: 8#最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。max-wait: -1ms#最小等待連接中的數量,設 0 為沒有限制min-idle: 0這樣Redis的環境就配置成功了,已經可以直接使用封裝好的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 {@Resourceprivate StringRedisTemplate stringRedisTemplate ;@RequestMapping("/setGet")public String setGet (){stringRedisTemplate.opsForValue().set("cicada","smile");return stringRedisTemplate.opsForValue().get("cicada") ;}@Resourceprivate RedisTemplate redisTemplate ;/*** 設置 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) ;/*** 序列化配置*/@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、序列化測試
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("小學");list.add("初中");list.add("高中");list.add("大學");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
總結
以上是生活随笔為你收集整理的SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Learning to see in t
- 下一篇: WinForm 自定义控件属性