Skip to content

Commit a523b82

Browse files
mp911dechristophstrobl
authored andcommitted
Add support for ZRANDMEMBER.
Closes: #2049 Original Pull Request: #2104
1 parent 0b8fbae commit a523b82

23 files changed

+1182
-11
lines changed

src/main/asciidoc/new-features.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This section briefly covers items that are new and noteworthy in the latest rele
77
== New in Spring Data Redis 2.6
88

99
* Support for `SubscriptionListener` when using `MessageListener` for subscription confirmation callbacks. `ReactiveRedisMessageListenerContainer` and `ReactiveRedisOperations` provide `receiveLater(…)` and `listenToLater(…)` methods to await until Redis acknowledges the subscription.
10-
* Support Redis 6.2 commands (`LPOP`/`RPOP` with `count`, `COPY`, `GETEX`, `GETDEL`, `ZPOPMIN`, `BZPOPMIN`, `ZPOPMAX`, `BZPOPMAX`, `ZMSCORE`, `ZDIFF`, `ZDIFFSTORE`, `ZINTER`, `ZUNION`, `HRANDFIELD`).
10+
* Support Redis 6.2 commands (`LPOP`/`RPOP` with `count`, `COPY`, `GETEX`, `GETDEL`, `ZPOPMIN`, `BZPOPMIN`, `ZPOPMAX`, `BZPOPMAX`, `ZMSCORE`, `ZDIFF`, `ZDIFFSTORE`, `ZINTER`, `ZUNION`, `HRANDFIELD`, `ZRANDMEMBER`).
1111

1212
[[new-in-2.5.0]]
1313
== New in Spring Data Redis 2.5

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

+73
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
8585
private final TupleConverter tupleConverter = new TupleConverter();
8686
private SetConverter<Tuple, StringTuple> tupleToStringTuple = new SetConverter<>(tupleConverter);
8787
private SetConverter<StringTuple, Tuple> stringTupleToTuple = new SetConverter<>(new StringTupleConverter());
88+
private ListConverter<Tuple, StringTuple> tupleListToStringTuple = new ListConverter<>(new TupleConverter());
8889
private ListConverter<byte[], String> byteListToStringList = new ListConverter<>(bytesToString);
8990
private MapConverter<byte[], String> byteMapToStringMap = new MapConverter<>(bytesToString);
9091
private MapConverter<String, byte[]> stringMapToByteMap = new MapConverter<>(stringToBytes);
@@ -3296,6 +3297,78 @@ public Long zInterStore(String destKey, String... sets) {
32963297
return zInterStore(serialize(destKey), serializeMulti(sets));
32973298
}
32983299

