背景
使用spring redis的increment方法时,爆出如上错误,详细异常栈如下
org.springframework.dao.InvalidDataAccessApiUsageException: ERR value is not an integer or out of range; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:64)
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41)
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37)
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37)
at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:212)
at org.springframework.data.redis.connection.jedis.JedisConnection.incrBy(JedisConnection.java:1439)
at org.springframework.data.redis.core.DefaultValueOperations$3.doInRedis(DefaultValueOperations.java:66)
at org.springframework.data.redis.core.DefaultValueOperations$3.doInRedis(DefaultValueOperations.java:63)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:202)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:164)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:88)
at org.springframework.data.redis.core.DefaultValueOperations.increment(DefaultValueOperations.java:63)
分析
redis对任何不合法的值,都称为ERR。
也就是说,上述异常的意思是,增加的值(ERR)不是integer或者超出了integer范围,所以才报的错误。
根据同事的建议,只有使用StringRedisSerializer序列化器才能使用incrment方法!那么为什么呢,为什么使用了StringRedisSerializer就可以而使用默认序列化器就不行呢?
实验
我们都知道序列化器是,将key或者value序列化成流,存储到redis。首先看一下RedisSerializer接口
它有两个方法,存储到redis时,使用的是serialize(T t)方法
为了测试各个序列化器的区别,我做了个测试
@Test
public void testSerializer(){
RedisTemplate<String, String> template = new RedisTemplate<String, String>();
template.setConnectionFactory(connectFactory);
template.afterPropertiesSet();
ValueOperations<String, String> operations = template.opsForValue();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
operations.set("StringRedisSerializer", "1");
template.setKeySerializer(new GenericToStringSerializer<String>(String.class));
template.setValueSerializer(new GenericToStringSerializer<String>(String.class));
operations.set("GenericToStringSerializer", "1");
template.setKeySerializer(new GenericJackson2JsonRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
operations.set("GenericJackson2JsonRedisSerializer", "1");
template.setKeySerializer(new Jackson2JsonRedisSerializer<String>(String.class));
template.setValueSerializer(new Jackson2JsonRedisSerializer<String>(String.class));
operations.set("Jackson2JsonRedisSerializer", "1");
template.setKeySerializer(new JdkSerializationRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
operations.set("JdkSerializationRedisSerializer", "1");
}
结果
领悟
至此真相大白,
- GenericJackson2JsonRedisSerializer、Jackson2JsonRedisSerializer是先将对象转为json,然后再保存到redis,所以,1在redis中是字符串1,所以无法进行加1
- JdkSerializationRedisSerializer使用的jdk对象序列化,序列化后的值有类信息、版本号等,所以是一个包含很多字母的字符串,所以根本无法加1,这个序列化器跟memcache的序列化规则很像memcache怎样存储的对象
- GenericToStringSerializer、StringRedisSerializer将字符串的值直接转为字节数组,所以保存到redis中是数字,所以可以进行加1
最后,看一下StringRedisSerializer的源代码
GenericToStringSerializer serialize方法
public byte[] serialize(T object) {
if (object == null) {
return null;
}
String string = converter.convert(object, String.class);
return string.getBytes(charset);
}
结论
使用GenericToStringSerializer、StringRedisSerializer序列化器,都可以使用increment方法