Spring Boot 整合 Redis 實現緩存操做

摘要: 原創出處 www.bysocket.com 「泥瓦匠BYSocket 」歡迎轉載,保留摘要,謝謝!
 
『 產品沒有價值,開發團隊再優秀也無濟於事 – 《啓示錄》 』
 
本文提綱
1、緩存的應用場景
2、更新緩存的策略
3、運行 springboot-mybatis-redis 工程案例
4、springboot-mybatis-redis 工程代碼配置詳解
 
運行環境
Mac OS 10.12.x
JDK 8 +
Redis 3.2.8
Spring Boot 1.5.1.RELEASE
 

1、緩存的應用場景

什麼是緩存?
在互聯網場景下,尤爲 2C 端大流量場景下,須要將一些常常展示和不會頻繁變動的數據,存放在存取速率更快的地方。緩存就是一個存儲器,在技術選型中,經常使用 Redis 做爲緩存數據庫。緩存主要是在獲取資源方便性能優化的關鍵方面。
 
Redis 是一個高性能的 key-value 數據庫。GitHub 地址: https://github.com/antirez/redis 。Github 是這麼描述的:
Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, HyperLogLogs, Bitmaps.
 
緩存的應用場景有哪些呢?
好比常見的電商場景,根據商品 ID 獲取商品信息時,店鋪信息和商品詳情信息就能夠緩存在 Redis,直接從 Redis 獲取。減小了去數據庫查詢的次數。但會出現新的問題,就是如何對緩存進行更新?這就是下面要講的。
 

2、更新緩存的策略

參考《緩存更新的套路》 http://coolshell.cn/articles/17416.html,緩存更新的模式有四種:Cache aside, Read through, Write through, Write behind caching。
 
這裏咱們使用的是 Cache Aside 策略,從三個維度:(摘自 耗子叔叔博客)
失效:應用程序先從cache取數據,沒有獲得,則從數據庫中取數據,成功後,放到緩存中。
命中:應用程序從cache中取數據,取到後返回。
更新:先把數據存到數據庫中,成功後,再讓緩存失效。
 
大體流程以下:
獲取商品詳情舉例
a. 從商品 Cache 中獲取商品詳情,若是存在,則返回獲取 Cache 數據返回。
b. 若是不存在,則從商品 DB 中獲取。獲取成功後,將數據存到 Cache 中。則下次獲取商品詳情,就能夠從 Cache 就能夠獲得商品詳情數據。
c. 從商品 DB 中更新或者刪除商品詳情成功後,則從緩存中刪除對應商品的詳情緩存
 
 

3、運行 springboot-mybatis-redis 工程案例

 
git clone 下載工程 springboot-learning-example ,項目地址見 GitHub –  https://github.com/JeffLi1993/springboot-learning-example
 
下面開始運行工程步驟(Quick Start):
 
1.數據庫和 Redis 準備
a.建立數據庫 springbootdb:
1
CREATE DATABASE springbootdb;
 
