@@ -512,6 +512,145 @@ default Mono<Long> hLen(ByteBuffer key) {
512
512
*/
513
513
Flux <NumericResponse <KeyCommand , Long >> hLen (Publisher <KeyCommand > commands );
514
514
515
+ /**
516
+ * {@literal HRANDFIELD} {@link Command}.
517
+ *
518
+ * @author Mark Paluch
519
+ * @since 2.6
520
+ * @see <a href="https://redis.io/commands/hrandfield">Redis Documentation: HRANDFIELD</a>
521
+ */
522
+ class HRandFieldCommand extends KeyCommand {
523
+
524
+ private long count ;
525
+
526
+ private HRandFieldCommand (@ Nullable ByteBuffer key , long count ) {
527
+
528
+ super (key );
529
+
530
+ this .count = count ;
531
+ }
532
+
533
+ /**
534
+ * Applies the hash {@literal key}. Constructs a new command instance with all previously configured properties.
535
+ *
536
+ * @param key must not be {@literal null}.
537
+ * @return a new {@link HRandFieldCommand} with {@literal key} applied.
538
+ */
539
+ public static HRandFieldCommand key (ByteBuffer key ) {
540
+
541
+ Assert .notNull (key , "Key must not be null!" );
542
+
543
+ return new HRandFieldCommand (key , 1 );
544
+ }
545
+
546
+ /**
547
+ * Applies the {@literal count}. Constructs a new command instance with all previously configured properties. If the
548
+ * provided {@code count} argument is positive, return a list of distinct fields, capped either at {@code count} or
549
+ * the hash size. If {@code count} is negative, the behavior changes and the command is allowed to return the same
550
+ * field multiple times. In this case, the number of returned fields is the absolute value of the specified count.
551
+ *
552
+ * @param count
553
+ * @return a new {@link HRandFieldCommand} with {@literal key} applied.
554
+ */
555
+ public HRandFieldCommand count (long count ) {
556
+ return new HRandFieldCommand (getKey (), count );
557
+ }
558
+
559
+ public long getCount () {
560
+ return count ;
561
+ }
562
+ }
563
+
564
+ /**
565
+ * Return a random field from the hash value stored at {@code key}.
566
+ *
567
+ * @param key must not be {@literal null}.
568
+ * @return
569
+ * @since 2.6
570
+ * @see <a href="https://redis.io/commands/hrandfield">Redis Documentation: HRANDFIELD</a>
571
+ */
572
+ default Mono <ByteBuffer > hRandField (ByteBuffer key ) {
573
+
574
+ Assert .notNull (key , "Key must not be null!" );
575
+
576
+ return hRandField (Mono .just (HRandFieldCommand .key (key ).count (1 ))).flatMap (CommandResponse ::getOutput ).next ();
577
+ }
578
+
579
+ /**
580
+ * Return a random field from the hash value stored at {@code key}.
581
+ *
582
+ * @param key must not be {@literal null}.
583
+ * @return
584
+ * @since 2.6
585
+ * @see <a href="https://redis.io/commands/hrandfield">Redis Documentation: HRANDFIELD</a>
586
+ */
587
+ default Mono <Map .Entry <ByteBuffer , ByteBuffer >> hRandFieldWithValues (ByteBuffer key ) {
588
+
589
+ Assert .notNull (key , "Key must not be null!" );
590
+
591
+ return hRandFieldWithValues (Mono .just (HRandFieldCommand .key (key ).count (1 ))).flatMap (CommandResponse ::getOutput )
592
+ .next ();
593
+ }
594
+
595
+ /**
596
+ * Return a random field from the hash value stored at {@code key}. If the provided {@code count} argument is
597
+ * positive, return a list of distinct fields, capped either at {@code count} or the hash size. If {@code count} is
598
+ * negative, the behavior changes and the command is allowed to return the same field multiple times. In this case,
599
+ * the number of returned fields is the absolute value of the specified count.
600
+ *
601
+ * @param key must not be {@literal null}.
602
+ * @param count number of fields to return.
603
+ * @return
604
+ * @since 2.6
605
+ * @see <a href="https://redis.io/commands/hrandfield">Redis Documentation: HRANDFIELD</a>
606
+ */
607
+ default Flux <ByteBuffer > hRandField (ByteBuffer key , long count ) {
608
+
609
+ Assert .notNull (key , "Key must not be null!" );
610
+
611
+ return hRandField (Mono .just (HRandFieldCommand .key (key ).count (count ))).flatMap (CommandResponse ::getOutput );
612
+ }
613
+
614
+ /**
615
+ * Return a random field from the hash value stored at {@code key}. If the provided {@code count} argument is
616
+ * positive, return a list of distinct fields, capped either at {@code count} or the hash size. If {@code count} is
617
+ * negative, the behavior changes and the command is allowed to return the same field multiple times. In this case,
618
+ * the number of returned fields is the absolute value of the specified count.
619
+ *
620
+ * @param key must not be {@literal null}.
621
+ * @param count number of fields to return.
622
+ * @return {@literal null} if key does not exist or when used in pipeline / transaction.
623
+ * @since 2.6
624
+ * @see <a href="https://redis.io/commands/hrandfield">Redis Documentation: HRANDFIELD</a>
625
+ */
626
+ default Flux <Map .Entry <ByteBuffer , ByteBuffer >> hRandFieldWithValues (ByteBuffer key , long count ) {
627
+
628
+ Assert .notNull (key , "Key must not be null!" );
629
+
630
+ return hRandFieldWithValues (Mono .just (HRandFieldCommand .key (key ).count (count ))).flatMap (CommandResponse ::getOutput );
631
+ }
632
+
633
+ /**
634
+ * Get random fields of hash at {@literal key}.
635
+ *
636
+ * @param commands must not be {@literal null}.
637
+ * @return
638
+ * @since 2.6
639
+ * @see <a href="https://redis.io/commands/hrandfield">Redis Documentation: HRANDFIELD</a>
640
+ */
641
+ Flux <CommandResponse <HRandFieldCommand , Flux <ByteBuffer >>> hRandField (Publisher <HRandFieldCommand > commands );
642
+
643
+ /**
644
+ * Get random fields along their values of hash at {@literal key}.
645
+ *
646
+ * @param commands must not be {@literal null}.
647
+ * @return
648
+ * @since 2.6
649
+ * @see <a href="https://redis.io/commands/hrandfield">Redis Documentation: HRANDFIELD</a>
650
+ */
651
+ Flux <CommandResponse <HRandFieldCommand , Flux <Map .Entry <ByteBuffer , ByteBuffer >>>> hRandFieldWithValues (
652
+ Publisher <HRandFieldCommand > commands );
653
+
515
654
/**
516
655
* Get key set (fields) of hash at {@literal key}.
517
656
*
0 commit comments