Skip to content

Commit 1deccce

Browse files
committed
Refine RedisList.
Add missing Javadoc. Add trim overload accepting long to avoid potential integer downcasting. Add factory methods to RedisList. See #2039.
1 parent 0484089 commit 1deccce

File tree

6 files changed

+101
-8
lines changed

6 files changed

+101
-8
lines changed

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java

+22
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ public DefaultRedisList(String key, RedisOperations<String, E> operations) {
6969
this(operations.boundListOps(key));
7070
}
7171

72+
/**
73+
* Constructs a new {@link DefaultRedisList} instance.
74+
*
75+
* @param key Redis key of this list.
76+
* @param operations {@link RedisOperations} for the value type of this list.
77+
* @param maxSize
78+
* @since 2.6
79+
*/
80+
public DefaultRedisList(String key, RedisOperations<String, E> operations, int maxSize) {
81+
this(operations.boundListOps(key), maxSize);
82+
}
83+
7284
/**
7385
* Constructs a new, uncapped {@link DefaultRedisList} instance.
7486
*
@@ -194,6 +206,16 @@ public RedisList<E> trim(int start, int end) {
194206
return this;
195207
}
196208

209+
/*
210+
* (non-Javadoc)
211+
* @see org.springframework.data.redis.support.collections.RedisList#trim(long, long)
212+
*/
213+
@Override
214+
public RedisList<E> trim(long start, long end) {
215+
listOps.trim(start, end);
216+
return this;
217+
}
218+
197219
/*
198220
* (non-Javadoc)
199221
* @see java.util.AbstractCollection#iterator()

src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void afterPropertiesSet() {
106106
private RedisStore createStore(DataType dt) {
107107
switch (dt) {
108108
case LIST:
109-
return new DefaultRedisList(key, template);
109+
return RedisList.create(key, template);
110110

111111
case SET:
112112
return new DefaultRedisSet(key, template);

src/main/java/org/springframework/data/redis/support/collections/RedisList.java

+73-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.concurrent.BlockingDeque;
2525
import java.util.concurrent.TimeUnit;
2626

27+
import org.springframework.data.redis.core.BoundListOperations;
28+
import org.springframework.data.redis.core.RedisOperations;
2729
import org.springframework.data.redis.core.TimeoutUtils;
2830
import org.springframework.lang.Nullable;
2931
import org.springframework.util.Assert;
@@ -37,6 +39,50 @@
3739
*/
3840
public interface RedisList<E> extends RedisCollection<E>, List<E>, BlockingDeque<E> {
3941

42+
/**
43+
* Constructs a new, uncapped {@link RedisList} instance.
44+
*
45+
* @param key Redis key of this list.
46+
* @param operations {@link RedisOperations} for the value type of this list.
47+
* @since 2.6
48+
*/
49+
static <E> RedisList<E> create(String key, RedisOperations<String, E> operations) {
50+
return new DefaultRedisList<>(key, operations);
51+
}
52+
53+
/**
54+
* Constructs a new {@link RedisList} instance.
55+
*
56+
* @param key Redis key of this list.
57+
* @param operations {@link RedisOperations} for the value type of this list.
58+
* @param maxSize
59+
* @since 2.6
60+
*/
61+
static <E> RedisList<E> create(String key, RedisOperations<String, E> operations, int maxSize) {
62+
return new DefaultRedisList<>(key, operations, maxSize);
63+
}
64+
65+
/**
66+
* Constructs a new, uncapped {@link DefaultRedisList} instance.
67+
*
68+
* @param boundOps {@link BoundListOperations} for the value type of this list.
69+
* @since 2.6
70+
*/
71+
static <E> RedisList<E> create(BoundListOperations<String, E> boundOps) {
72+
return new DefaultRedisList<>(boundOps);
73+
}
74+
75+
/**
76+
* Constructs a new {@link DefaultRedisList} instance.
77+
*
78+
* @param boundOps {@link BoundListOperations} for the value type of this list.
79+
* @param maxSize
80+
* @since 2.6
81+
*/
82+
static <E> RedisList<E> create(BoundListOperations<String, E> boundOps, int maxSize) {
83+
return new DefaultRedisList<>(boundOps, maxSize);
84+
}
85+
4086
/**
4187
* Atomically returns and removes the first element of the list stored at the bound key, and pushes the element at the
4288
* first/last element (head/tail depending on the {@link Direction destinationPosition} argument) of the list stored
@@ -155,7 +201,32 @@ default E moveLastTo(RedisList<E> destination, Direction destinationPosition, Du
155201
@Nullable
156202
E moveLastTo(RedisList<E> destination, Direction destinationPosition, long timeout, TimeUnit unit);
157203

158-
List<E> range(long begin, long end);
204+
/**
205+
* Get elements between {@code start} and {@code end} from list at the bound key.
206+
*
207+
* @param start
208+
* @param end
209+
* @return {@literal null} when used in pipeline / transaction.
210+
* @see <a href="https://redis.io/commands/lrange">Redis Documentation: LRANGE</a>
211+
*/
212+
List<E> range(long start, long end);
159213

160-
RedisList<E> trim(int begin, int end);
214+
/**
215+
* Trim list at the bound key to elements between {@code start} and {@code end}.
216+
*
217+
* @param start
218+
* @param end
219+
* @see <a href="https://redis.io/commands/ltrim">Redis Documentation: LTRIM</a>
220+
*/
221+
RedisList<E> trim(int start, int end);
222+
223+
/**
224+
* Trim list at the bound key to elements between {@code start} and {@code end}.
225+
*
226+
* @param start
227+
* @param end
228+
* @since 2.6
229+
* @see <a href="https://redis.io/commands/ltrim">Redis Documentation: LTRIM</a>
230+
*/
231+
RedisList<E> trim(long start, long end);
161232
}

