+ add append and substract operations

This commit is contained in:
Costin Leau
2010-12-08 23:23:59 +02:00
parent f18fffa4ee
commit ea20533a37
7 changed files with 70 additions and 3 deletions

View File

@@ -52,5 +52,5 @@ public interface RedisStringCommands {
Long append(byte[] key, byte[] value);
byte[] substr(byte[] key, long start, long end);
byte[] substr(byte[] key, int start, int end);
}

View File

@@ -499,7 +499,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public byte[] substr(byte[] key, long start, long end) {
public byte[] substr(byte[] key, int start, int end) {
try {
if (isQueueing()) {
transaction.substr(key, (int) start, (int) end);

View File

@@ -330,7 +330,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public byte[] substr(byte[] key, long start, long end) {
public byte[] substr(byte[] key, int start, int end) {
try {
return jredis.substr(JredisUtils.decode(key), start, end);
} catch (RedisException ex) {

View File

@@ -38,4 +38,8 @@ public interface BoundValueOperations<K, V> extends KeyBound<K> {
Long increment(long delta);
Integer append(String value);
String substract(int start, int end);
}

View File

@@ -50,6 +50,16 @@ class DefaultBoundValueOperations<K, V> extends DefaultKeyBound<K> implements Bo
return ops.increment(getKey(), delta);
}
@Override
public Integer append(String value) {
return ops.append(getKey(), value);
}
@Override
public String substract(int start, int end) {
return ops.substract(getKey(), start, end);
}
@Override
public void set(V value, long timeout, TimeUnit unit) {
ops.set(getKey(), value, timeout, unit);

View File

@@ -71,6 +71,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
private RedisSerializer valueSerializer = new SimpleRedisSerializer();
private RedisSerializer hashKeySerializer = new SimpleRedisSerializer();
private RedisSerializer hashValueSerializer = new SimpleRedisSerializer();
private RedisSerializer stringSerializer = new StringRedisSerializer();
// cache singleton objects (where possible)
private final ValueOperations<K, V> valueOps = new DefaultValueOperations();
@@ -205,6 +206,16 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
this.hashValueSerializer = hashValueSerializer;
}
/**
* Sets the string value serializer to be used by this template (when the arguments or return types
* are always strings). Defaults to {@link StringRedisSerializer}.
*
* @see ValueOperations#substract(Object, int, int)
* @param stringSerializer The stringValueSerializer to set.
*/
public void setStringSerializer(RedisSerializer<?> stringSerializer) {
this.stringSerializer = stringSerializer;
}
/**
* Invocation handler that suppresses close calls on JDO PersistenceManagers.
@@ -250,6 +261,11 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return (key != null ? keySerializer.serialize(key) : null);
}
@SuppressWarnings("unchecked")
private byte[] rawString(String key) {
return (key != null ? stringSerializer.serialize(key) : null);
}
@SuppressWarnings("unchecked")
private <T> byte[] rawValue(T value) {
return (value != null ? valueSerializer.serialize(value) : null);
@@ -339,6 +355,11 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return (V) deserialize(value, valueSerializer);
}
@SuppressWarnings("unchecked")
private String deserializeString(byte[] value) {
return (String) deserialize(value, stringSerializer);
}
@SuppressWarnings( { "unchecked", "unused" })
private <HK> HK deserializeHashKey(byte[] value) {
return (HK) deserialize(value, hashKeySerializer);
@@ -356,6 +377,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return serializer.deserialize(value);
}
private static boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
@@ -676,6 +698,33 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public Integer append(K key, String value) {
final byte[] rawKey = rawKey(key);
final byte[] rawString = rawString(value);
return execute(new RedisCallback<Integer>() {
@Override
public Integer doInRedis(RedisConnection connection) {
return connection.append(rawKey, rawString).intValue();
}
}, true);
}
@Override
public String substract(K key, final int start, final int end) {
final byte[] rawKey = rawKey(key);
byte[] rawReturn = execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) {
return connection.substr(rawKey, start, end);
}
}, true);
return deserializeString(rawReturn);
}
@Override
public Collection<V> multiGet(Collection<K> keys) {
if (keys.isEmpty()) {

View File

@@ -44,5 +44,9 @@ public interface ValueOperations<K, V> {
Long increment(K key, long delta);
Integer append(K key, String value);
String substract(K key, int start, int end);
RedisOperations<K, V> getOperations();
}