28
28
import org .springframework .cache .support .SimpleValueWrapper ;
29
29
import org .springframework .dao .DataAccessException ;
30
30
import org .springframework .data .redis .RedisSystemException ;
31
+ import org .springframework .data .redis .connection .DecoratedRedisConnection ;
31
32
import org .springframework .data .redis .connection .RedisClusterConnection ;
32
33
import org .springframework .data .redis .connection .RedisConnection ;
33
34
import org .springframework .data .redis .connection .ReturnType ;
39
40
40
41
/**
41
42
* Cache implementation on top of Redis.
42
- *
43
+ *
43
44
* @author Costin Leau
44
45
* @author Christoph Strobl
45
46
* @author Thomas Darimont
47
+ * @author Mark Paluch
46
48
*/
47
49
@ SuppressWarnings ("unchecked" )
48
50
public class RedisCache implements Cache {
@@ -54,7 +56,7 @@ public class RedisCache implements Cache {
54
56
55
57
/**
56
58
* Constructs a new <code>RedisCache</code> instance.
57
- *
59
+ *
58
60
* @param name cache name
59
61
* @param prefix
60
62
* @param redisOperations
@@ -624,7 +626,16 @@ public Void doInLock(RedisConnection connection) throws DataAccessException {
624
626
byte [] prefixToUse = Arrays .copyOf (metadata .getKeyPrefix (), metadata .getKeyPrefix ().length + WILD_CARD .length );
625
627
System .arraycopy (WILD_CARD , 0 , prefixToUse , metadata .getKeyPrefix ().length , WILD_CARD .length );
626
628
627
- connection .eval (REMOVE_KEYS_BY_PATTERN_LUA , ReturnType .INTEGER , 0 , prefixToUse );
629
+ if (isClusterConnection (connection )) {
630
+
631
+ // load keys to the client because currently Redis Cluster connections do not allow eval of lua scripts.
632
+ Set <byte []> keys = connection .keys (prefixToUse );
633
+ if (!keys .isEmpty ()) {
634
+ connection .del (keys .toArray (new byte [keys .size ()][]));
635
+ }
636
+ } else {
637
+ connection .eval (REMOVE_KEYS_BY_PATTERN_LUA , ReturnType .INTEGER , 0 , prefixToUse );
638
+ }
628
639
629
640
return null ;
630
641
}
@@ -765,8 +776,9 @@ public byte[] doInRedis(BinaryRedisCacheElement element, RedisConnection connect
765
776
766
777
return value ;
767
778
} catch (RuntimeException e ) {
768
-
769
- connection .discard ();
779
+ if (!isClusterConnection (connection )) {
780
+ connection .discard ();
781
+ }
770
782
throw e ;
771
783
}
772
784
} finally {
@@ -810,6 +822,11 @@ public RuntimeException create(Object key, Callable<?> valueLoader, Throwable ca
810
822
}
811
823
812
824
private static boolean isClusterConnection (RedisConnection connection ) {
825
+
826
+ while (connection instanceof DecoratedRedisConnection ) {
827
+ connection = ((DecoratedRedisConnection ) connection ).getDelegate ();
828
+ }
829
+
813
830
return connection instanceof RedisClusterConnection ;
814
831
}
815
832
0 commit comments