redis 序列化乱码导致 存储和 拿出数据比较失败

redis 序列化乱码导致 存储和 拿出数据比较失败

  • 在存入redis的模块添加以下配置类 保证存入与 取出的数据一致

在存入redis的模块添加以下配置类 保证存入与 取出的数据一致



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.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        //默认使用的jdk的序列号器,我们需要修改它默认的序列号器即可
        template.setKeySerializer(new StringRedisSerializer()); //键使用的字符串类型、
        //值的序列号器我使用默认的jdk的序列号器,因为它支持任意对象,而且不需要我们手动转换为json. 弊端:值部分在redis中是乱码。
//        template.setValueSerializer(new StringRedisSerializer()); //值是任意类型,以后我们有可能存储Dish、Category、setmeal  LOcaldateTime类型没法反序列化
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

你可能感兴趣的:(redis,缓存,nosql)