package cn.cetc54.platform.core.config.cache;
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.cache.Cache;
|
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
import org.springframework.cache.interceptor.CacheErrorHandler;
|
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.serializer.Jackson2JsonRedisSerializer;
|
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;
|
|
/**
|
* @author Exrick
|
*/
|
@Slf4j
|
@Configuration
|
public class RedisCacheConfig extends CachingConfigurerSupport {
|
|
@Value("${platform.cache.unit:day}")
|
private String unit;
|
|
@Value("${platform.cache.time:-1}")
|
private Integer time;
|
|
/**
|
* 自定义序列化方式
|
* @param factory
|
* @return
|
*/
|
@Bean
|
public CacheManager cacheManager(RedisConnectionFactory factory) {
|
|
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
|
// 解决查询缓存转换异常的问题
|
ObjectMapper om = new ObjectMapper();
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
om.activateDefaultTyping(new ObjectMapper().getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
|
jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
// 配置序列化(解决乱码的问题)
|
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
|
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
|
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
|
.disableCachingNullValues();
|
|
if(time<0){
|
time = -1;
|
}
|
Duration expireTime;
|
if("hour".equals(unit)){
|
expireTime = Duration.ofHours(time);
|
}else if("minute".equals(unit)){
|
expireTime = Duration.ofMinutes(time);
|
}else{
|
expireTime = Duration.ofDays(time);
|
}
|
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
|
.cacheDefaults(config.entryTtl(expireTime))
|
.build();
|
return cacheManager;
|
}
|
|
/**
|
* 异常处理 当Redis缓存相关操作发生异常时 打印日志 程序正常走
|
* @return
|
*/
|
@Override
|
public CacheErrorHandler errorHandler(){
|
|
CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() {
|
@Override
|
public void handleCacheGetError(RuntimeException e, Cache cache, Object key) {
|
log.warn("Redis occur handleCacheGetError:key: [{}]", key);
|
}
|
@Override
|
public void handleCachePutError(RuntimeException e, Cache cache, Object key, Object value) {
|
log.warn("Redis occur handleCachePutError:key: [{}];value: [{}]", key, value);
|
}
|
@Override
|
public void handleCacheEvictError(RuntimeException e, Cache cache, Object key) {
|
log.warn("Redis occur handleCacheEvictError:key: [{}]", key);
|
}
|
@Override
|
public void handleCacheClearError(RuntimeException e, Cache cache) {
|
log.warn("Redis occur handleCacheClearError");
|
}
|
};
|
return cacheErrorHandler;
|
}
|
}
|