2626import java .util .Map ;
2727import java .util .Set ;
2828import java .util .concurrent .TimeUnit ;
29+ import java .util .function .Function ;
2930
3031import org .springframework .beans .factory .BeanClassLoaderAware ;
3132import org .springframework .dao .InvalidDataAccessApiUsageException ;
@@ -551,21 +552,35 @@ protected <T> T postProcessResult(@Nullable T result, RedisConnection conn, bool
551552 // Methods dealing with Redis Keys
552553 // -------------------------------------------------------------------------
553554
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+
554569 @ Override
555570 public Boolean copy (K source , K target , boolean replace ) {
556571
557572 byte [] sourceKey = rawKey (source );
558573 byte [] targetKey = rawKey (target );
559574
560- return execute (connection -> connection .copy (sourceKey , targetKey , replace ), true );
575+ return doWithKeys (connection -> connection .copy (sourceKey , targetKey , replace ));
561576 }
562577
563578 @ Override
564579 public Boolean delete (K key ) {
565580
566581 byte [] rawKey = rawKey (key );
567582
568- Long result = execute (connection -> connection .del (rawKey ), true );
583+ Long result = doWithKeys (connection -> connection .del (rawKey ));
569584 return result != null && result .intValue () == 1 ;
570585 }
571586
@@ -578,15 +593,15 @@ public Long delete(Collection<K> keys) {
578593
579594 byte [][] rawKeys = rawKeys (keys );
580595
581- return execute (connection -> connection .del (rawKeys ), true );
596+ return doWithKeys (connection -> connection .del (rawKeys ));
582597 }
583598
584599 @ Override
585600 public Boolean unlink (K key ) {
586601
587602 byte [] rawKey = rawKey (key );
588603
589- Long result = execute (connection -> connection .unlink (rawKey ), true );
604+ Long result = doWithKeys (connection -> connection .unlink (rawKey ));
590605
591606 return result != null && result .intValue () == 1 ;
592607 }
@@ -600,15 +615,15 @@ public Long unlink(Collection<K> keys) {
600615
601616 byte [][] rawKeys = rawKeys (keys );
602617
603- return execute (connection -> connection .unlink (rawKeys ), true );
618+ return doWithKeys (connection -> connection .unlink (rawKeys ));
604619 }
605620
606621 @ Override
607622 public Boolean hasKey (K key ) {
608623
609624 byte [] rawKey = rawKey (key );
610625
611- return execute (connection -> connection .exists (rawKey ), true );
626+ return doWithKeys (connection -> connection .exists (rawKey ));
612627 }
613628
614629 @ Override
@@ -617,7 +632,7 @@ public Long countExistingKeys(Collection<K> keys) {
617632 Assert .notNull (keys , "Keys must not be null!" );
618633
619634 byte [][] rawKeys = rawKeys (keys );
620- return execute (connection -> connection .exists (rawKeys ), true );
635+ return doWithKeys (connection -> connection .exists (rawKeys ));
621636 }
622637
623638 @ Override
@@ -626,75 +641,61 @@ public Boolean expire(K key, final long timeout, final TimeUnit unit) {
626641 byte [] rawKey = rawKey (key );
627642 long rawTimeout = TimeoutUtils .toMillis (timeout , unit );
628643
629- return execute (connection -> {
644+ return doWithKeys (connection -> {
630645 try {
631646 return connection .pExpire (rawKey , rawTimeout );
632647 } catch (Exception e ) {
633648 // Driver may not support pExpire or we may be running on Redis 2.4
634649 return connection .expire (rawKey , TimeoutUtils .toSeconds (timeout , unit ));
635650 }
636- }, true );
651+ });
637652 }
638653
639654 @ Override
640655 public Boolean expireAt (K key , final Date date ) {
641656
642657 byte [] rawKey = rawKey (key );
643658
644- return execute (connection -> {
659+ return doWithKeys (connection -> {
645660 try {
646661 return connection .pExpireAt (rawKey , date .getTime ());
647662 } catch (Exception e ) {
648663 return connection .expireAt (rawKey , date .getTime () / 1000 );
649664 }
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+ });
665666 }
666667
667- //
668- // Value operations
669- //
668+ // -------------------------------------------------------------------------
669+ // Methods dealing with value operations
670+ // -------------------------------------------------------------------------
670671
671672 @ Override
672673 public Long getExpire (K key ) {
673674
674675 byte [] rawKey = rawKey (key );
675- return execute (connection -> connection .ttl (rawKey ), true );
676+ return doWithKeys (connection -> connection .ttl (rawKey ));
676677 }
677678
678679 @ Override
679680 public Long getExpire (K key , final TimeUnit timeUnit ) {
680681
681682 byte [] rawKey = rawKey (key );
682- return execute (connection -> {
683+ return doWithKeys (connection -> {
683684 try {
684685 return connection .pTtl (rawKey , timeUnit );
685686 } catch (Exception e ) {
686687 // Driver may not support pTtl or we may be running on Redis 2.4
687688 return connection .ttl (rawKey , timeUnit );
688689 }
689- }, true );
690+ });
690691 }
691692
692693 @ Override
693694 @ SuppressWarnings ("unchecked" )
694695 public Set <K > keys (K pattern ) {
695696
696697 byte [] rawKey = rawKey (pattern );
697- Set <byte []> rawKeys = execute (connection -> connection .keys (rawKey ), true );
698+ Set <byte []> rawKeys = doWithKeys (connection -> connection .keys (rawKey ));
698699
699700 return keySerializer != null ? SerializationUtils .deserialize (rawKeys , keySerializer ) : (Set <K >) rawKeys ;
700701 }
@@ -712,20 +713,20 @@ public Cursor<K> scan(ScanOptions options) {
712713 public Boolean persist (K key ) {
713714
714715 byte [] rawKey = rawKey (key );
715- return execute (connection -> connection .persist (rawKey ), true );
716+ return doWithKeys (connection -> connection .persist (rawKey ));
716717 }
717718
718719 @ Override
719720 public Boolean move (K key , final int dbIndex ) {
720721
721722 byte [] rawKey = rawKey (key );
722- return execute (connection -> connection .move (rawKey , dbIndex ), true );
723+ return doWithKeys (connection -> connection .move (rawKey , dbIndex ));
723724 }
724725
725726 @ Override
726727 public K randomKey () {
727728
728- byte [] rawKey = execute (RedisKeyCommands ::randomKey , true );
729+ byte [] rawKey = doWithKeys (RedisKeyCommands ::randomKey );
729730 return deserializeKey (rawKey );
730731 }
731732
@@ -735,25 +736,25 @@ public void rename(K oldKey, K newKey) {
735736 byte [] rawOldKey = rawKey (oldKey );
736737 byte [] rawNewKey = rawKey (newKey );
737738
738- execute (connection -> {
739+ doWithKeys (connection -> {
739740 connection .rename (rawOldKey , rawNewKey );
740741 return null ;
741- }, true );
742+ });
742743 }
743744
744745 @ Override
745746 public Boolean renameIfAbsent (K oldKey , K newKey ) {
746747
747748 byte [] rawOldKey = rawKey (oldKey );
748749 byte [] rawNewKey = rawKey (newKey );
749- return execute (connection -> connection .renameNX (rawOldKey , rawNewKey ), true );
750+ return doWithKeys (connection -> connection .renameNX (rawOldKey , rawNewKey ));
750751 }
751752
752753 @ Override
753754 public DataType type (K key ) {
754755
755756 byte [] rawKey = rawKey (key );
756- return execute (connection -> connection .type (rawKey ), true );
757+ return doWithKeys (connection -> connection .type (rawKey ));
757758 }
758759
759760 /**
@@ -768,7 +769,7 @@ public DataType type(K key) {
768769 public byte [] dump (K key ) {
769770
770771 byte [] rawKey = rawKey (key );
771- return execute (connection -> connection .dump (rawKey ), true );
772+ return doWithKeys (connection -> connection .dump (rawKey ));
772773 }
773774
774775 /**
@@ -784,18 +785,26 @@ public byte[] dump(K key) {
784785 * {@literal false}.
785786 */
786787 @ 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 ) {
788789
789790 byte [] rawKey = rawKey (key );
790791 long rawTimeout = TimeoutUtils .toMillis (timeToLive , unit );
791792
792- execute (connection -> {
793+ doWithKeys (connection -> {
793794 connection .restore (rawKey , rawTimeout , value , replace );
794795 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 );
796802 }
797803
798- // Sort operations
804+
805+ // -------------------------------------------------------------------------
806+ // Methods dealing with sorting
807+ // -------------------------------------------------------------------------
799808
800809 @ Override
801810 @ SuppressWarnings ("unchecked" )
@@ -809,7 +818,7 @@ public <T> List<T> sort(SortQuery<K> query, @Nullable RedisSerializer<T> resultS
809818 byte [] rawKey = rawKey (query .getKey ());
810819 SortParameters params = QueryUtils .convertQuery (query , stringSerializer );
811820
812- List <byte []> vals = execute (connection -> connection .sort (rawKey , params ), true );
821+ List <byte []> vals = doWithKeys (connection -> connection .sort (rawKey , params ));
813822
814823 return SerializationUtils .deserialize (vals , resultSerializer );
815824 }
@@ -858,10 +867,9 @@ public Long sort(SortQuery<K> query, K storeKey) {
858867 }
859868
860869 // -------------------------------------------------------------------------
861- // Methods dealing with Redis Transactions
870+ // Methods dealing with Transactions
862871 // -------------------------------------------------------------------------
863872
864-
865873 @ Override
866874 public void watch (K key ) {
867875
@@ -893,6 +901,7 @@ public void unwatch() {
893901 }, true );
894902 }
895903
904+
896905 @ Override
897906 public void multi () {
898907 execute (connection -> {
0 commit comments