3300+
/*
3301+
* (non-Javadoc)
3302+
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRandMember(byte[])
3303+
*/
3304+
@Override
3305+
public byte[] zRandMember(byte[] key) {
3306+
return delegate.zRandMember(key);
3307+
}
3308+
3309+
/*
3310+
* (non-Javadoc)
3311+
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRandMember(byte[], long)
3312+
*/
3313+
@Override
3314+
public List<byte[]> zRandMember(byte[] key, long count) {
3315+
return delegate.zRandMember(key, count);
3316+
}
3317+
3318+
/*
3319+
* (non-Javadoc)
3320+
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRandMemberWithScore(byte[])
3321+
*/
3322+
@Override
3323+
public Tuple zRandMemberWithScore(byte[] key) {
3324+
return delegate.zRandMemberWithScore(key);
3325+
}
3326+
3327+
/*
3328+
* (non-Javadoc)
3329+
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRandMemberWithScore(byte[], long)
3330+
*/
3331+
@Override
3332+
public List<Tuple> zRandMemberWithScore(byte[] key, long count) {
3333+
return delegate.zRandMemberWithScore(key, count);
3334+
}
3335+
3336+
/*
3337+
* (non-Javadoc)
3338+
* @see org.springframework.data.redis.connection.StringRedisConnection#zRandMember(java.lang.String)
3339+
*/
3340+
@Override
3341+
public String zRandMember(String key) {
3342+
return convertAndReturn(delegate.zRandMember(serialize(key)), bytesToString);
3343+
}
3344+
3345+
/*
3346+
* (non-Javadoc)
3347+
* @see org.springframework.data.redis.connection.StringRedisConnection#zRandMember(java.lang.String, long)
3348+
*/
3349+
@Override
3350+
public List<String> zRandMember(String key, long count) {
3351+
return convertAndReturn(delegate.zRandMember(serialize(key), count), byteListToStringList);
3352+
}
3353+
3354+
/*
3355+
* (non-Javadoc)
3356+
* @see org.springframework.data.redis.connection.StringRedisConnection#zRandMemberWithScore(java.lang.String)
3357+
*/
3358+
@Override
3359+
public StringTuple zRandMemberWithScore(String key) {
3360+
return convertAndReturn(delegate.zRandMemberWithScore(serialize(key)), new TupleConverter());
3361+
}
3362+
3363+
/*
3364+
* (non-Javadoc)
3365+
* @see org.springframework.data.redis.connection.StringRedisConnection#zRandMemberWithScores(java.lang.String, long)
3366+
*/
3367+
@Override
3368+
public List<StringTuple> zRandMemberWithScores(String key, long count) {
3369+
return convertAndReturn(delegate.zRandMemberWithScore(serialize(key), count), tupleListToStringTuple);
3370+
}
3371+
32993372
/*
33003373
* (non-Javadoc)
33013374
* @see org.springframework.data.redis.connection.StringRedisConnection#zRange(java.lang.String, long, long)

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

+28
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,34 @@ default Long zInterStore(byte[] destKey, byte[]... sets) {
10681068
return zSetCommands().zInterStore(destKey, sets);
10691069
}
10701070

1071+
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
1072+
@Override
1073+
@Deprecated
1074+
default byte[] zRandMember(byte[] key) {
1075+
return zSetCommands().zRandMember(key);
1076+
}
1077+
1078+
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
1079+
@Override
1080+
@Deprecated
1081+
default List<byte[]> zRandMember(byte[] key, long count) {
1082+
return zSetCommands().zRandMember(key, count);
1083+
}
1084+
1085+
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
1086+
@Override
1087+
@Deprecated
1088+
default Tuple zRandMemberWithScore(byte[] key) {
1089+
return zSetCommands().zRandMemberWithScore(key);
1090+
}
1091+
1092+
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
1093+
@Override
1094+
@Deprecated
1095+
default List<Tuple> zRandMemberWithScore(byte[] key, long count) {
1096+
return zSetCommands().zRandMemberWithScore(key, count);
1097+
}
1098+
10711099
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
10721100
@Override
10731101
@Deprecated

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

+136
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,142 @@ default Mono<Double> zIncrBy(ByteBuffer key, Number increment, ByteBuffer value)
477477
*/
478478
Flux<NumericResponse<ZIncrByCommand, Double>> zIncrBy(Publisher<ZIncrByCommand> commands);
479479

