SpringBoot 2.x版本 redis缓存序列化

网上教程大多是1.x版本,有的2.x版本也不太好用,这里我搜集到了合适的方法:

@Configuration
public class MyRedisConfig {

	//Test序列化
    @Bean
    public RedisTemplate<Object, Employee> MyredisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {
        RedisTemplate<Object,Employee> template = new RedisTemplate<Object,Employee>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
        template.setDefaultSerializer(ser);
        return template;
    }
 	 //Controller序列化
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory){
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofDays(1))
                .disableCachingNullValues()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();}


}

SpringBoot 2.x版本 redis缓存序列化_第1张图片

你可能感兴趣的:(经验)