StringRedisTemplate 多余空格问题
使用StringRedisTemplate的set方法为key设置过期时间时出现了redis中的value前出现了一大串的utf8编码后的空格。经过一番调研发现,是我使用的方法错误了,误以为提供的void set(K var1, V var2, long var3);
方法就是设置过期时间的,实则上下载源码后根据注释就能看出来
/**
* Overwrite parts of {@code key} starting at the specified {@code offset} with given {@code value}.
*
* @param key must not be {@literal null}.
* @param value
* @param offset
* @see <a href="https://redis.io/commands/setrange">Redis Documentation: SETRANGE</a>
*/
void set(K key, V value, long offset);
这个注释翻译下的意思就是:将value从指定的位置开始覆盖原有的值。如果指定的开始位置大于字符串长度,先补空格在追加。
实际设置过期时间的方法为void set(K key, V value, long timeout, TimeUnit unit);
/**
* Set the {@code value} and expiration {@code timeout} for {@code key}.
*
* @param key must not be {@literal null}.
* @param value must not be {@literal null}.
* @param timeout the key expiration timeout.
* @param unit must not be {@literal null}.
* @see <a href="https://redis.io/commands/setex">Redis Documentation: SETEX</a>
*/
void set(K key, V value, long timeout, TimeUnit unit);