SpringBoot2.0+ redis object value serialization garbled modification

Hello everyone, today is a discussion on the serialization of redis cache storage objects in SpringBoot.

Redis serialization

Friends who are familiar with redis know that if the data is stored in the database through String, serialization problems will not occur. As shown in the figure:
Insert picture description here
But we will not store the string entry every time in the project, but will store the object, for example: when
Insert picture description here
we query by id, an emp object is returned. We marked him with the @Cacheable annotation, then every time after querying through id, the return value object will be stored in the cache, which is our redis.
Insert picture description here
We have queried an object with an id of 8, then open redis and we can find that an emp::8 key is automatically generated.
Insert picture description here
But when we check the value of emp::8, we will find that it is not as we imagined Similarly, the returned garbled characters are not familiar objects.
Insert picture description here
The test takes out something like
Insert picture description here
this : This is because of the serialization problem of redis. If you don't set it yourself, the default jdk serialization format will be used. So we need to modify the default serialization format. How to modify the default configuration in SpringBoot? Of course, just write one yourself and throw it into the container instead of the default settings.

@Configuration
public class MyRedisConfig {
    
    

    @Bean
    public RedisTemplate<Object, TblEmp> empRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException{
    
    
        //设置redis模板
        RedisTemplate<Object, TblEmp> template = new RedisTemplate<>();
        //为redis模板传入连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        //设置json的序列化
        Jackson2JsonRedisSerializer<TblEmp> ser = new Jackson2JsonRedisSerializer<>(TblEmp.class);
        //为redis模板设置默认的序列化方式
        template.setDefaultSerializer(ser);
        //将设置好的模板设置到容器中
        return template;
    }

    @Bean                                       //传入上面定义好序列化方式的redis模板
    public RedisCacheManager redisCacheManager(RedisTemplate<Object, TblEmp> empRedisTemplate){
    
    
        //2.0以上开始就需要传入一个redisCacheWriter参数,并传入上面写的模板对象的连接工厂   内部也是为其设置连接工厂和sleeptime
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(empRedisTemplate.getConnectionFactory());
        //在redis缓存配置文件中传入刚定义好的缓存配置empRedisTemplate.getValueSerializer()
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(empRedisTemplate.getValueSerializer()));
        //将定义好的配置文件放入Redis缓存管理器,放入容器中。
        return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
    }

}

After the configuration is completed, it will take effect by default, so that if the value we pass in is in the format of an object, it will become a json string, as shown in the following figure:
Insert picture description here
it is no problem to retrieve it through the test.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41762594/article/details/105971533