• redis key/value 出现xACxEDx00x05tx00x05


    1.问题现象:

    最近使用spring-data-redis 和jedis 操作redis时发现存储在redis中的key不是程序中设置的string值,前面还多出了许多类似xacxedx00x05tx00这种字符串

    2.原因:

    spring-data-redis的RedisTemplate<K, V>模板类在操作redis时默认使用JdkSerializationRedisSerializer来进行序列化。

    public void afterPropertiesSet() {
    
            super.afterPropertiesSet();
    
            boolean defaultUsed = false;
    
            if (defaultSerializer == null) {
    
                defaultSerializer = new JdkSerializationRedisSerializer(
                        classLoader != null ? classLoader : this.getClass().getClassLoader());
            }
    
            if (enableDefaultSerializer) {
    
                if (keySerializer == null) {
                    keySerializer = defaultSerializer;
                    defaultUsed = true;
                }
                if (valueSerializer == null) {
                    valueSerializer = defaultSerializer;
                    defaultUsed = true;
                }
                if (hashKeySerializer == null) {
                    hashKeySerializer = defaultSerializer;
                    defaultUsed = true;
                }
                if (hashValueSerializer == null) {
                    hashValueSerializer = defaultSerializer;
                    defaultUsed = true;
                }
            }
    
            if (enableDefaultSerializer && defaultUsed) {
                Assert.notNull(defaultSerializer, "default serializer null and not all serializers initialized");
            }
    
            if (scriptExecutor == null) {
                this.scriptExecutor = new DefaultScriptExecutor<K>(this);
            }
    
            initialized = true;
        }

    3.解决方案

    public class RedisDao {
    
        private static final Logger log = LoggerFactory.getLogger(RedisDao.class);
    
        private RedisTemplate redisTemplate;
    
        private RedisTemplate<String, Object> redisTemplateMap;
    
        @Autowired(required = false)
        public void setRedisTemplate(RedisTemplate redisTemplate) {
            RedisSerializer stringSerializer = new StringRedisSerializer();
            redisTemplate.setKeySerializer(stringSerializer);
            redisTemplate.setValueSerializer(stringSerializer);
            redisTemplate.setHashKeySerializer(stringSerializer);
            redisTemplate.setHashValueSerializer(stringSerializer);
            this.redisTemplate = redisTemplate;
        }

    将redisTemplate默认改为stringRedis序列化方式。

  • 相关阅读:
    第七周编程总结
    第六周编程总结
    第五周编程总结
    2019 第四周作业编程总结
    第三周编程总结
    寒假作业 pta编程总结3
    跟着官网学Angular-创建响应式表单-源代码
    RxJS---转载
    NPM
    Sass
  • 原文地址:https://www.cnblogs.com/jiangds/p/9037553.html
Copyright © 2020-2023  润新知