26
26
import java .util .Map ;
27
27
import java .util .Set ;
28
28
import java .util .concurrent .TimeUnit ;
29
+ import java .util .function .Function ;
29
30
30
31
import org .springframework .beans .factory .BeanClassLoaderAware ;
31
32
import org .springframework .dao .InvalidDataAccessApiUsageException ;
@@ -551,21 +552,35 @@ protected <T> T postProcessResult(@Nullable T result, RedisConnection conn, bool
551
552
// Methods dealing with Redis Keys
552
553
// -------------------------------------------------------------------------
553
554
555
+ @ Override
556
+ public void convertAndSend (String channel , Object message ) {
557
+
558
+ Assert .hasText (channel , "a non-empty channel is required" );
559
+
560
+ byte [] rawChannel = rawString (channel );
561
+ byte [] rawMessage = rawValue (message );
562
+
563
+ execute (connection -> {
564
+ connection .publish (rawChannel , rawMessage );
565
+ return null ;
566
+ }, true );
567
+ }
568
+
554
569
@ Override
555
570
public Boolean copy (K source , K target , boolean replace ) {
556
571
557
572
byte [] sourceKey = rawKey (source );
558
573
byte [] targetKey = rawKey (target );
559
574
560
- return execute (connection -> connection .copy (sourceKey , targetKey , replace ), true );
575
+ return doWithKeys (connection -> connection .copy (sourceKey , targetKey , replace ));
561
576
}
562
577
563
578
@ Override
564
579
public Boolean delete (K key ) {
565
580
566
581
byte [] rawKey = rawKey (key );
567
582
568
- Long result = execute (connection -> connection .del (rawKey ), true );
583
+ Long result = doWithKeys (connection -> connection .del (rawKey ));
569
584
return result != null && result .intValue () == 1 ;
570
585
}
571
586
@@ -578,15 +593,15 @@ public Long delete(Collection<K> keys) {
578
593
579
594
byte [][] rawKeys = rawKeys (keys );
580
595
581
- return execute (connection -> connection .del (rawKeys ), true );
596
+ return doWithKeys (connection -> connection .del (rawKeys ));
582
597
}
583
598
584
599
@ Override
585
600
public Boolean unlink (K key ) {
586
601
587
602
byte [] rawKey = rawKey (key );
588
603
589
- Long result = execute (connection -> connection .unlink (rawKey ), true );
604
+ Long result = doWithKeys (connection -> connection .unlink (rawKey ));
590
605
591
606
return result != null && result .intValue () == 1 ;
592
607
}
@@ -600,15 +615,15 @@ public Long unlink(Collection<K> keys) {
600
615
601
616
byte [][] rawKeys = rawKeys (keys );
602
617
603
- return execute (connection -> connection .unlink (rawKeys ), true );
618
+ return doWithKeys (connection -> connection .unlink (rawKeys ));
604
619
}
605
620
606
621
@ Override
607
622
public Boolean hasKey (K key ) {
608
623
609
624
byte [] rawKey = rawKey (key );
610
625
611
- return execute (connection -> connection .exists (rawKey ), true );
626
+ return doWithKeys (connection -> connection .exists (rawKey ));
612
627
}
613
628
614
629
@ Override
@@ -617,7 +632,7 @@ public Long countExistingKeys(Collection<K> keys) {
617
632
Assert .notNull (keys , "Keys must not be null!" );
618
633
619
634
byte [][] rawKeys = rawKeys (keys );
620
- return execute (connection -> connection .exists (rawKeys ), true );
635
+ return doWithKeys (connection -> connection .exists (rawKeys ));
621
636
}
622
637
623
638
@ Override
@@ -626,75 +641,61 @@ public Boolean expire(K key, final long timeout, final TimeUnit unit) {
626
641
byte [] rawKey = rawKey (key );
627
642
long rawTimeout = TimeoutUtils .toMillis (timeout , unit );
628
643
629
- return execute (connection -> {
644
+ return doWithKeys (connection -> {
630
645
try {
631
646
return connection .pExpire (rawKey , rawTimeout );
632
647
} catch (Exception e ) {
633
648
// Driver may not support pExpire or we may be running on Redis 2.4
634
649
return connection .expire (rawKey , TimeoutUtils .toSeconds (timeout , unit ));
635
650
}
636
- }, true );
651
+ });
637
652
}
638
653
639
654
@ Override
640
655
public Boolean expireAt (K key , final Date date ) {
641
656
642
657
byte [] rawKey = rawKey (key );
643
658
644
- return execute (connection -> {
659
+ return doWithKeys (connection -> {
645
660
try {
646
661
return connection .pExpireAt (rawKey , date .getTime ());
647
662
} catch (Exception e ) {
648
663
return connection .expireAt (rawKey , date .getTime () / 1000 );
649
664
}
650
- }, true );
651
- }
652
-
653
- @ Override
654
- public void convertAndSend (String channel , Object message ) {
655
-
656
- Assert .hasText (channel , "a non-empty channel is required" );
657
-
658
- byte [] rawChannel = rawString (channel );
659
- byte [] rawMessage = rawValue (message );
660
-
661
- execute (connection -> {
662
- connection .publish (rawChannel , rawMessage );
663
- return null ;
664
- }, true );
665
+ });
665
666
}
666
667
667
- //
668
- // Value operations
669
- //
668
+ // -------------------------------------------------------------------------
669
+ // Methods dealing with value operations
670
+ // -------------------------------------------------------------------------
670
671
671
672
@ Override
672
673
public Long getExpire (K key ) {
673
674
674
675
byte [] rawKey = rawKey (key );
675
- return execute (connection -> connection .ttl (rawKey ), true );
676
+ return doWithKeys (connection -> connection .ttl (rawKey ));
676
677
}
677
678
678
679
@ Override
679
680
public Long getExpire (K key , final TimeUnit timeUnit ) {
680
681
681
682
byte [] rawKey = rawKey (key );
682
- return execute (connection -> {
683
+ return doWithKeys (connection -> {
683
684
try {
684
685
return connection .pTtl (rawKey , timeUnit );
685
686
} catch (Exception e ) {
686
687
// Driver may not support pTtl or we may be running on Redis 2.4
687
688
return connection .ttl (rawKey , timeUnit );
688
689
}
689
- }, true );
690
+ });
690
691
}
691
692
692
693
@ Override
693
694
@ SuppressWarnings ("unchecked" )
694
695
public Set <K > keys (K pattern ) {
695
696
696
697
byte [] rawKey = rawKey (pattern );
697
- Set <byte []> rawKeys = execute (connection -> connection .keys (rawKey ), true );
698
+ Set <byte []> rawKeys = doWithKeys (connection -> connection .keys (rawKey ));
698
699
699
700
return keySerializer != null ? SerializationUtils .deserialize (rawKeys , keySerializer ) : (Set <K >) rawKeys ;
700
701
}
@@ -712,20 +713,20 @@ public Cursor<K> scan(ScanOptions options) {
712
713
public Boolean persist (K key ) {
713
714
714
715
byte [] rawKey = rawKey (key );
715
- return execute (connection -> connection .persist (rawKey ), true );
716
+ return doWithKeys (connection -> connection .persist (rawKey ));
716
717
}
717
718
718
719
@ Override
719
720
public Boolean move (K key , final int dbIndex ) {
720
721
721
722
byte [] rawKey = rawKey (key );
722
- return execute (connection -> connection .move (rawKey , dbIndex ), true );
723
+ return doWithKeys (connection -> connection .move (rawKey , dbIndex ));
723
724
}
724
725
725
726
@ Override
726
727
public K randomKey () {
727
728
728
- byte [] rawKey = execute (RedisKeyCommands ::randomKey , true );
729
+ byte [] rawKey = doWithKeys (RedisKeyCommands ::randomKey );
729
730
return deserializeKey (rawKey );
730
731
}
731
732
@@ -735,25 +736,25 @@ public void rename(K oldKey, K newKey) {
735
736
byte [] rawOldKey = rawKey (oldKey );
736
737
byte [] rawNewKey = rawKey (newKey );
737
738
738
- execute (connection -> {
739
+ doWithKeys (connection -> {
739
740
connection .rename (rawOldKey , rawNewKey );
740
741
return null ;
741
- }, true );
742
+ });
742
743
}
743
744
744
745
@ Override
745
746
public Boolean renameIfAbsent (K oldKey , K newKey ) {
746
747
747
748
byte [] rawOldKey = rawKey (oldKey );
748
749
byte [] rawNewKey = rawKey (newKey );
749
- return execute (connection -> connection .renameNX (rawOldKey , rawNewKey ), true );
750
+ return doWithKeys (connection -> connection .renameNX (rawOldKey , rawNewKey ));
750
751
}
751
752
752
753
@ Override
753
754
public DataType type (K key ) {
754
755
755
756
byte [] rawKey = rawKey (key );
756
- return execute (connection -> connection .type (rawKey ), true );
757
+ return doWithKeys (connection -> connection .type (rawKey ));
757
758
}
758
759
759
760
/**
@@ -768,7 +769,7 @@ public DataType type(K key) {
768
769
public byte [] dump (K key ) {
769
770
770
771
byte [] rawKey = rawKey (key );
771
- return execute (connection -> connection .dump (rawKey ), true );
772
+ return doWithKeys (connection -> connection .dump (rawKey ));
772
773
}
773
774
774
775
/**
@@ -784,18 +785,26 @@ public byte[] dump(K key) {
784
785
* {@literal false}.
785
786
*/
786
787
@ Override
787
- public void restore (K key , final byte [] value , long timeToLive , TimeUnit unit , boolean replace ) {
788
+ public void restore (K key , byte [] value , long timeToLive , TimeUnit unit , boolean replace ) {
788
789
789
790
byte [] rawKey = rawKey (key );
790
791
long rawTimeout = TimeoutUtils .toMillis (timeToLive , unit );
791
792
792
- execute (connection -> {
793
+ doWithKeys (connection -> {
793
794
connection .restore (rawKey , rawTimeout , value , replace );
794
795
return null ;
795
- }, true );
796
+ });
797
+ }
798
+
799
+ @ Nullable
800
+ private <T > T doWithKeys (Function <RedisKeyCommands , T > action ) {
801
+ return execute ((RedisCallback <? extends T >) connection -> action .apply (connection .keyCommands ()), true );
796
802
}
797
803
798
- // Sort operations
804
+
805
+ // -------------------------------------------------------------------------
806
+ // Methods dealing with sorting
807
+ // -------------------------------------------------------------------------
799
808
800
809
@ Override
801
810
@ SuppressWarnings ("unchecked" )
@@ -809,7 +818,7 @@ public <T> List<T> sort(SortQuery<K> query, @Nullable RedisSerializer<T> resultS
809
818
byte [] rawKey = rawKey (query .getKey ());
810
819
SortParameters params = QueryUtils .convertQuery (query , stringSerializer );
811
820
812
- List <byte []> vals = execute (connection -> connection .sort (rawKey , params ), true );
821
+ List <byte []> vals = doWithKeys (connection -> connection .sort (rawKey , params ));
813
822
814
823
return SerializationUtils .deserialize (vals , resultSerializer );
815
824
}
@@ -858,10 +867,9 @@ public Long sort(SortQuery<K> query, K storeKey) {
858
867
}
859
868
860
869
// -------------------------------------------------------------------------
861
- // Methods dealing with Redis Transactions
870
+ // Methods dealing with Transactions
862
871
// -------------------------------------------------------------------------
863
872
864
-
865
873
@ Override
866
874
public void watch (K key ) {
867
875
@@ -893,6 +901,7 @@ public void unwatch() {
893
901
}, true );
894
902
}
895
903
904
+
896
905
@ Override
897
906
public void multi () {
898
907
execute (connection -> {
0 commit comments