480+
/**
481+
* {@code ZRANDMEMBER} command parameters.
482+
*
483+
* @author Mark Paluch
484+
* @since 2.6
485+
* @see <a href="https://redis.io/commands/srandmember">Redis Documentation: ZRANDMEMBER</a>
486+
*/
487+
class ZRandMemberCommand extends KeyCommand {
488+
489+
private final long count;
490+
491+
private ZRandMemberCommand(@Nullable ByteBuffer key, long count) {
492+
493+
super(key);
494+
this.count = count;
495+
}
496+
497+
/**
498+
* Creates a new {@link ZRandMemberCommand} given the number of values to retrieve.
499+
*
500+
* @param nrValuesToRetrieve
501+
* @return a new {@link ZRandMemberCommand} for a number of values to retrieve.
502+
*/
503+
public static ZRandMemberCommand valueCount(long nrValuesToRetrieve) {
504+
return new ZRandMemberCommand(null, nrValuesToRetrieve);
505+
}
506+
507+
/**
508+
* Creates a new {@link ZRandMemberCommand} to retrieve one random member.
509+
*
510+
* @return a new {@link ZRandMemberCommand} to retrieve one random member.
511+
*/
512+
public static ZRandMemberCommand singleValue() {
513+
return new ZRandMemberCommand(null, 1);
514+
}
515+
516+
/**
517+
* Applies the {@literal key}. Constructs a new command instance with all previously configured properties.
518+
*
519+
* @param key must not be {@literal null}.
520+
* @return a new {@link ZRandMemberCommand} with {@literal key} applied.
521+
*/
522+
public ZRandMemberCommand from(ByteBuffer key) {
523+
524+
Assert.notNull(key, "Key must not be null!");
525+
526+
return new ZRandMemberCommand(key, count);
527+
}
528+
529+
/**
530+
* @return
531+
*/
532+
public long getCount() {
533+
return count;
534+
}
535+
}
536+
537+
/**
538+
* Get random element from sorted set at {@code key}.
539+
*
540+
* @param key must not be {@literal null}.
541+
* @return
542+
* @since 2.6
543+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
544+
*/
545+
default Mono<ByteBuffer> zRandMember(ByteBuffer key) {
546+
return zRandMember(Mono.just(ZRandMemberCommand.singleValue().from(key))).flatMap(CommandResponse::getOutput)
547+
.next();
548+
}
549+
550+
/**
551+
* Get {@code count} random elements from sorted set at {@code key}.
552+
*
553+
* @param key must not be {@literal null}.
554+
* @param count if the provided {@code count} argument is positive, return a list of distinct fields, capped either at
555+
* {@code count} or the set size. If {@code count} is negative, the behavior changes and the command is
556+
* allowed to return the same value multiple times. In this case, the number of returned values is the
557+
* absolute value of the specified count.
558+
* @return
559+
* @since 2.6
560+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
561+
*/
562+
default Flux<ByteBuffer> zRandMember(ByteBuffer key, long count) {
563+
return zRandMember(Mono.just(ZRandMemberCommand.valueCount(count).from(key))).flatMap(CommandResponse::getOutput);
564+
}
565+
566+
/**
567+
* Get random elements from sorted set at {@code key}.
568+
*
569+
* @param commands must not be {@literal null}.
570+
* @return
571+
* @since 2.6
572+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
573+
*/
574+
Flux<CommandResponse<ZRandMemberCommand, Flux<ByteBuffer>>> zRandMember(Publisher<ZRandMemberCommand> commands);
575+
576+
/**
577+
* Get random element from sorted set at {@code key}.
578+
*
579+
* @param key must not be {@literal null}.
580+
* @return
581+
* @since 2.6
582+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
583+
*/
584+
default Mono<Tuple> zRandMemberWithScore(ByteBuffer key) {
585+
return zRandMemberWithScore(Mono.just(ZRandMemberCommand.singleValue().from(key)))
586+
.flatMap(CommandResponse::getOutput).next();
587+
}
588+
589+
/**
590+
* Get {@code count} random elements from sorted set at {@code key}.
591+
*
592+
* @param key must not be {@literal null}.
593+
* @param count if the provided {@code count} argument is positive, return a list of distinct fields, capped either at
594+
* {@code count} or the set size. If {@code count} is negative, the behavior changes and the command is
595+
* allowed to return the same value multiple times. In this case, the number of returned values is the
596+
* absolute value of the specified count.
597+
* @return
598+
* @since 2.6
599+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
600+
*/
601+
default Flux<Tuple> zRandMemberWithScore(ByteBuffer key, long count) {
602+
return zRandMemberWithScore(Mono.just(ZRandMemberCommand.valueCount(count).from(key)))
603+
.flatMap(CommandResponse::getOutput);
604+
}
605+
606+
/**
607+
* Get random elements from sorted set at {@code key}.
608+
*
609+
* @param commands must not be {@literal null}.
610+
* @return
611+
* @since 2.6
612+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
613+
*/
614+
Flux<CommandResponse<ZRandMemberCommand, Flux<Tuple>>> zRandMemberWithScore(Publisher<ZRandMemberCommand> commands);
615+
480616
/**
481617
* {@code ZRANK}/{@literal ZREVRANK} command parameters.
482618
*

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

+52
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,58 @@ default Long zAdd(byte[] key, Set<Tuple> tuples) {
634634
@Nullable
635635
Double zIncrBy(byte[] key, double increment, byte[] value);
636636

637+
/**
638+
* Get random element from sorted set at {@code key}.
639+
*
640+
* @param key must not be {@literal null}.
641+
* @return can be {@literal null}.
642+
* @since 2.6
643+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
644+
*/
645+
@Nullable
646+
byte[] zRandMember(byte[] key);
647+
648+
/**
649+
* Get {@code count} random elements from sorted set at {@code key}.
650+
*
651+
* @param key must not be {@literal null}.
652+
* @param count if the provided {@code count} argument is positive, return a list of distinct fields, capped either at
653+
* {@code count} or the set size. If {@code count} is negative, the behavior changes and the command is
654+
* allowed to return the same value multiple times. In this case, the number of returned values is the
655+
* absolute value of the specified count.
656+
* @return {@literal null} when used in pipeline / transaction.
657+
* @since 2.6
658+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
659+
*/
660+
@Nullable
661+
List<byte[]> zRandMember(byte[] key, long count);
662+
663+
/**
664+
* Get random element from sorted set at {@code key}.
665+
*
666+
* @param key must not be {@literal null}.
667+
* @return can be {@literal null}.
668+
* @since 2.6
669+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
670+
*/
671+
@Nullable
672+
Tuple zRandMemberWithScore(byte[] key);
673+
674+
/**
675+
* Get {@code count} random elements from sorted set at {@code key}.
676+
*
677+
* @param key must not be {@literal null}.
678+
* @param count if the provided {@code count} argument is positive, return a list of distinct fields, capped either at
679+
* {@code count} or the set size. If {@code count} is negative, the behavior changes and the command is
680+
* allowed to return the same value multiple times. In this case, the number of returned values is the
681+
* absolute value of the specified count.
682+
* @return {@literal null} when used in pipeline / transaction.
683+
* @since 2.6
684+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
685+
*/
686+
@Nullable
687+
List<Tuple> zRandMemberWithScore(byte[] key, long count);
688+
637689
/**
638690
* Determine the index of element with {@code value} in a sorted set.
639691
*

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

+52
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,58 @@ default Long lPos(String key, String element) {
12291229
*/
12301230
Double zIncrBy(String key, double increment, String value);
12311231

