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 16830923e..e178935b9 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 @@ -48,16 +48,7 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound * @param factory connection factory */ public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory) { - RedisTemplate redisTemplate = new RedisTemplate(factory); - redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setValueSerializer(new GenericToStringSerializer(Integer.class)); - redisTemplate.setExposeConnection(true); - this.key = redisCounter; - this.generalOps = redisTemplate; - this.operations = generalOps.opsForValue(); - if (this.operations.get(redisCounter) == null) { - set(0); - } + this(redisCounter, factory, null); } /** @@ -68,12 +59,27 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound * @param initialValue */ public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, int initialValue) { - RedisTemplate redisTemplate = new RedisTemplate(factory); + this(redisCounter, factory, Integer.valueOf(initialValue)); + } + + private RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, Integer initialValue) { + RedisTemplate redisTemplate = new RedisTemplate(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new GenericToStringSerializer(Integer.class)); redisTemplate.setExposeConnection(true); + redisTemplate.setConnectionFactory(factory); + redisTemplate.afterPropertiesSet(); + this.key = redisCounter; this.generalOps = redisTemplate; this.operations = generalOps.opsForValue(); - this.operations.set(redisCounter, initialValue); + + if (initialValue == null && this.operations.get(redisCounter) == null) { + set(0); + } + else { + set(initialValue); + } } /** @@ -82,6 +88,8 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound * * Use {@link #RedisAtomicInteger(String, RedisOperations, int)} to set the counter to a certain value * as an alternative constructor or {@link #set(int)}. + * + * Note that integers need to be properly serialized so that Redis can recognized the values as numeric and thus modify their value. * * @param redisCounter * @param operations @@ -98,6 +106,8 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound /** * Constructs a new RedisAtomicInteger instance with the given initial value. * + * Note that integers need to be properly serialized so that Redis can recognized the values as numeric and thus modify their value. + * * @param redisCounter * @param operations * @param initialValue 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 10275074a..6d87a106a 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 @@ -48,16 +48,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound redisTemplate = new RedisTemplate(factory); - redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setValueSerializer(new GenericToStringSerializer(Long.class)); - redisTemplate.setExposeConnection(true); - this.key = redisCounter; - this.generalOps = redisTemplate; - this.operations = generalOps.opsForValue(); - if (this.operations.get(redisCounter) == null) { - set(0); - } + this(redisCounter, factory, null); } /** @@ -68,21 +59,37 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound redisTemplate = new RedisTemplate(factory); + this(redisCounter, factory, Long.valueOf(initialValue)); + } + + private RedisAtomicLong(String redisCounter, RedisConnectionFactory factory, Long initialValue) { + RedisTemplate redisTemplate = new RedisTemplate(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new GenericToStringSerializer(Long.class)); redisTemplate.setExposeConnection(true); + redisTemplate.setConnectionFactory(factory); + redisTemplate.afterPropertiesSet(); + this.key = redisCounter; this.generalOps = redisTemplate; this.operations = generalOps.opsForValue(); - this.operations.set(redisCounter, initialValue); - } + if (initialValue == null && this.operations.get(redisCounter) == null) { + set(0); + } + else { + set(initialValue); + } + } /** * Constructs a new RedisAtomicLong instance. Uses as initial value * the data from the backing store (sets the counter to 0 if no value is found). * * Use {@link #RedisAtomicLong(String, RedisOperations, long)} to set the counter to a certain value - * as an alternative constructor or {@link #set(long)}. + * as an alternative constructor or {@link #set(long)}. + * + * Note that longs need to be properly serialized so that Redis can recognized the values as numeric and thus modify their value. * * @param redisCounter * @param operations @@ -99,6 +106,8 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBoundRedisAtomicLong instance with the given initial value. * + * Note that longs need to be properly serialized so that Redis can recognized the values as numeric and thus modify their value. + * * @param redisCounter * @param operations * @param initialValue