【redis】set后value出现空格
2019-04-02 14:29
84 查看
【redis】set后value出现空格
问题描述
第三方接口需要获取一个token进行请求,该token有效期为5分钟,因此获取一次token后利用redis进行缓存,减少多次获取token的开销。但是在测试中发现,获取token正确,存入redis后再取出原token中会多出很多空格。如下
[2019-03-18 11:48:57,838] - source:{"token":"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000regsetw"}
解决方案
通过打日志定位这些空格是在redis存取时出现的,因此重点检查redis set操作。发现代码中使用的set函数没有输入第四个参数TimeUnit,即为
void set(K var1, V var2, long var3);
此处看不出var3的含义,继续看该函数的实现
public void set(K key, V value, long offset) { byte[] rawKey = this.rawKey(key); byte[] rawValue = this.rawValue(value); this.execute((connection) -> { connection.setRange(rawKey, rawValue, offset); return null; }, true); }
可以明显看出,var3实际上是offset,它的作用是在setRange中,不是我之前想要的过期时间。该函数的意义是:将value从指定的位置开始覆盖原有的值。如果指定的开始位置大于字符串长度,先补空格在追加。
想要设置过期时间,应使用
void set(K var1, V var2, long var3, TimeUnit var5);
总结
void set(K var1, V var2, long var3);void set(K var1, V var2, long var3, TimeUnit var5);
这两个set方法不是简单的省略时间单位使用默认单位的关系,而是完全不同的两个方法。
//将value从var3的位置开始覆盖原有的值。如果指定的开始位置大于字符串长度,先补空格在追加。 void set(K var1, V var2, long var3);//设置过期时间为var3,单位为var5 void set(K var1, V var2, long var3, TimeUnit var5);
相关文章推荐
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- IFeatureBuffer.set_Value出现错误:“object”未包含“set_Value”的定义
- SpringBoot操作Redis时key值出现\xac\xed\x00\x05t\x00\tb,value也是同样的乱码
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- struts2.x 的提交表单出现 Could not create and/or set value back on to object
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- Spring 整合Redis 出现 afterPropertiesSet signature: ()V) Incompatible argument to function 解决办法
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- Redis - 解决reshard 出现的bug :Syntax error ,try CLIENT (LIST|KILL|GETNAME|SETNAME|PAUSE|REPLY)
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- HTML中属性值value值中不能出现空格解决方法
- Spring 整合Redis 出现 afterPropertiesSet signature: ()V) Incompatible argument to function 解决办法
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- 使用RedisTemplate键出现乱码(Can not load key value.Key was removed or redis-server went away.)
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- Tomcat启动多项目出现Web app root system property already set to different value: 'webapp.root'解决方案
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
- [备忘]Redis运行出现Client sent AUTH, but no password is set
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