diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java
index 164919eb2..09a26027f 100644
--- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java
+++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java
@@ -45,7 +45,8 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
/**
- * Constructs a new RedisAtomicInteger instance.
+ * Constructs a new RedisAtomicInteger instance. Uses the value existing
+ * in Redis or 0 if none is found.
*
* @param redisCounter redis counter
* @param factory connection factory
@@ -57,14 +58,36 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
/**
* Constructs a new RedisAtomicInteger instance.
*
- * @param redisCounter
- * @param factory
- * @param initialValue
+ * @param redisCounter the redis counter
+ * @param factory the factory
+ * @param initialValue the initial value
*/
public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, int initialValue) {
this(redisCounter, factory, Integer.valueOf(initialValue));
}
+ /**
+ * Constructs a new RedisAtomicInteger instance. Uses the value existing
+ * in Redis or 0 if none is found.
+ *
+ * @param redisCounter the redis counter
+ * @param template the template
+ */
+ public RedisAtomicInteger(String redisCounter, RedisOperations template) {
+ this(redisCounter, template, null);
+ }
+
+ /**
+ * Constructs a new RedisAtomicInteger instance.
+ *
+ * @param redisCounter the redis counter
+ * @param template the template
+ * @param initialValue the initial value
+ */
+ public RedisAtomicInteger(String redisCounter, RedisOperations template, int initialValue) {
+ this(redisCounter, template, Integer.valueOf(initialValue));
+ }
+
private RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, Integer initialValue) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setKeySerializer(new StringRedisSerializer());
@@ -87,6 +110,20 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
}
}
+ private RedisAtomicInteger(String redisCounter, RedisOperations template, Integer initialValue) {
+ this.key = redisCounter;
+ this.generalOps = template;
+ this.operations = generalOps.opsForValue();
+
+ if (initialValue == null) {
+ if (this.operations.get(redisCounter) == null) {
+ set(0);
+ }
+ }
+ else {
+ set(initialValue);
+ }
+ }
/**
* Get the current value.
*
diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java
index 2465c97dc..7299ec72e 100644
--- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java
+++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java
@@ -29,6 +29,7 @@ import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
+import org.springframework.util.Assert;
/**
* Atomic long backed by Redis.
@@ -45,7 +46,8 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
/**
- * Constructs a new RedisAtomicLong instance.
+ * Constructs a new RedisAtomicLong instance. Uses the value existing
+ * in Redis or 0 if none is found.
*
* @param redisCounter redis counter
* @param factory connection factory
@@ -66,6 +68,9 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
private RedisAtomicLong(String redisCounter, RedisConnectionFactory factory, Long initialValue) {
+ Assert.hasText(redisCounter, "a valid counter name is required");
+ Assert.notNull(factory, "a valid factory is required");
+
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericToStringSerializer(Long.class));
@@ -87,6 +92,47 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
}
+ /**
+ * Constructs a new RedisAtomicLong instance. Uses the value existing
+ * in Redis or 0 if none is found.
+ *
+ * @param redisCounter the redis counter
+ * @param template the template
+ */
+ public RedisAtomicLong(String redisCounter, RedisOperations template) {
+ this(redisCounter, template, null);
+ }
+
+ /**
+ * Constructs a new RedisAtomicLong instance.
+ *
+ * @param redisCounter the redis counter
+ * @param template the template
+ * @param initialValue the initial value
+ */
+ public RedisAtomicLong(String redisCounter, RedisOperations template, long initialValue) {
+ this(redisCounter, template, Long.valueOf(initialValue));
+ }
+
+ private RedisAtomicLong(String redisCounter, RedisOperations template, Long initialValue) {
+ Assert.hasText(redisCounter, "a valid counter name is required");
+ Assert.notNull(template, "a valid template is required");
+
+ this.key = redisCounter;
+ this.generalOps = template;
+ this.operations = generalOps.opsForValue();
+
+ if (initialValue == null) {
+ if (this.operations.get(redisCounter) == null) {
+ set(0);
+ }
+ }
+ else {
+ set(initialValue);
+ }
+ }
+
+
/**
* Gets the current value.
*
@@ -128,7 +174,6 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
return generalOps.execute(new SessionCallback() {
@SuppressWarnings("unchecked")
-
public Boolean execute(RedisOperations operations) {
for (;;) {
operations.watch(Collections.singleton(key));
@@ -229,38 +274,38 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
return (double) get();
}
-
+
public String getKey() {
return key;
}
-
+
public Boolean expire(long timeout, TimeUnit unit) {
return generalOps.expire(key, timeout, unit);
}
-
+
public Boolean expireAt(Date date) {
return generalOps.expireAt(key, date);
}
-
+
public Long getExpire() {
return generalOps.getExpire(key);
}
-
+
public Boolean persist() {
return generalOps.persist(key);
}
-
+
public void rename(String newKey) {
generalOps.rename(key, newKey);
key = newKey;
}
-
+
public DataType getType() {
return DataType.STRING;
}