概述
在我們的日常項目開發過程中緩存是無處不在的,因為它可以極大的提高系統的訪問速度,關於緩存的框架也種類繁多,今天主要介紹的是使用現在非常流行的NoSQL數據庫(Redis)來實現我們的緩存需求。
Redis簡介
Redis 是一個開源(BSD許可)的,內存中的數據結構存儲系統,它可以用作數據庫、緩存和消息中間件,Redis 的優勢包括它的速度、支持豐富的數據類型、操作原子性,以及它的通用性。
案例整合
本案例是在之前一篇SpringBoot + Mybatis + RESTful的基礎上來集成Redis的,所以大家如有什么不明白的地方可以前往https://my.oschina.net/feinik/blog/879266,由於篇幅原因這里不一一貼出所有的代碼,具體完整案例代碼可以看這里:https://github.com/AIFEINIK/SpringBoot-Learn/tree/master/spring-boot-redis2,關於Redis如何安裝可自行google。
1、在Maven pom.xml文件中加入Redis包
|
1
2
3
4
5
6
|
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${boot.version}</version>
</dependency>
|
2、SpringBoot配置文件中配置Redis連接(YAML方式配置)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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配置類
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@Configuration
public
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層應用緩存(注解方式)
|
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
|
@Service
public
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官網:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#cache-spel-context。
5、數據訪問資源類
|
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
|
@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更多的介紹,請前往查閱官方文檔。
