From 9eb8475c57106702c72805f0ba1e8c88ee781df4 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Mon, 8 Nov 2010 14:11:28 +0200 Subject: [PATCH] + cleanup AtomicInteger impl. --- .../redis/util/RedisAtomicInteger.java | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisAtomicInteger.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisAtomicInteger.java index 06e0372a2..6ef244e98 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisAtomicInteger.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisAtomicInteger.java @@ -21,12 +21,15 @@ import org.springframework.datastore.redis.connection.RedisCommands; /** * Atomic integer backed by Redis. + * Uses Redis atomic increment/decrement and watch/multi/exec commands for CAS operations. * * @see java.util.concurrent.atomic.AtomicInteger * @author Costin Leau */ public class RedisAtomicInteger extends Number implements Serializable { + private static final long serialVersionUID = 5984507176128031015L; + private final String key; private RedisCommands commands; @@ -92,10 +95,13 @@ public class RedisAtomicInteger extends Number implements Serializable { */ public int getAndIncrement() { for (;;) { - int current = get(); - int next = current + 1; - if (compareAndSet(current, next)) - return current; + commands.watch(key); + int value = get(); + commands.multi(); + commands.incr(key); + if (commands.exec() != null) { + return value; + } } } @@ -106,10 +112,13 @@ public class RedisAtomicInteger extends Number implements Serializable { */ public int getAndDecrement() { for (;;) { - int current = get(); - int next = current - 1; - if (compareAndSet(current, next)) - return current; + commands.watch(key); + int value = get(); + commands.multi(); + commands.decr(key); + if (commands.exec() != null) { + return value; + } } } @@ -121,10 +130,13 @@ public class RedisAtomicInteger extends Number implements Serializable { */ public int getAndAdd(int delta) { for (;;) { - int current = get(); - int next = current + delta; - if (compareAndSet(current, next)) - return current; + commands.watch(key); + int value = get(); + commands.multi(); + set(value + delta); + if (commands.exec() != null) { + return value; + } } }