SpringBoot集成Redis來實現緩存技術方案

在我們的日常項目開發過程中緩存是無處不在的,因為它可以極大的提高系統的訪問速度,關於緩存的框架也種類繁多,今天主要介紹的是使用現在非常流行的NoSQL資料庫(Redis)來實現我們的緩存需求。

Redis簡介

Redis 是一個開源(BSD許可)的,內存中的數據結構存儲系統,它可以用作資料庫、緩存和消息中間件,Redis 的優勢包括它的速度、支持豐富的數據類型、操作原子性,以及它的通用性。

案例整合

本案例是在之前一篇SpringBoot + Mybatis + RESTful的基礎上來集成Redis的,所以大家如有什麼不明白的地方可以前往my.oschina.net/feinik/b,由於篇幅原因這裡不一一貼出所有的代碼,具體完整案例代碼可以看這裡:github.com/AIFEINIK/Spr,關於Redis如何安裝可自行google。

1、在Maven pom.xml文件中加入Redis包

<!--redis--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>${boot.version}</version></dependency>

2、SpringBoot配置文件中配置Redis連接(YAML方式配置)

spring: application: name: spring-boot-redis redis: host: 192.168.145.132 port: 6379 timeout: 20000 cluster: nodes: 192.168.211.134:7000,192.168.211.134:7001,192.168.211.134:7002 maxRedirects: 6 pool: max-active: 8 min-idle: 0 max-idle: 8 max-wait: -1

解釋:本配置採用Redis一主三從的的配置方式來提高緩存的吞吐量

3、Redis配置類

@Configurationpublic class RedisConfig { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值 Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(mapper); template.setValueSerializer(serializer); //使用StringRedisSerializer來序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; }}

解釋:SpringBoot提供了對Redis的自動配置功能,在RedisAutoConfiguration中默認為我們配置了JedisConnectionFactory(客戶端連接)、RedisTemplate以及StringRedisTemplate(數據操作模板),其中StringRedisTemplate模板只針對鍵值對都是字元型的數據進行操作,本示例採用RedisTemplate作為數據操作模板,該模板默認採用JdkSerializationRedisSerializer的二進位數據序列化方式,為了方便演示本示例採用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值,使用StringRedisSerializer來序列化和反序列化redis的key值。

4、Service層應用緩存(註解方式)

@Servicepublic class PersonService { @Autowired private PersonRepo personRepo; /** * @Cacheable 應用到讀取數據的方法上,先從緩存中讀取,如果沒有再從DB獲取數據,然後把數據添加到緩存中 * unless 表示條件表達式成立的話不放入緩存 * @param username * @return */ @Cacheable(value = "user", key = "#root.targetClass + #username", unless = "#result eq null") public Person getPersonByName(String username) { Person person = personRepo.getPersonByName(username); return person; } /** * @CachePut 應用到寫數據的方法上,如新增/修改方法,調用方法時會自動把相應的數據放入緩存 * @param person * @return */ @CachePut(value = "user", key = "#root.targetClass + #result.username", unless = "#person eq null") public Person savePerson(Person person) { return personRepo.savePerson(person); } /** * @CacheEvict 應用到刪除數據的方法上,調用方法時會從緩存中刪除對應key的數據 * @param username * @return */ @CacheEvict(value = "user", key = "#root.targetClass + #username", condition = "#result eq true") public boolean removePersonByName(String username) { return personRepo.removePersonByName(username) > 0; } public boolean isExistPersonName(Person person) { return personRepo.existPersonName(person) > 0; }}

解釋:

1、這裡的緩存key為簡單的字元串組合,也可根據具體需要實現自定義的Key生成器,然後在註解中使用keyGenerator來引用。

2、Spring Cache提供了一些供我們使用的SpEL上下文數據,通過#來引用,具體可查看Spring官網:docs.spring.io/spring/d

5、數據訪問資源類

@Component@Path("personMgr")public class PersonMgrResource { @Autowired private PersonService personService; @GET @Path("getPersonByName") @Produces(MediaType.APPLICATION_JSON) public JsonResp getPersonByName(@QueryParam("username") String username) { Person person = personService.getPersonByName(username); return JsonResp.success(person); } @POST @Path("removePersonByName") @Produces(MediaType.APPLICATION_JSON) public JsonResp removePersonByName(@QueryParam("username") String username) { if (personService.removePersonByName(username)) { return JsonResp.success(); } return JsonResp.fail("系統錯誤!"); } @POST @Path("savePerson") @Produces(MediaType.APPLICATION_JSON) public JsonResp savePerson(Person person) { if (personService.isExistPersonName(person)) { return JsonResp.fail("用戶名已存在!"); } if (personService.savePerson(person).getId() > 0) { return JsonResp.success(); } return JsonResp.fail("系統錯誤!"); }}

6、通過postman工具來測試緩存是否生效

第一次訪問查找用戶:

第一次通過用戶名稱來查找用戶可以看到是從庫中查詢的數據,我們可以通過RedisClient工具來查看數據已放入了緩存

第二次查找用戶:發現服務端並未列印任何資料庫查詢日誌,可以知道第二次查詢是從緩存中查詢得到的數據。

總結

本文介紹如何通過SpringBoot來一步步集成Redis緩存,關於Redis的使用它不僅可以用作緩存,還可以用來構建隊列系統,Pub/Sub實時消息系統,分散式系統的的計數器應用,關於Redis更多的介紹,請前往查閱官方文檔。

源碼圖片展示來源SpringBoot集成Redis來實現緩存技術方案


推薦閱讀:

spring-jdbc 目前還是一個主流的廣泛使用的持久化框架嗎?
springboot怎麼學?
Spring boot與Spring cloud 是什麼關係?
關於Spring MVC的教程和例子?

TAG:SpringBoot | Redis | dubbo |