1232+
/**
1233+
* Get random element from sorted set at {@code key}.
1234+
*
1235+
* @param key must not be {@literal null}.
1236+
* @return can be {@literal null}.
1237+
* @since 2.6
1238+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
1239+
*/
1240+
@Nullable
1241+
String zRandMember(String key);
1242+
1243+
/**
1244+
* Get {@code count} random elements from sorted set at {@code key}.
1245+
*
1246+
* @param key must not be {@literal null}.
1247+
* @param count if the provided {@code count} argument is positive, return a list of distinct fields, capped either at
1248+
* {@code count} or the set size. If {@code count} is negative, the behavior changes and the command is
1249+
* allowed to return the same value multiple times. In this case, the number of returned values is the
1250+
* absolute value of the specified count.
1251+
* @return {@literal null} when used in pipeline / transaction.
1252+
* @since 2.6
1253+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
1254+
*/
1255+
@Nullable
1256+
List<String> zRandMember(String key, long count);
1257+
1258+
/**
1259+
* Get random element from sorted set at {@code key}.
1260+
*
1261+
* @param key must not be {@literal null}.
1262+
* @return can be {@literal null}.
1263+
* @since 2.6
1264+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
1265+
*/
1266+
@Nullable
1267+
StringTuple zRandMemberWithScore(String key);
1268+
1269+
/**
1270+
* Get {@code count} random elements from sorted set at {@code key}.
1271+
*
1272+
* @param key must not be {@literal null}.
1273+
* @param count if the provided {@code count} argument is positive, return a list of distinct fields, capped either at
1274+
* {@code count} or the set size. If {@code count} is negative, the behavior changes and the command is
1275+
* allowed to return the same value multiple times. In this case, the number of returned values is the
1276+
* absolute value of the specified count.
1277+
* @return {@literal null} when used in pipeline / transaction.
1278+
* @since 2.6
1279+
* @see <a href="https://redis.io/commands/zrandmember">Redis Documentation: ZRANDMEMBER</a>
1280+
*/
1281+
@Nullable
1282+
List<StringTuple> zRandMemberWithScores(String key, long count);
1283+
12321284
/**
12331285
* Determine the index of element with {@code value} in a sorted set.
12341286
*

0 commit comments

Comments
 (0)