From 0d1263f2a3bd12bd37fbd3e03031b3c991e24a00 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Fri, 12 Nov 2010 20:48:12 +0200 Subject: [PATCH] + implement most of the existing, basic, RedisOperations --- .../datastore/redis/core/RedisOperations.java | 6 +- .../datastore/redis/core/RedisTemplate.java | 104 ++++++++++++++++-- .../redis/util/RedisAtomicInteger.java | 2 +- .../datastore/redis/util/RedisAtomicLong.java | 2 +- 4 files changed, 100 insertions(+), 14 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 d9eab1c2d..30ac24154 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 @@ -27,15 +27,15 @@ public interface RedisOperations { V get(K key); - V getSet(K key, V newValue); + V getAndSet(K key, V newValue); - void watch(K key); + void watch(K... keys); void multi(); Object exec(); - V increment(K key, int delta); + Integer increment(K key, int delta); ListOperations listOps(); 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 6a2135d27..f353b5e64 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 @@ -180,6 +180,36 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } } + private byte[] rawKey(K key) { + return (key != null ? keySerializer.serialize(key) : null); + } + + private byte[] rawValue(V value) { + return (value != null ? valueSerializer.serialize(value) : null); + } + + // utility methods for the template internal methods + private abstract class DeserializingRedisCallback implements RedisCallback { + private K key; + + public DeserializingRedisCallback(K key) { + this.key = key; + } + + @SuppressWarnings("unchecked") + @Override + public final V doInRedis(RedisConnection connection) throws Exception { + byte[] result = inRedis(rawKey(key), connection); + if (result != null) { + return (V) valueSerializer.deserialize(result); + } + return null; + } + + protected abstract byte[] inRedis(byte[] rawKey, RedisConnection connection); + } + + // // RedisOperations // @@ -195,18 +225,52 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public V get(K key) { - throw new UnsupportedOperationException(); + public V get(final K key) { + return execute(new DeserializingRedisCallback(key) { + + @Override + protected byte[] inRedis(byte[] rawKey, RedisConnection connection) { + return connection.get(rawKey); + } + + }, false); } @Override - public V getSet(K key, V newValue) { - throw new UnsupportedOperationException(); + public V getAndSet(K key, V newValue) { + final byte[] rawValue = rawValue(newValue); + return execute(new DeserializingRedisCallback(key) { + + @Override + protected byte[] inRedis(byte[] rawKey, RedisConnection connection) { + return connection.getSet(rawKey, rawValue); + } + + }, false); } @Override - public V increment(K key, int delta) { - throw new UnsupportedOperationException(); + public Integer increment(K key, final int delta) { + final byte[] rawKey = rawKey(key); + return execute(new RedisCallback() { + + @Override + public Integer doInRedis(RedisConnection connection) throws Exception { + if (delta == 1) { + return connection.incr(rawKey); + } + + if (delta == -1) { + return connection.decr(rawKey); + } + + if (delta < 0) { + return connection.decrBy(rawKey, delta); + } + + return connection.incrBy(rawKey, delta); + } + }, false); } @Override @@ -221,11 +285,33 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation @Override public void set(K key, V value) { - throw new UnsupportedOperationException(); + final byte[] rawValue = rawValue(value); + execute(new DeserializingRedisCallback(key) { + + @Override + protected byte[] inRedis(byte[] rawKey, RedisConnection connection) { + connection.set(rawKey, rawValue); + return null; + } + + }, false); } @Override - public void watch(K key) { - throw new UnsupportedOperationException(); + public void watch(K... keys) { + final byte[][] rawKeys = new byte[keys.length][]; + + for (int i = 0; i < keys.length; i++) { + rawKeys[i] = rawKey(keys[i]); + } + + execute(new RedisCallback() { + + @Override + public Object doInRedis(RedisConnection connection) throws Exception { + connection.watch(rawKeys); + return null; + } + }, false); } } \ No newline at end of file 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 6e5b7f514..fc2153ebc 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 @@ -79,7 +79,7 @@ public class RedisAtomicInteger extends Number implements Serializable { * @return the previous value */ public int getAndSet(int newValue) { - return operations.getSet(key, newValue); + return operations.getAndSet(key, newValue); } /** 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 732872b7c..8b736681b 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 @@ -79,7 +79,7 @@ public class RedisAtomicLong extends Number implements Serializable { * @return the previous value */ public long getAndSet(long newValue) { - return operations.getSet(key, newValue); + return operations.getAndSet(key, newValue); } /**