+ add support for [x]pushX on list operations

+ add support for blocking rightPopLeftPush
This commit is contained in:
Costin Leau
2011-01-10 14:20:54 +02:00
parent e19def6de2
commit 20e1ecedaa
4 changed files with 56 additions and 0 deletions

View File

@@ -35,10 +35,14 @@ public interface BoundListOperations<K, V> extends KeyBound<K> {
Long leftPush(V value);
Long leftPushIfPresent(V value);
Long leftPush(V pivot, V value);
Long rightPush(V value);
Long rightPushIfPresent(V value);
Long rightPush(V pivot, V value);
V leftPop();

View File

@@ -65,6 +65,11 @@ class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements Bou
return ops.leftPush(getKey(), value);
}
@Override
public Long leftPushIfPresent(V value) {
return ops.leftPushIfPresent(getKey(), value);
}
@Override
public Long leftPush(V pivot, V value) {
return ops.leftPush(getKey(), pivot, value);
@@ -95,6 +100,10 @@ class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements Bou
return ops.rightPop(getKey(), timeout, unit);
}
@Override
public Long rightPushIfPresent(V value) {
return ops.rightPushIfPresent(getKey(), value);
}
@Override
public Long rightPush(V value) {

View File

@@ -33,10 +33,14 @@ public interface ListOperations<K, V> {
Long leftPush(K key, V value);
Long leftPushIfPresent(K key, V value);
Long leftPush(K key, V pivot, V value);
Long rightPush(K key, V value);
Long rightPushIfPresent(K key, V value);
Long rightPush(K key, V pivot, V value);
void set(K key, long index, V value);
@@ -55,5 +59,7 @@ public interface ListOperations<K, V> {
V rightPopAndLeftPush(K sourceKey, K destinationKey);
V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit);
RedisOperations<K, V> getOperations();
}

View File

@@ -940,6 +940,18 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public Long leftPushIfPresent(K key, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) {
return connection.lPushX(rawKey, rawValue);
}
}, true);
}
@Override
public Long leftPush(K key, V pivot, V value) {
final byte[] rawKey = rawKey(key);
@@ -1021,6 +1033,18 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public Long rightPushIfPresent(K key, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) {
return connection.rPushX(rawKey, rawValue);
}
}, true);
}
@Override
public Long rightPush(K key, V pivot, V value) {
final byte[] rawKey = rawKey(key);
@@ -1047,6 +1071,19 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) {
final int tm = (int) unit.toSeconds(timeout);
final byte[] rawDestKey = rawKey(destinationKey);
return execute(new ValueDeserializingRedisCallback(sourceKey) {
@Override
protected byte[] inRedis(byte[] rawSourceKey, RedisConnection connection) {
return connection.bRPopLPush(tm, rawSourceKey, rawDestKey);
}
}, true);
}
@Override
public void set(K key, final long index, V value) {
final byte[] rawValue = rawValue(value);