+ update RedisAtomicLong to use generics

+ updated RedisOperation/Template in the process
This commit is contained in:
Costin Leau
2010-11-12 16:34:54 +02:00
parent e8a2cd8b40
commit 4c40fa0f87
3 changed files with 69 additions and 36 deletions

View File

@@ -20,7 +20,24 @@ package org.springframework.datastore.redis.core;
*
* @author Costin Leau
*/
public interface RedisOperations {
public interface RedisOperations<K, V> {
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);
}

View File

@@ -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<K, V> 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<Object>() {
@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> T execute(RedisCallback<T> action, boolean exposeConnection) {
return execute(action, isExposeConnection(), defaultSerializer);
}
public <T> T execute(RedisCallback<T> 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;
}
/**

View File

@@ -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<String, Long> operations;
/**
* Constructs a new <code>RedisAtomicLong</code> 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<String, Long> operations) {
this(redisCounter, operations, 0);
}
/**
* Constructs a new <code>RedisAtomicLong</code> 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<String, Long> 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() {