diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java index 00c9bd108..3ed6d3ab9 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java @@ -69,6 +69,18 @@ public class DefaultRedisList extends AbstractRedisCollection 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 operations, int maxSize) { + this(operations.boundListOps(key), maxSize); + } + /** * Constructs a new, uncapped {@link DefaultRedisList} instance. * @@ -194,6 +206,16 @@ public class DefaultRedisList extends AbstractRedisCollection implements R return this; } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.support.collections.RedisList#trim(long, long) + */ + @Override + public RedisList trim(long start, long end) { + listOps.trim(start, end); + return this; + } + /* * (non-Javadoc) * @see java.util.AbstractCollection#iterator() diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java b/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java index bf3df4809..6e1053f47 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java @@ -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); diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisList.java b/src/main/java/org/springframework/data/redis/support/collections/RedisList.java index 29773cd35..ee83e9183 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisList.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisList.java @@ -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 extends RedisCollection, List, BlockingDeque { + /** + * 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 RedisList create(String key, RedisOperations 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 RedisList create(String key, RedisOperations 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 RedisList create(BoundListOperations 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 RedisList create(BoundListOperations 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 extends RedisCollection, List, BlockingDeque @Nullable E moveLastTo(RedisList destination, Direction destinationPosition, long timeout, TimeUnit unit); - List 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 Redis Documentation: LRANGE + */ + List range(long start, long end); - RedisList trim(int begin, int end); + /** + * Trim list at the bound key to elements between {@code start} and {@code end}. + * + * @param start + * @param end + * @see Redis Documentation: LTRIM + */ + RedisList 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 Redis Documentation: LTRIM + */ + RedisList trim(long start, long end); } diff --git a/src/test/java/org/springframework/data/redis/support/BoundKeyParams.java b/src/test/java/org/springframework/data/redis/support/BoundKeyParams.java index bb9dced6c..59cf7226e 100644 --- a/src/test/java/org/springframework/data/redis/support/BoundKeyParams.java +++ b/src/test/java/org/springframework/data/redis/support/BoundKeyParams.java @@ -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(); diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListIntegrationTests.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListIntegrationTests.java index aa2d35b12..6b3b6cc90 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListIntegrationTests.java @@ -319,9 +319,10 @@ public abstract class AbstractRedisListIntegrationTests 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") diff --git a/src/test/java/org/springframework/data/redis/support/collections/RedisListIntegrationTests.java b/src/test/java/org/springframework/data/redis/support/collections/RedisListIntegrationTests.java index 06d2baa69..251975e17 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/RedisListIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/RedisListIntegrationTests.java @@ -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 createCollection() {