src/test/java/org/springframework/data/redis/support/BoundKeyParams.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.data.redis.core.StringRedisTemplate;
2727
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
2828
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
29-
import org.springframework.data.redis.support.collections.DefaultRedisList;
3029
import org.springframework.data.redis.support.collections.DefaultRedisMap;
3130
import org.springframework.data.redis.support.collections.DefaultRedisSet;
3231
import org.springframework.data.redis.support.collections.RedisList;
@@ -47,7 +46,7 @@ public static Collection<Object[]> testParams() {
4746
StringRedisTemplate templateJS = new StringRedisTemplate(jedisConnFactory);
4847
DefaultRedisMap mapJS = new DefaultRedisMap("bound:key:map", templateJS);
4948
DefaultRedisSet setJS = new DefaultRedisSet("bound:key:set", templateJS);
50-
RedisList list = new DefaultRedisList("bound:key:list", templateJS);
49+
RedisList list = RedisList.create("bound:key:list", templateJS);
5150

5251
// Lettuce
5352
LettuceConnectionFactory lettuceConnFactory = LettuceConnectionFactoryExtension
@@ -56,7 +55,7 @@ public static Collection<Object[]> testParams() {
5655
StringRedisTemplate templateLT = new StringRedisTemplate(lettuceConnFactory);
5756
DefaultRedisMap mapLT = new DefaultRedisMap("bound:key:mapLT", templateLT);
5857
DefaultRedisSet setLT = new DefaultRedisSet("bound:key:setLT", templateLT);
59-
RedisList listLT = new DefaultRedisList("bound:key:listLT", templateLT);
58+
RedisList listLT = RedisList.create("bound:key:listLT", templateLT);
6059

6160
StringObjectFactory sof = new StringObjectFactory();
6261

src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListIntegrationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,10 @@ void testTrim() {
319319
list.add(t1);
320320
list.add(t2);
321321
assertThat(list).hasSize(2);
322-
assertThat(list.trim(0, 0)).hasSize(1);
322+
assertThat(list.trim(0L, 0L)).hasSize(1);
323323
assertThat(list).hasSize(1);
324324
assertThat(list.get(0)).isEqualTo(t1);
325+
assertThat(list).hasSize(1);
325326
}
326327

327328
@SuppressWarnings("unchecked")

src/test/java/org/springframework/data/redis/support/collections/RedisListIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public RedisListIntegrationTests(ObjectFactory<Object> factory, RedisTemplate<Ob
3636
}
3737

3838
RedisStore copyStore(RedisStore store) {
39-
return new DefaultRedisList<>(store.getKey(), store.getOperations());
39+
return RedisList.create(store.getKey(), store.getOperations());
4040
}
4141

4242
AbstractRedisCollection<Object> createCollection() {

0 commit comments

Comments
 (0)