17
17
18
18
import java .util .List ;
19
19
20
+ import lombok .NonNull ;
21
+ import lombok .RequiredArgsConstructor ;
22
+ import org .springframework .dao .DataAccessException ;
20
23
import org .springframework .dao .InvalidDataAccessApiUsageException ;
21
24
import org .springframework .data .redis .connection .RedisScriptingCommands ;
22
25
import org .springframework .data .redis .connection .ReturnType ;
26
+ import redis .clients .jedis .JedisCluster ;
23
27
24
28
/**
25
29
* @author Mark Paluch
26
30
* @since 2.0
27
31
*/
28
- enum JedisClusterScriptingCommands implements RedisScriptingCommands {
32
+ @ RequiredArgsConstructor
33
+ class JedisClusterScriptingCommands implements RedisScriptingCommands {
29
34
30
- INSTANCE ;
35
+ private final @ NonNull JedisClusterConnection clusterConnection ;
31
36
32
37
/*
33
38
* (non-Javadoc)
@@ -70,8 +75,15 @@ public List<Boolean> scriptExists(String... scriptShas) {
70
75
* @see org.springframework.data.redis.connection.RedisScriptingCommands#eval(byte[], org.springframework.data.redis.connection.ReturnType, int, byte[][])
71
76
*/
72
77
@ Override
78
+ @ SuppressWarnings ("unchecked" )
73
79
public <T > T eval (byte [] script , ReturnType returnType , int numKeys , byte []... keysAndArgs ) {
74
- throw new InvalidDataAccessApiUsageException ("Eval is not supported in cluster environment." );
80
+ checkConnection ();
81
+ try {
82
+ return (T ) new JedisScriptReturnConverter (returnType )
83
+ .convert (getCluster ().eval (script , JedisConverters .toBytes (numKeys ), keysAndArgs ));
84
+ } catch (Exception ex ) {
85
+ throw convertJedisAccessException (ex );
86
+ }
75
87
}
76
88
77
89
/*
@@ -80,15 +92,51 @@ public <T> T eval(byte[] script, ReturnType returnType, int numKeys, byte[]... k
80
92
*/
81
93
@ Override
82
94
public <T > T evalSha (String scriptSha , ReturnType returnType , int numKeys , byte []... keysAndArgs ) {
83
- throw new InvalidDataAccessApiUsageException ( "EvalSha is not supported in cluster environment." );
95
+ return evalSha ( JedisConverters . toBytes ( scriptSha ), returnType , numKeys , keysAndArgs );
84
96
}
85
97
86
98
/*
87
99
* (non-Javadoc)
88
100
* @see org.springframework.data.redis.connection.RedisScriptingCommands#evalSha(byte[], org.springframework.data.redis.connection.ReturnType, int, byte[][])
89
101
*/
90
102
@ Override
103
+ @ SuppressWarnings ("unchecked" )
91
104
public <T > T evalSha (byte [] scriptSha , ReturnType returnType , int numKeys , byte []... keysAndArgs ) {
92
- throw new InvalidDataAccessApiUsageException ("EvalSha is not supported in cluster environment." );
105
+ checkConnection ();
106
+ try {
107
+ return (T ) new JedisScriptReturnConverter (returnType )
108
+ .convert (getCluster ().evalsha (scriptSha , numKeys , keysAndArgs ));
109
+ } catch (Exception ex ) {
110
+ throw convertJedisAccessException (ex );
111
+ }
112
+ }
113
+
114
+ public JedisClusterConnection getClusterConnection () {
115
+ return clusterConnection ;
116
+ }
117
+
118
+ protected RuntimeException convertJedisAccessException (Exception ex ) {
119
+ return clusterConnection .convertJedisAccessException (ex );
120
+ }
121
+
122
+ private JedisCluster getCluster () {
123
+ return clusterConnection .getCluster ();
124
+ }
125
+
126
+ protected void checkConnection () {
127
+ if (isQueueing ()) {
128
+ throw new UnsupportedOperationException ();
129
+ }
130
+ if (isPipelined ()) {
131
+ throw new UnsupportedOperationException ();
132
+ }
133
+ }
134
+
135
+ private boolean isPipelined () {
136
+ return clusterConnection .isPipelined ();
137
+ }
138
+
139
+ private boolean isQueueing () {
140
+ return clusterConnection .isQueueing ();
93
141
}
94
142
}
0 commit comments