🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 配置 1.spring boot 默认使用的是Lettuce 2.如果使用Jedis则要引入Jedis并设置 RedisConnectionFactory为JedisConnectionFactory 3.推荐使用Lettuce 因为他使用Netty避免线程浪费 ~~~ /** * @author ZengMingyong * @since 2022/2/27 */ @Configuration @Slf4j public class RedisConfig { /** * 注入redisTemplate并且配置序列化and反序列化 * @return */ @Bean("redisTemplate") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // key 序列化 // 使用Jackson2JsonRedisSerialize替换默认序列化 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.activateDefaultTyping(new LaissezFaireSubTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); template.setKeySerializer(new StringRedisSerializer()); template.setStringSerializer(new StringRedisSerializer()); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); log.info("redis template 初始化完成"); return template; } } ~~~