從配置最明顯的來看,RedisCacheManager類初始化不再以RedisTemplate為參數(shù)進行初始化叶沛,順帶引起了一些變化蒲讯,遂記錄于此。
一. springboot 1.5.x 版本
1. 引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
2. application.yml/properties配置
spring:
cache:
type: redis
redis:
host: 127.0.0.1
port: 6379
timeout: 0
database: 0
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
到此springboot可以以通用方式配置redis緩存了灰署。但是如果需要特殊處理判帮,則需要自己利用代碼進行配置了,比如 序列與反序列化方式溉箕, redis緩存key的生成方案等等晦墙。
3. 按需配置
/**
* Created by xiaoyiyiyo on 2018/4/28.
*/
@EnableCaching
@Configuration
public class RedisCacheConfig extends CachingConfigurerSupport{
@Bean
public RedisTemplate redisTemplate() {
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//全局開關(guān),支持jackson在反序列是使用多態(tài)
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager manager = new RedisCacheManager(redisTemplate);
manager.setUsePrefix(true);
RedisCachePrefix cachePrefix = new RedisPrefix("prefix");
manager.setCachePrefix(cachePrefix);
// 整體緩存過期時間
manager.setDefaultExpiration(3600L);
// 設置緩存過期時間肴茄。key和緩存過期時間晌畅,單位秒
Map<String, Long> expiresMap = new HashMap<>();
expiresMap.put("user", 1000L);
manager.setExpires(expiresMap);
return manager;
}
/**
* 緩存的key是 包名+方法名+參數(shù)列表
*/
@Bean
@Override
public KeyGenerator keyGenerator() {
return (target, method, objects) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append("::" + method.getName() + ":");
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
};
}
}
二. springboot 2.x 版本
1. 引入依賴
????同1.5.x
2. application.yml/properties配置
2.x版本默認redis客戶端連接池類型有所不同。
版本 | 默認客戶端類型 |
---|---|
1.5.x | jedis |
2.x | lettuce |
所以配置連接池的時候會有不同寡痰,如下:
application.yml
#2.x版本中由于引入了不同客戶端抗楔,需要指定配置哪種連接池
#jedis客戶端
spring:
cache:
type: redis
redis:
host: 127.0.0.1
port: 6379
timeout: 0
database: 0
jedis:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
3. 按需配置
package com.xiaoyiyiyo.configuration;
@Bean
public ObjectMapper objectMapper() {
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //補上
return om;
}
@Bean
public GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer (ObjectMapper om) {
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(om);
return serializer;
}
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory,
GenericJackson2JsonRedisSerializer serializer) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);//key序列化
redisTemplate.setValueSerializer(serializer);//value序列化
redisTemplate.setHashKeySerializer(stringSerializer);//Hash key序列化
redisTemplate.setHashValueSerializer(serializer);//Hash value序列化
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate ) {
RedisCacheManager manager = new RedisCacheManager(redisTemplate);
manager.setUsePrefix(true);
// 整體緩存過期時間
manager.setDefaultExpiration(3600L);
// 設置緩存過期時間。key和緩存過期時間拦坠,單位秒
Map<String, Long> expiresMap = new HashMap<>();
expiresMap.put("user", 1000L);
manager.setExpires(expiresMap);
return manager;
}
// @Bean
// public RedisCacheConfiguration redisCacheConfiguration(GenericJackson2JsonRedisSerializer serializer) {
// return RedisCacheConfiguration
// .defaultCacheConfig()
// .serializeKeysWith(
// RedisSerializationContext
// .SerializationPair
// .fromSerializer(new StringRedisSerializer()))
// .serializeValuesWith(
// RedisSerializationContext
// .SerializationPair
// .fromSerializer(serializer));
// }
@Bean
@Override
public KeyGenerator keyGenerator() {
return (target, method, objects) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append("::" + method.getName() + ":");
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
};
}