| | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Redis配置类 |
| | | * @author WWW |
| | | */ |
| | | @Configuration |
| | | @SuppressWarnings("ALL") |
| | | @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(); |
| | |
| | | |
| | | template.setValueSerializer(serializer); |
| | | |
| | | //使用StringRedisSerializer来序列化和反序列化redis的key值 |
| | | template.setKeySerializer(new StringRedisSerializer()); |
| | | template.setHashKeySerializer(new StringRedisSerializer()); |
| | | template.setHashValueSerializer(serializer); |
| | |
| | | @Bean |
| | | @Override |
| | | public CacheErrorHandler errorHandler() { |
| | | // 异常处理,当Redis发生异常时,打印日志,但是程序正常走 |
| | | return new CacheErrorHandler() { |
| | | @Override |
| | | public void handleCacheGetError(RuntimeException e, Cache cache, Object key) { |