Skip to content

Commit 3b146cc

Browse files
denglimingmp911de
authored andcommitted
Add support for LPOP and RPOP with count option.
Closes #1987
1 parent 94bfd81 commit 3b146cc

15 files changed

+391
-10
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

+36
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,15 @@ public byte[] lPop(byte[] key) {
697697
return convertAndReturn(delegate.lPop(key), Converters.identityConverter());
698698
}
699699

700+
/*
701+
* (non-Javadoc)
702+
* @see org.springframework.data.redis.connection.RedisListCommands#lPop(byte[], long)
703+
*/
704+
@Override
705+
public List<byte[]> lPop(byte[] key, long count) {
706+
return convertAndReturn(delegate.lPop(key, count), Converters.identityConverter());
707+
}
708+
700709
/*
701710
* (non-Javadoc)
702711
* @see org.springframework.data.redis.connection.RedisListCommands#lPos(byte[], byte[], java.lang.Integer, java.lang.Integer)
@@ -896,6 +905,15 @@ public byte[] rPop(byte[] key) {
896905
return convertAndReturn(delegate.rPop(key), Converters.identityConverter());
897906
}
898907

908+
/*
909+
* (non-Javadoc)
910+
* @see org.springframework.data.redis.connection.RedisListCommands#rPop(byte[], long)
911+
*/
912+
@Override
913+
public List<byte[]> rPop(byte[] key, long count) {
914+
return convertAndReturn(delegate.rPop(key, count), Converters.identityConverter());
915+
}
916+
899917
/*
900918
* (non-Javadoc)
901919
* @see org.springframework.data.redis.connection.RedisListCommands#rPopLPush(byte[], byte[])
@@ -2169,6 +2187,15 @@ public String lPop(String key) {
21692187
return convertAndReturn(delegate.lPop(serialize(key)), bytesToString);
21702188
}
21712189

2190+
/*
2191+
* (non-Javadoc)
2192+
* @see org.springframework.data.redis.connection.StringRedisConnection#lPop(java.lang.String, long)
2193+
*/
2194+
@Override
2195+
public List<String> lPop(String key, long count) {
2196+
return convertAndReturn(delegate.lPop(serialize(key), count), byteListToStringList);
2197+
}
2198+
21722199
/*
21732200
* (non-Javadoc)
21742201
* @see org.springframework.data.redis.connection.StringRedisConnection#lPos(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)
@@ -2322,6 +2349,15 @@ public String rPop(String key) {
23222349
return convertAndReturn(delegate.rPop(serialize(key)), bytesToString);
23232350
}
23242351

2352+
/*
2353+
* (non-Javadoc)
2354+
* @see org.springframework.data.redis.connection.StringRedisConnection#rPop(java.lang.String, long)
2355+
*/
2356+
@Override
2357+
public List<String> rPop(String key, long count) {
2358+
return convertAndReturn(delegate.rPop(serialize(key), count), byteListToStringList);
2359+
}
2360+
23252361
/*
23262362
* (non-Javadoc)
23272363
* @see org.springframework.data.redis.connection.StringRedisConnection#rPopLPush(java.lang.String, java.lang.String)

src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java

+15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* @author Mark Paluch
5757
* @author Tugdual Grall
5858
* @author Andrey Shlykov
59+
* @author dengliming
5960
* @since 2.0
6061
*/
6162
public interface DefaultedRedisConnection extends RedisConnection {
@@ -712,13 +713,27 @@ default byte[] lPop(byte[] key) {
712713
return listCommands().lPop(key);
713714
}
714715

716+
/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
717+
@Override
718+
@Deprecated
719+
default List<byte[]> lPop(byte[] key, long count) {
720+
return listCommands().lPop(key, count);
721+
}
722+
715723
/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
716724
@Override
717725
@Deprecated
718726
default byte[] rPop(byte[] key) {
719727
return listCommands().rPop(key);
720728
}
721729

730+
/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
731+
@Override
732+
@Deprecated
733+
default List<byte[]> rPop(byte[] key, long count) {
734+
return listCommands().rPop(key, count);
735+
}
736+
722737
/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
723738
@Override
724739
@Deprecated

src/main/java/org/springframework/data/redis/connection/ReactiveListCommands.java

+62-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*
4343
* @author Christoph Strobl
4444
* @author Mark Paluch
45+
* @author dengliming
4546
* @since 2.0
4647
*/
4748
public interface ReactiveListCommands {
@@ -874,12 +875,14 @@ default Mono<Long> lRem(ByteBuffer key, Long count, ByteBuffer value) {
874875
*/
875876
class PopCommand extends KeyCommand {
876877

878+
private final long count;
879+
877880
private final Direction direction;
878881

879-
private PopCommand(@Nullable ByteBuffer key, Direction direction) {
882+
private PopCommand(@Nullable ByteBuffer key, long count, Direction direction) {
880883

881884
super(key);
882-
885+
this.count = count;
883886
this.direction = direction;
884887
}
885888

@@ -889,7 +892,7 @@ private PopCommand(@Nullable ByteBuffer key, Direction direction) {
889892
* @return a new {@link PopCommand} for right push ({@literal RPOP}).
890893
*/
891894
public static PopCommand right() {
892-
return new PopCommand(null, Direction.RIGHT);
895+
return new PopCommand(null, 0, Direction.RIGHT);
893896
}
894897

895898
/**
@@ -898,7 +901,7 @@ public static PopCommand right() {
898901
* @return a new {@link PopCommand} for right push ({@literal LPOP}).
899902
*/
900903
public static PopCommand left() {
901-
return new PopCommand(null, Direction.LEFT);
904+
return new PopCommand(null, 0, Direction.LEFT);
902905
}
903906

904907
/**
@@ -911,7 +914,17 @@ public PopCommand from(ByteBuffer key) {
911914

912915
Assert.notNull(key, "Key must not be null!");
913916

914-
return new PopCommand(key, direction);
917+
return new PopCommand(key, count, direction);
918+
}
919+
920+
/**
921+
* Applies the {@literal key}. Constructs a new command instance with all previously configured properties.
922+
*
923+
* @param count
924+
* @return a new {@link LSetCommand} with {@literal value} applied.
925+
*/
926+
public PopCommand count(long count) {
927+
return new PopCommand(getKey(), count, direction);
915928
}
916929

917930
/**
@@ -920,6 +933,10 @@ public PopCommand from(ByteBuffer key) {
920933
public Direction getDirection() {
921934
return direction;
922935
}
936+
937+
public long getCount() {
938+
return count;
939+
}
923940
}
924941

925942
/**
@@ -936,6 +953,21 @@ default Mono<ByteBuffer> lPop(ByteBuffer key) {
936953
return pop(Mono.just(PopCommand.left().from(key))).next().map(ByteBufferResponse::getOutput);
937954
}
938955

956+
/**
957+
* Removes and returns first element in list stored at {@literal key}.
958+
*
959+
* @param key must not be {@literal null}.
960+
* @param count
961+
* @return
962+
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
963+
*/
964+
default Flux<ByteBuffer> lPop(ByteBuffer key, long count) {
965+
966+
Assert.notNull(key, "Key must not be null!");
967+
968+
return popList(Mono.just(PopCommand.left().from(key).count(count))).flatMap(CommandResponse::getOutput);
969+
}
970+
939971
/**
940972
* Removes and returns last element in list stored at {@literal key}.
941973
*
@@ -950,6 +982,21 @@ default Mono<ByteBuffer> rPop(ByteBuffer key) {
950982
return pop(Mono.just(PopCommand.right().from(key))).next().map(ByteBufferResponse::getOutput);
951983
}
952984

985+
/**
986+
* Removes and returns last element in list stored at {@literal key}.
987+
*
988+
* @param key must not be {@literal null}.
989+
* @param count
990+
* @return
991+
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
992+
*/
993+
default Flux<ByteBuffer> rPop(ByteBuffer key, long count) {
994+
995+
Assert.notNull(key, "Key must not be null!");
996+
997+
return popList(Mono.just(PopCommand.right().from(key).count(count))).flatMap(CommandResponse::getOutput);
998+
}
999+
9531000
/**
9541001
* Removes and returns last element in list stored at {@link KeyCommand#getKey()}
9551002
*
@@ -960,6 +1007,16 @@ default Mono<ByteBuffer> rPop(ByteBuffer key) {
9601007
*/
9611008
Flux<ByteBufferResponse<PopCommand>> pop(Publisher<PopCommand> commands);
9621009

1010+
/**
1011+
* Removes and returns last element in list stored at {@link KeyCommand#getKey()}
1012+
*
1013+
* @param commands must not be {@literal null}.
1014+
* @return
1015+
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
1016+
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
1017+
*/
1018+
Flux<CommandResponse<PopCommand, Flux<ByteBuffer>>> popList(Publisher<PopCommand> commands);
1019+
9631020
/**
9641021
* @author Christoph Strobl
9651022
* @see <a href="https://redis.io/commands/blpop">Redis Documentation: BLPOP</a>

src/main/java/org/springframework/data/redis/connection/RedisListCommands.java

+23
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @author Costin Leau
2727
* @author Christoph Strobl
2828
* @author Mark Paluch
29+
* @author dengliming
2930
*/
3031
public interface RedisListCommands {
3132

@@ -200,6 +201,17 @@ default Long lPos(byte[] key, byte[] element) {
200201
@Nullable
201202
byte[] lPop(byte[] key);
202203

204+
/**
205+
* Removes and returns first element in list stored at {@code key}.
206+
*
207+
* @param key must not be {@literal null}.
208+
* @param count
209+
* @return {@literal null} when key does not exist or used in pipeline / transaction.
210+
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
211+
*/
212+
@Nullable
213+
List<byte[]> lPop(byte[] key, long count);
214+
203215
/**
204216
* Removes and returns last element in list stored at {@code key}.
205217
*
@@ -210,6 +222,17 @@ default Long lPos(byte[] key, byte[] element) {
210222
@Nullable
211223
byte[] rPop(byte[] key);
212224

225+
/**
226+
* Removes and returns last element in list stored at {@code key}.
227+
*
228+
* @param key must not be {@literal null}.
229+
* @param count
230+
* @return {@literal null} when key does not exist or used in pipeline / transaction.
231+
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
232+
*/
233+
@Nullable
234+
List<byte[]> rPop(byte[] key, long count);
235+
213236
/**
214237
* Removes and returns first element from lists stored at {@code keys}. <br>
215238
* <b>Blocks connection</b> until element available or {@code timeout} reached.

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

+22
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,17 @@ default Long lPos(String key, String element) {
842842
*/
843843
String lPop(String key);
844844

845+
/**
846+
* Removes and returns first element in list stored at {@code key}.
847+
*
848+
* @param key must not be {@literal null}.
849+
* @param count
850+
* @return
851+
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
852+
* @see RedisListCommands#lPop(byte[], long)
853+
*/
854+
List<String> lPop(String key, long count);
855+
845856
/**
846857
* Removes and returns last element in list stored at {@code key}.
847858
*
@@ -852,6 +863,17 @@ default Long lPos(String key, String element) {
852863
*/
853864
String rPop(String key);
854865

866+
/**
867+
* Removes and returns last element in list stored at {@code key}.
868+
*
869+
* @param key must not be {@literal null}.
870+
* @param count
871+
* @return
872+
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
873+
* @see RedisListCommands#rPop(byte[], long)
874+
*/
875+
List<String> rPop(String key, long count);
876+
855877
/**
856878
* Removes and returns first element from lists stored at {@code keys} (see: {@link #lPop(byte[])}). <br>
857879
* <b>Blocks connection</b> until element available or {@code timeout} reached.

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterListCommands.java

+33
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @author Christoph Strobl
3434
* @author Mark Paluch
3535
* @author Jot Zhao
36+
* @author dengliming
3637
* @since 2.0
3738
*/
3839
class JedisClusterListCommands implements RedisListCommands {
@@ -269,6 +270,22 @@ public byte[] lPop(byte[] key) {
269270
}
270271
}
271272

273+
/*
274+
* (non-Javadoc)
275+
* @see org.springframework.data.redis.connection.RedisListCommands#lPop(byte[], long)
276+
*/
277+
@Override
278+
public List<byte[]> lPop(byte[] key, long count) {
279+
280+
Assert.notNull(key, "Key must not be null!");
281+
282+
try {
283+
return connection.getCluster().lpop(key, (int) count);
284+
} catch (Exception ex) {
285+
throw convertJedisAccessException(ex);
286+
}
287+
}
288+
272289
/*
273290
* (non-Javadoc)
274291
* @see org.springframework.data.redis.connection.RedisListCommands#rPop(byte[])
@@ -285,6 +302,22 @@ public byte[] rPop(byte[] key) {
285302
}
286303
}
287304

305+
/*
306+
* (non-Javadoc)
307+
* @see org.springframework.data.redis.connection.RedisListCommands#rPop(byte[], long)
308+
*/
309+
@Override
310+
public List<byte[]> rPop(byte[] key, long count) {
311+
312+
Assert.notNull(key, "Key must not be null!");
313+
314+
try {
315+
return connection.getCluster().rpop(key, (int) count);
316+
} catch (Exception ex) {
317+
throw convertJedisAccessException(ex);
318+
}
319+
}
320+
288321
/*
289322
* (non-Javadoc)
290323
* @see org.springframework.data.redis.connection.RedisListCommands#bLPop(int, byte[][])

0 commit comments

Comments
 (0)