jbaiter
jbaiter

Reputation: 7099

Cannot increment RedisAtomicLong

I am trying to increment a RedisAtomicLong object by a given delta:

private void updateBandwidthUsage(String remoteAddr, int length) {
    RedisAtomicLong counter = new RedisAtomicLong("someKey", redisTemplate)
    counter.getAndAdd(length);
    counter.expire(1, TimeUnit.DAYS);
}

This fails with

redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range

When I use the MONITOR command on the server, this is what I can see:

1403019417.097887 [0 10.0.2.2:46694] "INCRBY" "\xac\xed\x00\x05t\x00\x150:0:0:0:0:0:0:1:16238" "7625"

I am using Spring Data Redis (1.3.0) with the Jedis (2.5.1) connector, the server is running Redis 2.8.6.


edit: I just noticed something weird: When I manually use set on the counter the data that is sent to Redis looks pretty weird:

1403020463.368050 [0 10.0.2.2:47127] "SET" "\xac\xed\x00\x05t\x00\x150:0:0:0:0:0:0:1:16238" "\xac\xed\x00\x05sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x00"

Upvotes: 1

Views: 3482

Answers (2)

sumit dugar
sumit dugar

Reputation: 121

I had a similar situation where i was not able to increment using RedisAtomicInteger and was getting this error "ERR value is not an integer or out of range".

I was setting my value using template.opsForValue().set(key,value) and then for incrementing I was using RedisAtomicInteger operations.

The issue here is default template uses default serializer i.e JdkSerializationRedisSerializer and RedisAtomicInteger uses StringRedisSerializer for both keys and values. So what i was doing wrong was that i was setting my value using one type of serializer and then incrementing it using another.

Using increment and set operations of default template was still giving similar error so, I used RedisAtomicInteger for all get,set,increment operations.

Upvotes: 0

jbaiter
jbaiter

Reputation: 7099

I managed to solve the issue now by instantiating the RedisAtomicLong with the redisTemplate's instance of RedisConnectionFactory:

RedisAtomicLong counter = new RedisAtomicLong("someKey", redisTemplate.getConnectionFactory());

Upvotes: 3

Related Questions