+ fix problem causing atomic counters to reinitialize Redis values (even if no value was given)

This commit is contained in:
Costin Leau
2011-04-14 15:49:46 +03:00
parent d6d87a27e1
commit af2265c813
3 changed files with 15 additions and 4 deletions

View File

@@ -77,8 +77,10 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
this.generalOps = redisTemplate;
this.operations = generalOps.opsForValue();
if (initialValue == null || this.operations.get(redisCounter) == null) {
set(0);
if (initialValue == null) {
if (this.operations.get(redisCounter) == null) {
set(0);
}
}
else {
set(initialValue);

View File

@@ -77,8 +77,10 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
this.generalOps = redisTemplate;
this.operations = generalOps.opsForValue();
if (initialValue == null || this.operations.get(redisCounter) == null) {
set(0);
if (initialValue == null) {
if (this.operations.get(redisCounter) == null) {
set(0);
}
}
else {
set(initialValue);

View File

@@ -105,4 +105,11 @@ public class RedisAtomicTests {
int delta = 5;
assertEquals(delta, intCounter.addAndGet(delta));
}
@Test
public void testReadExistingValue() throws Exception {
longCounter.set(5);
RedisAtomicLong keyCopy = new RedisAtomicLong(longCounter.getKey(), factory);
assertEquals(longCounter.get(), keyCopy.get());
}
}