b.建立表 city :(由於我喜歡徒步)
1
2
3
4
5
6
7
8
DROP TABLE IF EXISTS  `city`;
CREATE TABLE `city` (
   ` id ` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市編號' ,
   `province_id` int(10) unsigned  NOT NULL COMMENT '省份編號' ,
   `city_name` varchar(25) DEFAULT NULL COMMENT '城市名稱' ,
   `description` varchar(25) DEFAULT NULL COMMENT '描述' ,
   PRIMARY KEY (` id `)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
 
c.插入數據
1
INSERT city VALUES (1 ,1, '溫嶺市' , 'BYSocket 的家在溫嶺。' );
 
d.本地安裝 Redis
詳見寫過的文章《 Redis 安裝 》 http://www.bysocket.com/?p=917
 
2. springboot-mybatis-redis 工程項目結構介紹
1
2
3
4
5
6
7
springboot-mybatis-redis 工程項目結構以下圖所示:
org.spring.springboot.controller - Controller 層
org.spring.springboot.dao - 數據操做層 DAO
org.spring.springboot.domain - 實體類
org.spring.springboot.service - 業務邏輯層
Application - 應用啓動類
application.properties - 應用配置文件,應用啓動會自動讀取配置
 
3.改數據庫配置
打開 application.properties 文件, 修改相應的數據源配置,好比數據源地址、帳號、密碼等。
(若是不是用 MySQL,自行添加鏈接驅動 pom,而後修改驅動名配置。)
 
4.編譯工程
在項目根目錄 springboot-learning-example,運行 maven 指令:
1
mvn clean install
 
5.運行工程
右鍵運行 springboot-mybatis-redis 工程 Application 應用啓動類的 main 函數。
 
項目運行成功後,這是個 HTTP OVER JSON 服務項目。因此用 postman 工具能夠以下操做
根據 ID,獲取城市信息
GET http://127.0.0.1:8080/api/city/1
再請求一次,獲取城市信息會發現數據獲取的耗時快了不少。服務端 Console 輸出的日誌:
1
2
2017-04-13 18:29:00.273  INFO 13038 --- [nio-8080- exec -1] o.s.s.service.impl.CityServiceImpl       : CityServiceImpl.findCityById() : 城市插入緩存 >> City{ id =12, provinceId=3, cityName= '三亞' , description= '水好,天藍' }
2017-04-13 18:29:03.145  INFO 13038 --- [nio-8080- exec -2] o.s.s.service.impl.CityServiceImpl       : CityServiceImpl.findCityById() : 從緩存中獲取了城市 >> City{ id =12, provinceId=3, cityName= '三亞' , description= '水好,天藍' }
可見,第一次是從數據庫 DB 獲取數據,並插入緩存,第二次直接從緩存中取。
 
更新城市信息
PUT http://127.0.0.1:8080/api/city
刪除城市信息
DELETE http://127.0.0.1:8080/api/city/2
這兩種操做中,若是緩存有對應的數據,則刪除緩存。服務端 Console 輸出的日誌:
1
2017-04-13 18:29:52.248  INFO 13038 --- [nio-8080- exec -9] o.s.s.service.impl.CityServiceImpl       : CityServiceImpl.deleteCity() : 從緩存中刪除城市 ID >> 12
 

4、springboot-mybatis-redis 工程代碼配置詳解

這裏,我強烈推薦 註解 的方式實現對象的緩存。可是這裏爲了更好說明緩存更新策略。下面講講工程代碼的實現。
 
pom.xml 依賴配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version= "1.0" encoding= "UTF-8" ?>
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
     <modelVersion>4.0.0< /modelVersion >
 
     <groupId>springboot< /groupId >
     <artifactId>springboot-mybatis-redis< /artifactId >
     <version>0.0.1-SNAPSHOT< /version >
     <name>springboot-mybatis-redis :: 整合 Mybatis 並使用 Redis 做爲緩存< /name >
 
     <!-- Spring Boot 啓動父依賴 -->
     <parent>
         <groupId>org.springframework.boot< /groupId >
         <artifactId>spring-boot-starter-parent< /artifactId >
         <version>1.5.1.RELEASE< /version >
     < /parent >
 
     <properties>
         <mybatis-spring-boot>1.2.0< /mybatis-spring-boot >
         <mysql-connector>5.1.39< /mysql-connector >
         <spring-boot-starter-redis-version>1.3.2.RELEASE< /spring-boot-starter-redis-version >
     < /properties >
 
     <dependencies>
 
         <!-- Spring Boot Reids 依賴 -->
         <dependency>
             <groupId>org.springframework.boot< /groupId >
             <artifactId>spring-boot-starter-redis< /artifactId >
             <version>${spring-boot-starter-redis-version}< /version >
         < /dependency >
 
         <!-- Spring Boot Web 依賴 -->
         <dependency>
             <groupId>org.springframework.boot< /groupId >
             <artifactId>spring-boot-starter-web< /artifactId >
         < /dependency >
 
         <!-- Spring Boot Test 依賴 -->
         <dependency>
             <groupId>org.springframework.boot< /groupId >
             <artifactId>spring-boot-starter- test < /artifactId >
             <scope> test < /scope >
         < /dependency >
 
         <!-- Spring Boot Mybatis 依賴 -->
         <dependency>
             <groupId>org.mybatis.spring.boot< /groupId >
             <artifactId>mybatis-spring-boot-starter< /artifactId >
             <version>${mybatis-spring-boot}< /version >
         < /dependency >
 
         <!-- MySQL 鏈接驅動依賴 -->
         <dependency>
             <groupId>mysql< /groupId >
             <artifactId>mysql-connector-java< /artifactId >
             <version>${mysql-connector}< /version >
         < /dependency >
 
         <!-- Junit -->
         <dependency>
             <groupId>junit< /groupId >
             <artifactId>junit< /artifactId >
             <version>4.12< /version >
         < /dependency >
     < /dependencies >
< /project >
包括了 Spring Boot Reids 依賴、 MySQL 依賴和 Mybatis 依賴。
 
 
在 application.properties 應用配置文件,增長 Redis 相關配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
## 數據源配置
spring.datasource.url=jdbc:mysql: //localhost :3306 /springbootdb ?useUnicode= true &characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 
## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml
 
## Redis 配置
## Redis數據庫索引(默認爲0)
spring.redis.database=0
## Redis服務器地址
spring.redis.host=127.0.0.1
## Redis服務器鏈接端口
spring.redis.port=6379
## Redis服務器鏈接密碼(默認爲空)
spring.redis.password=
## 鏈接池最大鏈接數(使用負值表示沒有限制)
spring.redis.pool.max-active=8
## 鏈接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.pool.max-wait=-1
## 鏈接池中的最大空閒鏈接
spring.redis.pool.max-idle=8
## 鏈接池中的最小空閒鏈接
spring.redis.pool.min-idle=0
## 鏈接超時時間(毫秒)
spring.redis.timeout=0
 
詳細解釋能夠參考註釋。對應的配置類:org.springframework.boot.autoconfigure.data.redis.RedisProperties
 
CityRestController 控制層依舊是 Restful 風格的,詳情能夠參考《Springboot 實現 Restful 服務,基於 HTTP / JSON 傳輸》。  http://www.bysocket.com/?p=1627 domain 對象 City 必須實現序列化,由於須要將對象序列化後存儲到 Redis。若是沒實現 Serializable ,控制檯會爆出如下異常:
 
1
2
Serializable
java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type
 
City.java 城市對象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package org.spring.springboot.domain;
 
import java.io.Serializable;
 
/**
  * 城市實體類
  *
  * Created by bysocket on 07 /02/2017 .
  */
public class City implements Serializable {
 
     private static final long serialVersionUID = -1L;
 
     /**
      * 城市編號
      */
     private Long id ;
 
     /**
      * 省份編號
      */
     private Long provinceId;
 
     /**
      * 城市名稱
      */
     private String cityName;
 
     /**
      * 描述
      */
     private String description;
 
     public Long getId() {
         return id ;
     }
 
     public void setId(Long id ) {
         this. id = id ;
     }
 
     public Long getProvinceId() {
         return provinceId;
     }
 
     public void setProvinceId(Long provinceId) {
         this.provinceId = provinceId;
     }
 
     public String getCityName() {
         return cityName;
     }
 
     public void setCityName(String cityName) {
         this.cityName = cityName;
     }
 
     public String getDescription() {
         return description;
     }
 
     public void setDescription(String description) {
         this.description = description;
     }
 
     @Override
     public String toString() {
         return "City{" +
                 "id=" + id +
                 ", provinceId=" + provinceId +
                 ", cityName='" + cityName + '\ '' +
                 ", description='" + description + '\ '' +
                 '}' ;
     }
}
 
若是須要自定義序列化實現,只要實現 RedisSerializer 接口去實現便可,而後在使用 RedisTemplate.setValueSerializer 方法去設置你實現的序列化實現。
 
主要仍是城市業務邏輯實現類 CityServiceImpl.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.spring.springboot.service.impl;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.dao.CityDao;
import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.concurrent.TimeUnit;
 
/**
  * 城市業務邏輯實現類
  * <p>
  * Created by bysocket on 07 /02/2017 .
  */
@Service
public class CityServiceImpl implements CityService {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class);
 
     @Autowired
     private CityDao cityDao;
 
     @Autowired
     private RedisTemplate redisTemplate;
 
     /**
      * 獲取城市邏輯:
      * 若是緩存存在,從緩存中獲取城市信息
      * 若是緩存不存在,從 DB 中獲取城市信息,而後插入緩存
      */
     public City findCityById(Long id ) {
         // 從緩存中獲取城市信息
         String key = "city_" + id ;
         ValueOperations<String, City> operations = redisTemplate.opsForValue();
 
         // 緩存存在
         boolean hasKey = redisTemplate.hasKey(key);
         if (hasKey) {
             City city = operations.get(key);
 
             LOGGER.info( "CityServiceImpl.findCityById() : 從緩存中獲取了城市 >> " + city.toString());
             return city;
         }
 
         // 從 DB 中獲取城市信息
         City city = cityDao.findById( id );
 
         // 插入緩存
         operations. set (key, city, 10, TimeUnit.SECONDS);
         LOGGER.info( "CityServiceImpl.findCityById() : 城市插入緩存 >> " + city.toString());
 
         return city;
     }
 
     @Override
     public Long saveCity(City city) {
         return cityDao.saveCity(city);
     }
 
     /**
      * 更新城市邏輯:
      * 若是緩存存在,刪除
      * 若是緩存不存在,不操做
      */
     @Override
     public Long updateCity(City city) {
         Long ret = cityDao.updateCity(city);
 
         // 緩存存在,刪除緩存
         String key = "city_" + city.getId();
         boolean hasKey = redisTemplate.hasKey(key);
         if (hasKey) {
             redisTemplate.delete(key);
 
             LOGGER.info( "CityServiceImpl.updateCity() : 從緩存中刪除城市 >> " + city.toString());
         }
 
         return ret;
     }
 
     @Override
     public Long deleteCity(Long id ) {
 
         Long ret = cityDao.deleteCity( id );
 
         // 緩存存在,刪除緩存
         String key = "city_" + id ;
         boolean hasKey = redisTemplate.hasKey(key);
         if (hasKey) {
             redisTemplate.delete(key);
 
             LOGGER.info( "CityServiceImpl.deleteCity() : 從緩存中刪除城市 ID >> " + id );
         }
         return ret;
     }
 
}
 
