From 20e1ecedaaf3c22646c73c23a18372012b8e91e7 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Mon, 10 Jan 2011 14:20:54 +0200 Subject: [PATCH] + add support for [x]pushX on list operations + add support for blocking rightPopLeftPush --- .../redis/core/BoundListOperations.java | 4 ++ .../core/DefaultBoundListOperations.java | 9 +++++ .../keyvalue/redis/core/ListOperations.java | 6 +++ .../keyvalue/redis/core/RedisTemplate.java | 37 +++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundListOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundListOperations.java index 46d3c9b1c..d3d8ba350 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundListOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundListOperations.java @@ -35,10 +35,14 @@ public interface BoundListOperations extends KeyBound { 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(); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundListOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundListOperations.java index 040c62f7a..3bbaa4066 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundListOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundListOperations.java @@ -65,6 +65,11 @@ class DefaultBoundListOperations extends DefaultKeyBound 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 extends DefaultKeyBound 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) { diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ListOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ListOperations.java index 8af535037..6029ac2e3 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ListOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ListOperations.java @@ -33,10 +33,14 @@ public interface ListOperations { 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 { V rightPopAndLeftPush(K sourceKey, K destinationKey); + V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit); + RedisOperations getOperations(); } diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java index d0917d53b..44f8f1311 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java @@ -940,6 +940,18 @@ public class RedisTemplate 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() { + @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 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() { + @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 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);