Search code examples
javaredisjedisspring-data-redis

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"

Solution

  • 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());