首先這裏注入了 RedisTemplate 對象。聯想到 Spring 的 JdbcTemplate ,RedisTemplate 封裝了 RedisConnection,具備鏈接管理,序列化和 Redis 操做等功能。還有針對 String 的支持對象 StringRedisTemplate。
Redis 操做視圖接口類用的是 ValueOperations,對應的是 Redis String/Value 操做。還有其餘的操做視圖,ListOperations、SetOperations、ZSetOperations 和 HashOperations 。ValueOperations 插入緩存是能夠設置失效時間,這裏設置的失效時間是 10 s。
 
回到更新緩存的邏輯
a. findCityById 獲取城市邏輯:
若是緩存存在,從緩存中獲取城市信息
若是緩存不存在,從 DB 中獲取城市信息,而後插入緩存
 
b. deleteCity 刪除 / updateCity 更新城市邏輯:
若是緩存存在,刪除
若是緩存不存在,不操做
 
其餘不明白的,能夠 git clone 下載工程 springboot-learning-example ,工程代碼註解很詳細。 https://github.com/JeffLi1993/springboot-learning-example。
 

5、小結

本文涉及到 Spring Boot 在使用 Redis 緩存時,一個是緩存對象須要序列化,二個是緩存更新策略是如何的。
 
 
歡迎掃一掃個人公衆號關注 — 及時獲得博客訂閱哦!
相關文章
相關標籤/搜索