SpringBoot2.0 Cache自定义key/value序列化

前言

Sping Boot2.0.3变化太多了,今天写redis缓存配置的时候又发现了RedisCacheManage的配置又相比SpringBoot 1.5变化了很多,再这再备注一下🖊

SpringBoot2.0 Cache自定义key/value序列化

1、pom依赖

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、配置yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
spring:
cache:
redis:
use-key-prefix: true
time-to-live: 1d
redis:
port: 6379
host: ip
password: pwd
database: 0
timeout: 1s
jedis:
pool:
max-active: 8
max-idle: 50
min-idle: 0
max-wait: -1ms

3、自定义CacheConfiguration 类

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
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@Log4j2
@Configuration
@EnableCaching
@ConfigurationProperties(prefix = "spring.cache.redis")
public class CacheConfiguration {
private Duration timeToLive = Duration.ZERO;
public void setTimeToLive(Duration timeToLive) {
this.timeToLive = timeToLive;
}

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(this.timeToLive)
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
.disableCachingNullValues();

RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.transactionAware()
.build();

log.debug("自定义RedisCacheManager加载完成");
return redisCacheManager;
}

@Bean(name = "redisTemplate")
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);

redisTemplate.setKeySerializer(keySerializer());
redisTemplate.setHashKeySerializer(keySerializer());
redisTemplate.setValueSerializer(valueSerializer());
redisTemplate.setHashValueSerializer(valueSerializer());

log.debug("自定义RedisTemplate加载完成");
return redisTemplate;
}

private RedisSerializer<String> keySerializer() {
return new StringRedisSerializer();
}

private RedisSerializer<Object> valueSerializer() {
return new GenericJackson2JsonRedisSerializer();
}

}

本文标题:SpringBoot2.0 Cache自定义key/value序列化

文章作者:Aaron.H

发布时间:2018年08月01日 - 21:08

最后更新:2018年09月06日 - 16:09

原始链接:https://uncleaaron.github.io/Blog/JavaWeb/Cache自定义key-value序列化/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。