From da5f1323369ec00aaf59ee5b376ac00a1d9784b2 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 24 Feb 2011 20:58:06 +0200 Subject: [PATCH] + improve getAndXXX operations (taking advantage of the increment operation which is already atomic). --- .../support/atomic/RedisAtomicInteger.java | 32 +++---------------- .../redis/support/atomic/RedisAtomicLong.java | 31 ++---------------- 2 files changed, 7 insertions(+), 56 deletions(-) diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicInteger.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicInteger.java index 653201532..afac4fa7e 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicInteger.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicInteger.java @@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.redis.support.atomic; import java.io.Serializable; import java.util.Collections; -import java.util.concurrent.Callable; import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory; import org.springframework.data.keyvalue.redis.core.KeyBound; @@ -172,18 +171,11 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound /** * Atomically increment by one the current value. + * * @return the previous value */ public int getAndIncrement() { - return CASUtils.execute(generalOps, key, new Callable() { - @Override - public Integer call() throws Exception { - int value = get(); - generalOps.multi(); - operations.increment(key, 1); - return value; - } - }); + return incrementAndGet() - 1; } @@ -192,15 +184,7 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound * @return the previous value */ public int getAndDecrement() { - return CASUtils.execute(generalOps, key, new Callable() { - @Override - public Integer call() throws Exception { - int value = get(); - generalOps.multi(); - operations.increment(key, -1); - return value; - } - }); + return decrementAndGet() + 1; } @@ -210,15 +194,7 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound * @return the previous value */ public int getAndAdd(final int delta) { - return CASUtils.execute(generalOps, key, new Callable() { - @Override - public Integer call() throws Exception { - int value = get(); - generalOps.multi(); - set(value + delta); - return value; - } - }); + return addAndGet(delta) - delta; } /** diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicLong.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicLong.java index b1c6c8bbf..c3005a6da 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicLong.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicLong.java @@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.redis.support.atomic; import java.io.Serializable; import java.util.Collections; -import java.util.concurrent.Callable; import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory; import org.springframework.data.keyvalue.redis.core.KeyBound; @@ -177,15 +176,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound() { - @Override - public Long call() throws Exception { - long value = get(); - generalOps.multi(); - operations.increment(key, 1); - return value; - } - }); + return incrementAndGet() - 1; } /** @@ -194,15 +185,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound() { - @Override - public Long call() throws Exception { - long value = get(); - generalOps.multi(); - operations.increment(key, -11); - return value; - } - }); + return decrementAndGet() + 1; } /** @@ -212,15 +195,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound() { - @Override - public Long call() throws Exception { - long value = get(); - generalOps.multi(); - set(value + delta); - return value; - } - }); + return addAndGet(delta) - delta; } /**