From 4c40fa0f872ad3f041e49a0cb5c83222f76b5db9 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Fri, 12 Nov 2010 16:34:54 +0200 Subject: [PATCH] + update RedisAtomicLong to use generics + updated RedisOperation/Template in the process --- .../datastore/redis/core/RedisOperations.java | 19 +++++- .../datastore/redis/core/RedisTemplate.java | 25 ++++++-- .../datastore/redis/util/RedisAtomicLong.java | 61 ++++++++++--------- 3 files changed, 69 insertions(+), 36 deletions(-) diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/RedisOperations.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/RedisOperations.java index 7d6b8ea0e..83e0f1f86 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/RedisOperations.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/RedisOperations.java @@ -20,7 +20,24 @@ package org.springframework.datastore.redis.core; * * @author Costin Leau */ -public interface RedisOperations { +public interface RedisOperations { + void set(K key, V value); + + V get(K key); + + V getSet(K key, V newValue); + + void watch(K key); + + void multi(); + + Object exec(); + + V incr(K key); + + V decr(K key); + + V incrBy(K key, int delta); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/RedisTemplate.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/RedisTemplate.java index 89673c6f0..6a12b8dad 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/RedisTemplate.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/RedisTemplate.java @@ -24,6 +24,7 @@ import org.springframework.datastore.redis.connection.RedisConnection; import org.springframework.datastore.redis.connection.RedisConnectionFactory; import org.springframework.datastore.redis.serializer.RedisSerializer; import org.springframework.datastore.redis.serializer.SimpleRedisSerializer; +import org.springframework.datastore.redis.serializer.StringRedisSerializer; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -43,10 +44,12 @@ import org.springframework.util.ClassUtils; * * @author Costin Leau */ -public class RedisTemplate extends RedisAccessor { +public class RedisTemplate extends RedisAccessor { private boolean exposeConnection = false; - private RedisSerializer converter = new SimpleRedisSerializer(); + private RedisSerializer keySerializer = new StringRedisSerializer(); + private RedisSerializer valueSerializer = new SimpleRedisSerializer(); + private RedisSerializer defaultSerializer = new SimpleRedisSerializer(); public RedisTemplate() { } @@ -60,7 +63,7 @@ public class RedisTemplate extends RedisAccessor { execute(new RedisCallback() { @Override public Object doInRedis(RedisConnection connection) throws Exception { - connection.del(redisKey); + connection.del(keySerializer.serialize(redisKey)); return null; } }); @@ -72,6 +75,10 @@ public class RedisTemplate extends RedisAccessor { } public T execute(RedisCallback action, boolean exposeConnection) { + return execute(action, isExposeConnection(), defaultSerializer); + } + + public T execute(RedisCallback action, boolean exposeConnection, RedisSerializer returnSerializer) { Assert.notNull(action, "Callback object must not be null"); RedisConnectionFactory factory = getConnectionFactory(); @@ -122,8 +129,16 @@ public class RedisTemplate extends RedisAccessor { this.exposeConnection = exposeConnection; } - public void setRedisConverter(RedisSerializer converter) { - this.converter = converter; + public void setKeySerializer(RedisSerializer serializer) { + this.keySerializer = serializer; + } + + public void setValueSerializer(RedisSerializer serializer) { + this.valueSerializer = serializer; + } + + public void setDefaultSerializer(RedisSerializer serializer) { + this.defaultSerializer = serializer; } /** diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisAtomicLong.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisAtomicLong.java index 64775e673..b1587e4de 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisAtomicLong.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisAtomicLong.java @@ -17,11 +17,11 @@ package org.springframework.datastore.redis.util; import java.io.Serializable; -import org.springframework.datastore.redis.connection.RedisCommands; +import org.springframework.datastore.redis.core.RedisOperations; /** * Atomic long backed by Redis. - * Uses Redis atomic increment/decrement and watch/multi/exec commands for CAS operations. + * Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS operations. * * @see java.util.concurrent.atomic.AtomicLong * @author Costin Leau @@ -29,29 +29,29 @@ import org.springframework.datastore.redis.connection.RedisCommands; public class RedisAtomicLong extends Number implements Serializable { private final String key; - private RedisCommands commands; + private RedisOperations operations; /** * Constructs a new RedisAtomicLong instance with an initial value of zero. * * @param redisCounter - * @param commands + * @param operations */ - public RedisAtomicLong(String redisCounter, RedisCommands commands) { - this(redisCounter, commands, 0); + public RedisAtomicLong(String redisCounter, RedisOperations operations) { + this(redisCounter, operations, 0); } /** * Constructs a new RedisAtomicLong instance with the given initial value. * * @param redisCounter - * @param commands + * @param operations * @param initialValue */ - public RedisAtomicLong(String redisCounter, RedisCommands commands, long initialValue) { + public RedisAtomicLong(String redisCounter, RedisOperations operations, long initialValue) { this.key = redisCounter; - this.commands = commands; - commands.set(redisCounter, Long.toString(initialValue)); + this.operations = operations; + operations.set(redisCounter, initialValue); } /** @@ -60,7 +60,7 @@ public class RedisAtomicLong extends Number implements Serializable { * @return the current value */ public long get() { - return Long.valueOf(commands.get(key)); + return operations.get(key); } /** @@ -69,7 +69,7 @@ public class RedisAtomicLong extends Number implements Serializable { * @param newValue the new value */ public void set(long newValue) { - commands.set(key, Long.toString(newValue)); + operations.set(key, newValue); } /** @@ -79,7 +79,7 @@ public class RedisAtomicLong extends Number implements Serializable { * @return the previous value */ public long getAndSet(long newValue) { - return Long.valueOf(commands.getSet(key, Long.toString(newValue))); + return operations.getSet(key, newValue); } /** @@ -93,11 +93,11 @@ public class RedisAtomicLong extends Number implements Serializable { */ public boolean compareAndSet(long expect, long update) { for (;;) { - commands.watch(key); + operations.watch(key); if (expect == get()) { - commands.multi(); + operations.multi(); set(update); - if (commands.exec() != null) { + if (operations.exec() != null) { return true; } } @@ -112,11 +112,11 @@ public class RedisAtomicLong extends Number implements Serializable { */ public long getAndIncrement() { for (;;) { - commands.watch(key); + operations.watch(key); long value = get(); - commands.multi(); - commands.incr(key); - if (commands.exec() != null) { + operations.multi(); + operations.incr(key); + if (operations.exec() != null) { return value; } } @@ -129,11 +129,11 @@ public class RedisAtomicLong extends Number implements Serializable { */ public long getAndDecrement() { for (;;) { - commands.watch(key); + operations.watch(key); long value = get(); - commands.multi(); - commands.decr(key); - if (commands.exec() != null) { + operations.multi(); + operations.decr(key); + if (operations.exec() != null) { return value; } } @@ -147,11 +147,11 @@ public class RedisAtomicLong extends Number implements Serializable { */ public long getAndAdd(long delta) { for (;;) { - commands.watch(key); + operations.watch(key); long value = get(); - commands.multi(); + operations.multi(); set(value + delta); - if (commands.exec() != null) { + if (operations.exec() != null) { return value; } } @@ -163,7 +163,7 @@ public class RedisAtomicLong extends Number implements Serializable { * @return the updated value */ public long incrementAndGet() { - return commands.incr(key); + return operations.incr(key); } /** @@ -172,7 +172,7 @@ public class RedisAtomicLong extends Number implements Serializable { * @return the updated value */ public long decrementAndGet() { - return commands.decr(key); + return operations.decr(key); } /** @@ -183,11 +183,12 @@ public class RedisAtomicLong extends Number implements Serializable { */ public long addAndGet(long delta) { // TODO: is this really safe - return commands.incrBy(key, (int) delta); + return operations.incrBy(key, (int) delta); } /** * Returns the String representation of the current value. + * * @return the String representation of the current value. */ public String toString() {