/**
* @Author: zc
* @Date: 2019/11/3 14:12
* @Description: SpringBoot2.0 Redis缓存配置
* @EnableCaching:开启缓存支持
*/
@Slf4j
@Configuration
@EnableCaching
public
class
RedisConfig
extends
CachingConfigurerSupport {
@Value
(
"${sys.dataCaching.expireTime:0}"
)
private
int
expireTime;
@Resource
private
LettuceConnectionFactory lettuceConnectionFactory;
@Override
@Bean
public
KeyGenerator keyGenerator() {
return
(target, method, params) -> {
StringBuilder sb =
new
StringBuilder();
sb.append(target.getClass().getName());
sb.append(
",Method:"
);
sb.append(method.getName());
sb.append(
",Params["
);
for
(
int
i =
0
; i < params.length; i++) {
sb.append(params[i].toString());
if
(i != (params.length -
1
)) {
sb.append(
","
);
}
}
sb.append(
"]"
);
log.debug(
"Data Caching Redis Key : {}"
, sb.toString());
return
sb.toString();
};
}
@Bean
public
KeyGenerator updateByIdkeyGenerator() {
return
(target, method, params) -> {
StringBuilder sb =
new
StringBuilder();
sb.append(target.getClass().getName());
sb.append(
",Method:"
);
sb.append(
"getById"
);
sb.append(
",Params["
);
try
{
Field id = params[
0
].getClass().getDeclaredField(
"id"
);
id.setAccessible(
true
);
sb.append(id.get(params[
0
]).toString());
}
catch
(IllegalAccessException e) {
e.printStackTrace();
}
catch
(NoSuchFieldException e) {
e.printStackTrace();
}
sb.append(
"]"
);
log.debug(
"Data Caching Redis Key : {}"
, sb.toString());
return
sb.toString();
};
}
@Bean
public
KeyGenerator deleteByIdkeyGenerator() {
return
(target, method, params) -> {
StringBuilder sb =
new
StringBuilder();
sb.append(target.getClass().getName());
sb.append(
",Method:"
);
sb.append(
"getById"
);
sb.append(
",Params["
);
for
(
int
i =
0
; i < params.length; i++) {
sb.append(params[i].toString());
if
(i != (params.length -
1
)) {
sb.append(
","
);
}
}
sb.append(
"]"
);
log.debug(
"Data Caching Redis Key : {}"
, sb.toString());
return
sb.toString();
};
}
@Bean
public
CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
if
(expireTime >
0
) {
log.info(
"Redis 缓存过期时间 : {}"
, expireTime);
redisCacheConfiguration.entryTtl(Duration.ofSeconds(expireTime));
}
else
{
log.info(
"Redis 未设置缓存过期时间"
);
}
return
RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
@Bean
public
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
new
Jackson2JsonRedisSerializer<Object>(
Object.
class
);
ObjectMapper om =
new
ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisTemplate<String, Object> redisTemplate =
new
RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
RedisSerializer<?> stringSerializer =
new
StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return
redisTemplate;
}
}