package com.terra.common.config;
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
import org.springframework.cache.Cache;
|
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.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* Redis配置类
|
* @author WWW
|
*/
|
@Configuration
|
@AutoConfigureAfter(RedisAutoConfiguration.class)
|
public class RedisConfig extends CachingConfigurerSupport {
|
/**
|
* 配置自定义redisTemplate
|
*/
|
@SuppressWarnings("deprecation")
|
@Bean
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
template.setConnectionFactory(redisConnectionFactory);
|
|
// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
|
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(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.setHashKeySerializer(new StringRedisSerializer());
|
template.setHashValueSerializer(serializer);
|
template.afterPropertiesSet();
|
|
return template;
|
}
|
|
@Bean
|
@Override
|
public CacheErrorHandler errorHandler() {
|
// 异常处理,当Redis发生异常时,打印日志,但是程序正常走
|
return new CacheErrorHandler() {
|
@Override
|
public void handleCacheGetError(RuntimeException e, Cache cache, Object key) {
|
Map<String, Object> error = new HashMap<String, Object>(3);
|
error.put("e", e);
|
error.put("cache", cache);
|
error.put("key", key);
|
}
|
|
@Override
|
public void handleCachePutError(RuntimeException e, Cache cache, Object key, Object value) {
|
Map<String, Object> error = new HashMap<String, Object>(4);
|
error.put("e", e);
|
error.put("cache", cache);
|
error.put("key", key);
|
error.put("value", value);
|
}
|
|
@Override
|
public void handleCacheEvictError(RuntimeException e, Cache cache, Object key) {
|
Map<String, Object> error = new HashMap<String, Object>(3);
|
error.put("e", e);
|
error.put("cache", cache);
|
error.put("key", key);
|
}
|
|
@Override
|
public void handleCacheClearError(RuntimeException e, Cache cache) {
|
Map<String, Object> error = new HashMap<String, Object>(2);
|
error.put("e", e);
|
error.put("cache", cache);
|
}
|
};
|
}
|
}
|