Refine RedisList.

Add missing Javadoc. Add trim overload accepting long to avoid potential integer downcasting. Add factory methods to RedisList.

See: #2039
Original Pull Request: #2107
This commit is contained in:
Mark Paluch
2021-06-30 11:30:33 +02:00
committed by Christoph Strobl
parent 54ad66b62c
commit eefbd0d821
6 changed files with 101 additions and 8 deletions

View File

@@ -69,6 +69,18 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
this(operations.boundListOps(key));
}
/**
* Constructs a new {@link DefaultRedisList} instance.
*
* @param key Redis key of this list.
* @param operations {@link RedisOperations} for the value type of this list.
* @param maxSize
* @since 2.6
*/
public DefaultRedisList(String key, RedisOperations<String, E> operations, int maxSize) {
this(operations.boundListOps(key), maxSize);
}
/**
* Constructs a new, uncapped {@link DefaultRedisList} instance.
*
@@ -194,6 +206,16 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisList#trim(long, long)
*/
@Override
public RedisList<E> trim(long start, long end) {
listOps.trim(start, end);
return this;
}
/*
* (non-Javadoc)
* @see java.util.AbstractCollection#iterator()

View File

@@ -106,7 +106,7 @@ public class RedisCollectionFactoryBean implements InitializingBean, BeanNameAwa
private RedisStore createStore(DataType dt) {
switch (dt) {
case LIST:
return new DefaultRedisList(key, template);
return RedisList.create(key, template);
case SET:
return new DefaultRedisSet(key, template);

View File

@@ -24,6 +24,8 @@ import java.util.Queue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -37,6 +39,50 @@ import org.springframework.util.Assert;
*/
public interface RedisList<E> extends RedisCollection<E>, List<E>, BlockingDeque<E> {
/**
* Constructs a new, uncapped {@link RedisList} instance.
*
* @param key Redis key of this list.
* @param operations {@link RedisOperations} for the value type of this list.
* @since 2.6
*/
static <E> RedisList<E> create(String key, RedisOperations<String, E> operations) {
return new DefaultRedisList<>(key, operations);
}
/**
* Constructs a new {@link RedisList} instance.
*
* @param key Redis key of this list.
* @param operations {@link RedisOperations} for the value type of this list.
* @param maxSize
* @since 2.6
*/
static <E> RedisList<E> create(String key, RedisOperations<String, E> operations, int maxSize) {
return new DefaultRedisList<>(key, operations, maxSize);
}
/**
* Constructs a new, uncapped {@link DefaultRedisList} instance.
*
* @param boundOps {@link BoundListOperations} for the value type of this list.
* @since 2.6
*/
static <E> RedisList<E> create(BoundListOperations<String, E> boundOps) {
return new DefaultRedisList<>(boundOps);
}
/**
* Constructs a new {@link DefaultRedisList} instance.
*
* @param boundOps {@link BoundListOperations} for the value type of this list.
* @param maxSize
* @since 2.6
*/
static <E> RedisList<E> create(BoundListOperations<String, E> boundOps, int maxSize) {
return new DefaultRedisList<>(boundOps, maxSize);
}
/**
* Atomically returns and removes the first element of the list stored at the bound key, and pushes the element at the
* first/last element (head/tail depending on the {@link Direction destinationPosition} argument) of the list stored
@@ -155,7 +201,32 @@ public interface RedisList<E> extends RedisCollection<E>, List<E>, BlockingDeque
@Nullable
E moveLastTo(RedisList<E> destination, Direction destinationPosition, long timeout, TimeUnit unit);
List<E> range(long begin, long end);
/**
* Get elements between {@code start} and {@code end} from list at the bound key.
*
* @param start
* @param end
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="https://redis.io/commands/lrange">Redis Documentation: LRANGE</a>
*/
List<E> range(long start, long end);
RedisList<E> trim(int begin, int end);
/**
* Trim list at the bound key to elements between {@code start} and {@code end}.
*
* @param start
* @param end
* @see <a href="https://redis.io/commands/ltrim">Redis Documentation: LTRIM</a>
*/
RedisList<E> trim(int start, int end);
/**
* Trim list at the bound key to elements between {@code start} and {@code end}.
*
* @param start
* @param end
* @since 2.6
* @see <a href="https://redis.io/commands/ltrim">Redis Documentation: LTRIM</a>
*/
RedisList<E> trim(long start, long end);
}

View File

@@ -26,7 +26,6 @@ import org.springframework.data.redis.connection.lettuce.extension.LettuceConnec
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.data.redis.support.collections.DefaultRedisMap;
import org.springframework.data.redis.support.collections.DefaultRedisSet;
import org.springframework.data.redis.support.collections.RedisList;
@@ -47,7 +46,7 @@ public class BoundKeyParams {
StringRedisTemplate templateJS = new StringRedisTemplate(jedisConnFactory);
DefaultRedisMap mapJS = new DefaultRedisMap("bound:key:map", templateJS);
DefaultRedisSet setJS = new DefaultRedisSet("bound:key:set", templateJS);
RedisList list = new DefaultRedisList("bound:key:list", templateJS);
RedisList list = RedisList.create("bound:key:list", templateJS);
// Lettuce
LettuceConnectionFactory lettuceConnFactory = LettuceConnectionFactoryExtension
@@ -56,7 +55,7 @@ public class BoundKeyParams {
StringRedisTemplate templateLT = new StringRedisTemplate(lettuceConnFactory);
DefaultRedisMap mapLT = new DefaultRedisMap("bound:key:mapLT", templateLT);
DefaultRedisSet setLT = new DefaultRedisSet("bound:key:setLT", templateLT);
RedisList listLT = new DefaultRedisList("bound:key:listLT", templateLT);
RedisList listLT = RedisList.create("bound:key:listLT", templateLT);
StringObjectFactory sof = new StringObjectFactory();

View File

@@ -319,9 +319,10 @@ public abstract class AbstractRedisListIntegrationTests<T> extends AbstractRedis
list.add(t1);
list.add(t2);
assertThat(list).hasSize(2);
assertThat(list.trim(0, 0)).hasSize(1);
assertThat(list.trim(0L, 0L)).hasSize(1);
assertThat(list).hasSize(1);
assertThat(list.get(0)).isEqualTo(t1);
assertThat(list).hasSize(1);
}
@SuppressWarnings("unchecked")

View File

@@ -36,7 +36,7 @@ public class RedisListIntegrationTests extends AbstractRedisListIntegrationTests
}
RedisStore copyStore(RedisStore store) {
return new DefaultRedisList<>(store.getKey(), store.getOperations());
return RedisList.create(store.getKey(), store.getOperations());
}
AbstractRedisCollection<Object> createCollection() {