Skip to content

Commit 33e8974

Browse files
mp911dechristophstrobl
authored andcommitted
Refine defaulted connection arrangement.
DefaultedRedis…Connection is now deprecated to help with migration towards segregated/commands interface usage. The connection now exposes a commands() object providing access to RedisCommands so that a composite commands object can be accessed independently from the connection. Redis…Commands provider represent entry points to obtain segregated command interfaces. See: #2273 Original Pull Request: #2276
1 parent f6eaa40 commit 33e8974

13 files changed

+470
-232
lines changed

src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @author Mark Paluch
3232
* @since 1.4
3333
*/
34-
public abstract class AbstractRedisConnection implements DefaultedRedisConnection {
34+
public abstract class AbstractRedisConnection implements RedisConnection {
3535

3636
private @Nullable RedisSentinelConfiguration sentinelConfiguration;
3737
private final Map<RedisNode, RedisSentinelConnection> connectionCache = new ConcurrentHashMap<>();

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* @author ihaohong
8181
* @author Dennis Neufeld
8282
*/
83+
@SuppressWarnings({ "ConstantConditions", "deprecation" })
8384
public class DefaultStringRedisConnection implements StringRedisConnection, DecoratedRedisConnection {
8485

8586
private static final byte[][] EMPTY_2D_BYTE_ARRAY = new byte[0][];
@@ -184,6 +185,66 @@ public DefaultStringRedisConnection(RedisConnection connection, RedisSerializer<
184185
this.byteGeoResultsToStringGeoResults = Converters.deserializingGeoResultsConverter(serializer);
185186
}
186187

188+
@Override
189+
public RedisCommands commands() {
190+
return this;
191+
}
192+
193+
@Override
194+
public RedisGeoCommands geoCommands() {
195+
return this;
196+
}
197+
198+
@Override
199+
public RedisHashCommands hashCommands() {
200+
return this;
201+
}
202+
203+
@Override
204+
public RedisHyperLogLogCommands hyperLogLogCommands() {
205+
return this;
206+
}
207+
208+
@Override
209+
public RedisKeyCommands keyCommands() {
210+
return this;
211+
}
212+
213+
@Override
214+
public RedisListCommands listCommands() {
215+
return this;
216+
}
217+
218+
@Override
219+
public RedisSetCommands setCommands() {
220+
return this;
221+
}
222+
223+
@Override
224+
public RedisScriptingCommands scriptingCommands() {
225+
return this;
226+
}
227+
228+
@Override
229+
public RedisServerCommands serverCommands() {
230+
return this;
231+
}
232+
233+
@Override
234+
public RedisStreamCommands streamCommands() {
235+
return this;
236+
}
237+
238+
@Override
239+
public RedisStringCommands stringCommands() {
240+
return this;
241+
}
242+
243+
@Override
244+
public RedisZSetCommands zSetCommands() {
245+
return this;
246+
}
247+
187248
@Override
188249
public Long append(byte[] key, byte[] value) {
189250
return convertAndReturn(delegate.append(key, value), Converters.identityConverter());
@@ -1011,6 +1072,13 @@ public Set<StringTuple> zInterWithScores(Aggregate aggregate, Weights weights, S
10111072
return convertAndReturn(delegate.zInterWithScores(aggregate, weights, serializeMulti(sets)), tupleToStringTuple);
10121073
}
10131074

1075+
@Nullable
1076+
@Override
1077+
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
1078+
return convertAndReturn(delegate.zInterStore(destKey, aggregate, Weights.of(weights), sets),
1079+
Converters.identityConverter());
1080+
}
1081+
10141082
@Override
10151083
public Long zInterStore(byte[] destKey, Aggregate aggregate, Weights weights, byte[]... sets) {
10161084
return convertAndReturn(delegate.zInterStore(destKey, aggregate, weights, sets), Converters.identityConverter());
@@ -1211,6 +1279,13 @@ public Long zUnionStore(byte[] destKey, Aggregate aggregate, Weights weights, by
12111279
return convertAndReturn(delegate.zUnionStore(destKey, aggregate, weights, sets), Converters.identityConverter());
12121280
}
12131281

1282+
@Nullable
1283+
@Override
1284+
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
1285+
return convertAndReturn(delegate.zUnionStore(destKey, aggregate, Weights.of(weights), sets),
1286+
Converters.identityConverter());
1287+
}
1288+
12141289
public Long zUnionStore(byte[] destKey, byte[]... sets) {
12151290
return convertAndReturn(delegate.zUnionStore(destKey, sets), Converters.identityConverter());
12161291
}
@@ -1303,7 +1378,6 @@ private GeoReference<byte[]> serialize(GeoReference<String> data) {
13031378
: (GeoReference) data;
13041379
}
13051380

1306-
@SuppressWarnings("unchecked")
13071381
private StreamOffset<byte[]>[] serialize(StreamOffset<String>[] offsets) {
13081382

13091383
return Arrays.stream(offsets).map(it -> StreamOffset.create(serialize(it.getKey()), it.getOffset()))

src/main/java/org/springframework/data/redis/connection/DefaultedRedisClusterConnection.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import java.util.Collection;
1918
import java.util.List;
2019
import java.util.Properties;
2120
import java.util.concurrent.TimeUnit;
2221

2322
import org.springframework.data.redis.core.types.RedisClientInfo;
24-
import org.springframework.lang.Nullable;
25-
import org.springframework.util.Assert;
2623

2724
/**
2825
* @author Christoph Strobl
2926
* @author Mark Paluch
3027
* @author Dennis Neufeld
3128
* @since 2.0
3229
*/
33-
public interface DefaultedRedisClusterConnection extends RedisClusterConnection, DefaultedRedisConnection {
30+
@Deprecated
31+
public interface DefaultedRedisClusterConnection
32+
extends DefaultedRedisConnection, RedisClusterCommands, RedisClusterServerCommands, RedisClusterCommandsProvider {
3433

3534
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
3635
@Override
@@ -165,24 +164,4 @@ default List<RedisClientInfo> getClientList(RedisClusterNode node) {
165164
return serverCommands().getClientList(node);
166165
}
167166

168-
@Nullable
169-
@Override
170-
@SuppressWarnings("unchecked")
171-
default <T> T execute(String command, byte[] key, Collection<byte[]> args) {
172-
173-
Assert.notNull(command, "Command must not be null!");
174-
Assert.notNull(key, "Key must not be null!");
175-
Assert.notNull(args, "Args must not be null!");
176-
177-
byte[][] commandArgs = new byte[args.size() + 1][];
178-
179-
commandArgs[0] = key;
180-
int targetIndex = 1;
181-
182-
for (byte[] binaryArgument : args) {
183-
commandArgs[targetIndex++] = binaryArgument;
184-
}
185-
186-
return (T) execute(command, commandArgs);
187-
}
188167
}

src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
* @author Dennis Neufeld
6666
* @since 2.0
6767
*/
68-
public interface DefaultedRedisConnection extends RedisConnection {
68+
@Deprecated
69+
public interface DefaultedRedisConnection extends RedisCommands, RedisCommandsProvider {
6970

7071
// KEY COMMANDS
7172

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.connection;
17+
18+
/**
19+
* Provides access to {@link RedisClusterCommands} and the segregated command interfaces.
20+
*
21+
* @author Mark Paluch
22+
* @since 3.0
23+
*/
24+
public interface RedisClusterCommandsProvider extends RedisCommandsProvider {
25+
26+
/**
27+
* Get {@link RedisGeoCommands}.
28+
*
29+
* @return never {@literal null}.
30+
* @since 2.0
31+
*/
32+
RedisClusterCommands clusterCommands();
33+
34+
/**
35+
* Get {@link RedisServerCommands}.
36+
*
37+
* @return never {@literal null}.
38+
* @since 2.0
39+
*/
40+
RedisClusterServerCommands serverCommands();
41+
42+
}

src/main/java/org/springframework/data/redis/connection/RedisClusterConnection.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.data.redis.core.Cursor;
2222
import org.springframework.data.redis.core.ScanOptions;
2323
import org.springframework.lang.Nullable;
24+
import org.springframework.util.Assert;
2425

2526
/**
2627
* {@link RedisClusterConnection} allows sending commands to dedicated nodes within the cluster. A
@@ -32,7 +33,8 @@
3233
* @author Mark Paluch
3334
* @since 1.7
3435
*/
35-
public interface RedisClusterConnection extends RedisConnection, RedisClusterCommands, RedisClusterServerCommands {
36+
public interface RedisClusterConnection
37+
extends RedisConnection, DefaultedRedisClusterConnection, RedisClusterCommandsProvider {
3638

3739
/**
3840
* @param node must not be {@literal null}.
@@ -89,15 +91,22 @@ public interface RedisClusterConnection extends RedisConnection, RedisClusterCom
8991
* @since 2.1
9092
*/
9193
@Nullable
92-
<T> T execute(String command, byte[] key, Collection<byte[]> args);
94+
default <T> T execute(String command, byte[] key, Collection<byte[]> args) {
9395

94-
/**
95-
* Get {@link RedisClusterServerCommands}.
96-
*
97-
* @return never {@literal null}.
98-
* @since 2.0
99-
*/
100-
default RedisClusterServerCommands serverCommands() {
101-
return this;
96+
Assert.notNull(command, "Command must not be null!");
97+
Assert.notNull(key, "Key must not be null!");
98+
Assert.notNull(args, "Args must not be null!");
99+
100+
byte[][] commandArgs = new byte[args.size() + 1][];
101+
102+
commandArgs[0] = key;
103+
int targetIndex = 1;
104+
105+
for (byte[] binaryArgument : args) {
106+
commandArgs[targetIndex++] = binaryArgument;
107+
}
108+
109+
return (T) execute(command, commandArgs);
102110
}
111+
103112
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.connection;
17+
18+
/**
19+
* Provides access to {@link RedisCommands} and the segregated command interfaces.
20+
*
21+
* @author Mark Paluch
22+
* @since 3.0
23+
*/
24+
public interface RedisCommandsProvider {
25+
26+
/**
27+
* Get {@link RedisCommands}.
28+
*
29+
* @return never {@literal null}.
30+
* @since 3.0
31+
*/
32+
RedisCommands commands();
33+
34+
/**
35+
* Get {@link RedisGeoCommands}.
36+
*
37+
* @return never {@literal null}.
38+
* @since 2.0
39+
*/
40+
RedisGeoCommands geoCommands();
41+
42+
/**
43+
* Get {@link RedisHashCommands}.
44+
*
45+
* @return never {@literal null}.
46+
* @since 2.0
47+
*/
48+
RedisHashCommands hashCommands();
49+
50+
/**
51+
* Get {@link RedisHyperLogLogCommands}.
52+
*
53+
* @return never {@literal null}.
54+
* @since 2.0
55+
*/
56+
RedisHyperLogLogCommands hyperLogLogCommands();
57+
58+
/**
59+
* Get {@link RedisKeyCommands}.
60+
*
61+
* @return never {@literal null}.
62+
* @since 2.0
63+
*/
64+
RedisKeyCommands keyCommands();
65+
66+
/**
67+
* Get {@link RedisListCommands}.
68+
*
69+
* @return never {@literal null}.
70+
* @since 2.0
71+
*/
72+
RedisListCommands listCommands();
73+
74+
/**
75+
* Get {@link RedisSetCommands}.
76+
*
77+
* @return never {@literal null}.
78+
* @since 2.0
79+
*/
80+
RedisSetCommands setCommands();
81+
82+
/**
83+
* Get {@link RedisScriptingCommands}.
84+
*
85+
* @return never {@literal null}.
86+
* @since 2.0
87+
*/
88+
RedisScriptingCommands scriptingCommands();
89+
90+
/**
91+
* Get {@link RedisServerCommands}.
92+
*
93+
* @return never {@literal null}.
94+
* @since 2.0
95+
*/
96+
RedisServerCommands serverCommands();
97+
98+
/**
99+
* Get {@link RedisStreamCommands}.
100+
*
101+
* @return never {@literal null}.
102+
* @since 2.2
103+
*/
104+
RedisStreamCommands streamCommands();
105+
106+
/**
107+
* Get {@link RedisStringCommands}.
108+
*
109+
* @return never {@literal null}.
110+
* @since 2.0
111+
*/
112+
RedisStringCommands stringCommands();
113+
114+
/**
115+
* Get {@link RedisZSetCommands}.
116+
*
117+
* @return never {@literal null}.
118+
* @since 2.0
119+
*/
120+
RedisZSetCommands zSetCommands();
121+
}

0 commit comments

Comments
 (0)