diff --git a/src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java index 83a58bcbe..5c9a07a56 100644 --- a/src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java @@ -31,7 +31,7 @@ import org.springframework.util.Assert; * @author Mark Paluch * @since 1.4 */ -public abstract class AbstractRedisConnection implements DefaultedRedisConnection { +public abstract class AbstractRedisConnection implements RedisConnection { private @Nullable RedisSentinelConfiguration sentinelConfiguration; private final Map connectionCache = new ConcurrentHashMap<>(); diff --git a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java index 5ebdf8283..69d608c3e 100644 --- a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java @@ -80,6 +80,7 @@ import org.springframework.util.ObjectUtils; * @author ihaohong * @author Dennis Neufeld */ +@SuppressWarnings({ "ConstantConditions", "deprecation" }) public class DefaultStringRedisConnection implements StringRedisConnection, DecoratedRedisConnection { private static final byte[][] EMPTY_2D_BYTE_ARRAY = new byte[0][]; @@ -184,6 +185,66 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco this.byteGeoResultsToStringGeoResults = Converters.deserializingGeoResultsConverter(serializer); } + @Override + public RedisCommands commands() { + return this; + } + + @Override + public RedisGeoCommands geoCommands() { + return this; + } + + @Override + public RedisHashCommands hashCommands() { + return this; + } + + @Override + public RedisHyperLogLogCommands hyperLogLogCommands() { + return this; + } + + @Override + public RedisKeyCommands keyCommands() { + return this; + } + + @Override + public RedisListCommands listCommands() { + return this; + } + + @Override + public RedisSetCommands setCommands() { + return this; + } + + @Override + public RedisScriptingCommands scriptingCommands() { + return this; + } + + @Override + public RedisServerCommands serverCommands() { + return this; + } + + @Override + public RedisStreamCommands streamCommands() { + return this; + } + + @Override + public RedisStringCommands stringCommands() { + return this; + } + + @Override + public RedisZSetCommands zSetCommands() { + return this; + } + @Override public Long append(byte[] key, byte[] value) { return convertAndReturn(delegate.append(key, value), Converters.identityConverter()); @@ -1011,6 +1072,13 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco return convertAndReturn(delegate.zInterWithScores(aggregate, weights, serializeMulti(sets)), tupleToStringTuple); } + @Nullable + @Override + public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) { + return convertAndReturn(delegate.zInterStore(destKey, aggregate, Weights.of(weights), sets), + Converters.identityConverter()); + } + @Override public Long zInterStore(byte[] destKey, Aggregate aggregate, Weights weights, byte[]... sets) { return convertAndReturn(delegate.zInterStore(destKey, aggregate, weights, sets), Converters.identityConverter()); @@ -1211,6 +1279,13 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco return convertAndReturn(delegate.zUnionStore(destKey, aggregate, weights, sets), Converters.identityConverter()); } + @Nullable + @Override + public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) { + return convertAndReturn(delegate.zUnionStore(destKey, aggregate, Weights.of(weights), sets), + Converters.identityConverter()); + } + public Long zUnionStore(byte[] destKey, byte[]... sets) { return convertAndReturn(delegate.zUnionStore(destKey, sets), Converters.identityConverter()); } @@ -1303,7 +1378,6 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco : (GeoReference) data; } - @SuppressWarnings("unchecked") private StreamOffset[] serialize(StreamOffset[] offsets) { return Arrays.stream(offsets).map(it -> StreamOffset.create(serialize(it.getKey()), it.getOffset())) diff --git a/src/main/java/org/springframework/data/redis/connection/DefaultedRedisClusterConnection.java b/src/main/java/org/springframework/data/redis/connection/DefaultedRedisClusterConnection.java index 4fba9aa18..8479fbb06 100644 --- a/src/main/java/org/springframework/data/redis/connection/DefaultedRedisClusterConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/DefaultedRedisClusterConnection.java @@ -15,14 +15,11 @@ */ package org.springframework.data.redis.connection; -import java.util.Collection; import java.util.List; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.springframework.data.redis.core.types.RedisClientInfo; -import org.springframework.lang.Nullable; -import org.springframework.util.Assert; /** * @author Christoph Strobl @@ -30,7 +27,9 @@ import org.springframework.util.Assert; * @author Dennis Neufeld * @since 2.0 */ -public interface DefaultedRedisClusterConnection extends RedisClusterConnection, DefaultedRedisConnection { +@Deprecated +public interface DefaultedRedisClusterConnection + extends DefaultedRedisConnection, RedisClusterCommands, RedisClusterServerCommands, RedisClusterCommandsProvider { /** @deprecated in favor of {@link RedisConnection#serverCommands()}. */ @Override @@ -165,24 +164,4 @@ public interface DefaultedRedisClusterConnection extends RedisClusterConnection, return serverCommands().getClientList(node); } - @Nullable - @Override - @SuppressWarnings("unchecked") - default T execute(String command, byte[] key, Collection args) { - - Assert.notNull(command, "Command must not be null!"); - Assert.notNull(key, "Key must not be null!"); - Assert.notNull(args, "Args must not be null!"); - - byte[][] commandArgs = new byte[args.size() + 1][]; - - commandArgs[0] = key; - int targetIndex = 1; - - for (byte[] binaryArgument : args) { - commandArgs[targetIndex++] = binaryArgument; - } - - return (T) execute(command, commandArgs); - } } diff --git a/src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java index 71da3804f..801d4b19f 100644 --- a/src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java @@ -65,7 +65,8 @@ import org.springframework.lang.Nullable; * @author Dennis Neufeld * @since 2.0 */ -public interface DefaultedRedisConnection extends RedisConnection { +@Deprecated +public interface DefaultedRedisConnection extends RedisCommands, RedisCommandsProvider { // KEY COMMANDS diff --git a/src/main/java/org/springframework/data/redis/connection/RedisClusterCommandsProvider.java b/src/main/java/org/springframework/data/redis/connection/RedisClusterCommandsProvider.java new file mode 100644 index 000000000..85c36a76d --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/RedisClusterCommandsProvider.java @@ -0,0 +1,42 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection; + +/** + * Provides access to {@link RedisClusterCommands} and the segregated command interfaces. + * + * @author Mark Paluch + * @since 3.0 + */ +public interface RedisClusterCommandsProvider extends RedisCommandsProvider { + + /** + * Get {@link RedisGeoCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisClusterCommands clusterCommands(); + + /** + * Get {@link RedisServerCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisClusterServerCommands serverCommands(); + +} diff --git a/src/main/java/org/springframework/data/redis/connection/RedisClusterConnection.java b/src/main/java/org/springframework/data/redis/connection/RedisClusterConnection.java index 940456b88..a361f7ffa 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisClusterConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisClusterConnection.java @@ -21,6 +21,7 @@ import java.util.Set; import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.ScanOptions; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; /** * {@link RedisClusterConnection} allows sending commands to dedicated nodes within the cluster. A @@ -32,7 +33,8 @@ import org.springframework.lang.Nullable; * @author Mark Paluch * @since 1.7 */ -public interface RedisClusterConnection extends RedisConnection, RedisClusterCommands, RedisClusterServerCommands { +public interface RedisClusterConnection + extends RedisConnection, DefaultedRedisClusterConnection, RedisClusterCommandsProvider { /** * @param node must not be {@literal null}. @@ -89,15 +91,22 @@ public interface RedisClusterConnection extends RedisConnection, RedisClusterCom * @since 2.1 */ @Nullable - T execute(String command, byte[] key, Collection args); + default T execute(String command, byte[] key, Collection args) { - /** - * Get {@link RedisClusterServerCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisClusterServerCommands serverCommands() { - return this; + Assert.notNull(command, "Command must not be null!"); + Assert.notNull(key, "Key must not be null!"); + Assert.notNull(args, "Args must not be null!"); + + byte[][] commandArgs = new byte[args.size() + 1][]; + + commandArgs[0] = key; + int targetIndex = 1; + + for (byte[] binaryArgument : args) { + commandArgs[targetIndex++] = binaryArgument; + } + + return (T) execute(command, commandArgs); } + } diff --git a/src/main/java/org/springframework/data/redis/connection/RedisCommandsProvider.java b/src/main/java/org/springframework/data/redis/connection/RedisCommandsProvider.java new file mode 100644 index 000000000..74e63da95 --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/RedisCommandsProvider.java @@ -0,0 +1,121 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection; + +/** + * Provides access to {@link RedisCommands} and the segregated command interfaces. + * + * @author Mark Paluch + * @since 3.0 + */ +public interface RedisCommandsProvider { + + /** + * Get {@link RedisCommands}. + * + * @return never {@literal null}. + * @since 3.0 + */ + RedisCommands commands(); + + /** + * Get {@link RedisGeoCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisGeoCommands geoCommands(); + + /** + * Get {@link RedisHashCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisHashCommands hashCommands(); + + /** + * Get {@link RedisHyperLogLogCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisHyperLogLogCommands hyperLogLogCommands(); + + /** + * Get {@link RedisKeyCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisKeyCommands keyCommands(); + + /** + * Get {@link RedisListCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisListCommands listCommands(); + + /** + * Get {@link RedisSetCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisSetCommands setCommands(); + + /** + * Get {@link RedisScriptingCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisScriptingCommands scriptingCommands(); + + /** + * Get {@link RedisServerCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisServerCommands serverCommands(); + + /** + * Get {@link RedisStreamCommands}. + * + * @return never {@literal null}. + * @since 2.2 + */ + RedisStreamCommands streamCommands(); + + /** + * Get {@link RedisStringCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisStringCommands stringCommands(); + + /** + * Get {@link RedisZSetCommands}. + * + * @return never {@literal null}. + * @since 2.0 + */ + RedisZSetCommands zSetCommands(); +} diff --git a/src/main/java/org/springframework/data/redis/connection/RedisConnection.java b/src/main/java/org/springframework/data/redis/connection/RedisConnection.java index 0a9e9afa9..4603b79e0 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisConnection.java @@ -29,117 +29,7 @@ import org.springframework.dao.DataAccessException; * @author Mark Paluch * @author James Howe */ -public interface RedisConnection extends RedisCommands, AutoCloseable { - - /** - * Get {@link RedisGeoCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisGeoCommands geoCommands() { - return this; - } - - /** - * Get {@link RedisHashCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisHashCommands hashCommands() { - return this; - } - - /** - * Get {@link RedisHyperLogLogCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisHyperLogLogCommands hyperLogLogCommands() { - return this; - } - - /** - * Get {@link RedisKeyCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisKeyCommands keyCommands() { - return this; - } - - /** - * Get {@link RedisListCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisListCommands listCommands() { - return this; - } - - /** - * Get {@link RedisSetCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisSetCommands setCommands() { - return this; - } - - /** - * Get {@link RedisScriptingCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisScriptingCommands scriptingCommands() { - return this; - } - - /** - * Get {@link RedisServerCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisServerCommands serverCommands() { - return this; - } - - /** - * Get {@link RedisStreamCommands}. - * - * @return never {@literal null}. - * @since 2.2 - */ - default RedisStreamCommands streamCommands() { - return this; - } - - /** - * Get {@link RedisStringCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisStringCommands stringCommands() { - return this; - } - - /** - * Get {@link RedisZSetCommands}. - * - * @return never {@literal null}. - * @since 2.0 - */ - default RedisZSetCommands zSetCommands() { - return this; - } +public interface RedisConnection extends RedisCommandsProvider, DefaultedRedisConnection, AutoCloseable { /** * Closes (or quits) the connection. diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java index c77a0030d..6ffcf80c2 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java @@ -73,7 +73,7 @@ import org.springframework.util.Assert; * @author Liming Deng * @since 1.7 */ -public class JedisClusterConnection implements DefaultedRedisClusterConnection { +public class JedisClusterConnection implements RedisClusterConnection { private static final ExceptionTranslationStrategy EXCEPTION_TRANSLATION = new FallbackExceptionTranslationStrategy( JedisExceptionConverter.INSTANCE); @@ -83,6 +83,16 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection { private final Log log = LogFactory.getLog(getClass()); private final JedisCluster cluster; + private final JedisClusterGeoCommands geoCommands = new JedisClusterGeoCommands(this); + private final JedisClusterHashCommands hashCommands = new JedisClusterHashCommands(this); + private final JedisClusterHyperLogLogCommands hllCommands = new JedisClusterHyperLogLogCommands(this); + private final JedisClusterKeyCommands keyCommands = new JedisClusterKeyCommands(this); + private final JedisClusterListCommands listCommands = new JedisClusterListCommands(this); + private final JedisClusterSetCommands setCommands = new JedisClusterSetCommands(this); + private final JedisClusterServerCommands serverCommands = new JedisClusterServerCommands(this); + private final JedisClusterStreamCommands streamCommands = new JedisClusterStreamCommands(this); + private final JedisClusterStringCommands stringCommands = new JedisClusterStringCommands(this); + private final JedisClusterZSetCommands zSetCommands = new JedisClusterZSetCommands(this); private boolean closed; @@ -233,54 +243,64 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection { }, keys).resultsAsList(); } + @Override + public RedisCommands commands() { + return this; + } + + @Override + public RedisClusterCommands clusterCommands() { + return this; + } + @Override public RedisGeoCommands geoCommands() { - return new JedisClusterGeoCommands(this); + return geoCommands; } @Override public RedisHashCommands hashCommands() { - return new JedisClusterHashCommands(this); + return hashCommands; } @Override public RedisHyperLogLogCommands hyperLogLogCommands() { - return new JedisClusterHyperLogLogCommands(this); + return hllCommands; } @Override public RedisKeyCommands keyCommands() { - return doGetKeyCommands(); - } - - @Override - public RedisStringCommands stringCommands() { - return new JedisClusterStringCommands(this); + return keyCommands; } @Override public RedisListCommands listCommands() { - return new JedisClusterListCommands(this); + return listCommands; } @Override public RedisSetCommands setCommands() { - return new JedisClusterSetCommands(this); - } - - @Override - public RedisStreamCommands streamCommands() { - return new JedisClusterStreamCommands(this); - } - - @Override - public RedisZSetCommands zSetCommands() { - return new JedisClusterZSetCommands(this); + return setCommands; } @Override public RedisClusterServerCommands serverCommands() { - return new JedisClusterServerCommands(this); + return serverCommands; + } + + @Override + public RedisStreamCommands streamCommands() { + return streamCommands; + } + + @Override + public RedisStringCommands stringCommands() { + return stringCommands; + } + + @Override + public RedisZSetCommands zSetCommands() { + return zSetCommands; } @Override @@ -288,23 +308,19 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection { return new JedisClusterScriptingCommands(this); } - private JedisClusterKeyCommands doGetKeyCommands() { - return new JedisClusterKeyCommands(this); - } - @Override public Set keys(RedisClusterNode node, byte[] pattern) { - return doGetKeyCommands().keys(node, pattern); + return keyCommands.keys(node, pattern); } @Override public Cursor scan(RedisClusterNode node, ScanOptions options) { - return doGetKeyCommands().scan(node, options); + return keyCommands.scan(node, options); } @Override public byte[] randomKey(RedisClusterNode node) { - return doGetKeyCommands().randomKey(node); + return keyCommands.randomKey(node); } @Override @@ -452,12 +468,13 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection { RedisClusterNode node = clusterGetNodeForSlot(slot); - clusterCommandExecutor + NodeResult> result = clusterCommandExecutor .executeCommandOnSingleNode( (JedisClusterCommandCallback>) client -> JedisConverters.stringListToByteList() .convert(client.clusterGetKeysInSlot(slot, count != null ? count.intValue() : Integer.MAX_VALUE)), node); - return null; + + return result.getValue(); } @Override @@ -482,7 +499,6 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection { return clusterCommandExecutor.executeCommandOnSingleNode( (JedisClusterCommandCallback) client -> client.clusterCountKeysInSlot(slot), node).getValue(); - } @Override diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java index 92fc89c68..de9a22de0 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java @@ -70,11 +70,24 @@ public class JedisConnection extends AbstractRedisConnection { private final JedisInvoker statusInvoker = new JedisInvoker((directFunction, pipelineFunction, converter, nullDefault) -> doInvoke(true, directFunction, pipelineFunction, converter, nullDefault)); + private final JedisGeoCommands geoCommands = new JedisGeoCommands(this); + private final JedisHashCommands hashCommands = new JedisHashCommands(this); + private final JedisHyperLogLogCommands hllCommands = new JedisHyperLogLogCommands(this); + private final JedisKeyCommands keyCommands = new JedisKeyCommands(this); + private final JedisListCommands listCommands = new JedisListCommands(this); + private final JedisScriptingCommands scriptingCommands = new JedisScriptingCommands(this); + private final JedisServerCommands serverCommands = new JedisServerCommands(this); + private final JedisSetCommands setCommands = new JedisSetCommands(this); + private final JedisStreamCommands streamCommands = new JedisStreamCommands(this); + private final JedisStringCommands stringCommands = new JedisStringCommands(this); + private final JedisZSetCommands zSetCommands = new JedisZSetCommands(this); + private final @Nullable Pool pool; private final String clientName; private final JedisClientConfig nodeConfig; private final JedisClientConfig sentinelConfig; + private List pipelinedResults = new ArrayList<>(); private Queue>> txResults = new LinkedList<>(); @@ -189,58 +202,63 @@ public class JedisConnection extends AbstractRedisConnection { } @Override - public RedisKeyCommands keyCommands() { - return new JedisKeyCommands(this); - } - - @Override - public RedisStreamCommands streamCommands() { - return new JedisStreamCommands(this); - } - - @Override - public RedisStringCommands stringCommands() { - return new JedisStringCommands(this); - } - - @Override - public RedisListCommands listCommands() { - return new JedisListCommands(this); - } - - @Override - public RedisSetCommands setCommands() { - return new JedisSetCommands(this); - } - - @Override - public RedisZSetCommands zSetCommands() { - return new JedisZSetCommands(this); - } - - @Override - public RedisHashCommands hashCommands() { - return new JedisHashCommands(this); + public RedisCommands commands() { + return this; } @Override public RedisGeoCommands geoCommands() { - return new JedisGeoCommands(this); + return geoCommands; } @Override - public RedisScriptingCommands scriptingCommands() { - return new JedisScriptingCommands(this); - } - - @Override - public RedisServerCommands serverCommands() { - return new JedisServerCommands(this); + public RedisHashCommands hashCommands() { + return hashCommands; } @Override public RedisHyperLogLogCommands hyperLogLogCommands() { - return new JedisHyperLogLogCommands(this); + return hllCommands; + } + + @Override + public RedisKeyCommands keyCommands() { + return keyCommands; + } + + @Override + public RedisListCommands listCommands() { + return listCommands; + } + + @Override + public RedisSetCommands setCommands() { + return setCommands; + } + + @Override + public RedisStreamCommands streamCommands() { + return streamCommands; + } + + @Override + public RedisStringCommands stringCommands() { + return stringCommands; + } + + @Override + public RedisZSetCommands zSetCommands() { + return zSetCommands; + } + + @Override + public RedisScriptingCommands scriptingCommands() { + return scriptingCommands; + } + + @Override + public RedisServerCommands serverCommands() { + return serverCommands; } @Override diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnection.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnection.java index eb858bd37..0323c9219 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnection.java @@ -58,12 +58,22 @@ import org.springframework.util.ObjectUtils; * @author Mark Paluch * @since 1.7 */ -public class LettuceClusterConnection extends LettuceConnection implements DefaultedRedisClusterConnection { +public class LettuceClusterConnection extends LettuceConnection + implements RedisClusterConnection, DefaultedRedisClusterConnection { static final ExceptionTranslationStrategy exceptionConverter = new PassThroughExceptionTranslationStrategy( LettuceExceptionConverter.INSTANCE); private final Log log = LogFactory.getLog(getClass()); + private final LettuceClusterGeoCommands geoCommands = new LettuceClusterGeoCommands(this); + private final LettuceClusterHashCommands hashCommands = new LettuceClusterHashCommands(this); + private final LettuceClusterHyperLogLogCommands hllCommands = new LettuceClusterHyperLogLogCommands(this); + private final LettuceClusterKeyCommands keyCommands = new LettuceClusterKeyCommands(this); + private final LettuceClusterListCommands listCommands = new LettuceClusterListCommands(this); + private final LettuceClusterStringCommands stringCommands = new LettuceClusterStringCommands(this); + private final LettuceClusterSetCommands setCommands = new LettuceClusterSetCommands(this); + private final LettuceClusterZSetCommands zSetCommands = new LettuceClusterZSetCommands(this); + private final LettuceClusterServerCommands serverCommands = new LettuceClusterServerCommands(this); private ClusterCommandExecutor clusterCommandExecutor; private ClusterTopologyProvider topologyProvider; @@ -198,53 +208,54 @@ public class LettuceClusterConnection extends LettuceConnection implements Defau connectionProvider.getClass().getName())); } + @Override + public org.springframework.data.redis.connection.RedisClusterCommands clusterCommands() { + return this; + } + @Override public RedisGeoCommands geoCommands() { - return new LettuceClusterGeoCommands(this); + return geoCommands; } @Override public RedisHashCommands hashCommands() { - return new LettuceClusterHashCommands(this); + return hashCommands; } @Override public RedisHyperLogLogCommands hyperLogLogCommands() { - return new LettuceClusterHyperLogLogCommands(this); + return hllCommands; } @Override public RedisKeyCommands keyCommands() { - return doGetClusterKeyCommands(); - } - - private LettuceClusterKeyCommands doGetClusterKeyCommands() { - return new LettuceClusterKeyCommands(this); + return keyCommands; } @Override public RedisListCommands listCommands() { - return new LettuceClusterListCommands(this); - } - - @Override - public RedisStringCommands stringCommands() { - return new LettuceClusterStringCommands(this); + return listCommands; } @Override public RedisSetCommands setCommands() { - return new LettuceClusterSetCommands(this); - } - - @Override - public RedisZSetCommands zSetCommands() { - return new LettuceClusterZSetCommands(this); + return setCommands; } @Override public RedisClusterServerCommands serverCommands() { - return new LettuceClusterServerCommands(this); + return serverCommands; + } + + @Override + public RedisStringCommands stringCommands() { + return stringCommands; + } + + @Override + public RedisZSetCommands zSetCommands() { + return zSetCommands; } @Override @@ -438,16 +449,16 @@ public class LettuceClusterConnection extends LettuceConnection implements Defau @Override public Set keys(RedisClusterNode node, byte[] pattern) { - return doGetClusterKeyCommands().keys(node, pattern); + return new LettuceClusterKeyCommands(this).keys(node, pattern); } @Override public Cursor scan(RedisClusterNode node, ScanOptions options) { - return doGetClusterKeyCommands().scan(node, options); + return new LettuceClusterKeyCommands(this).scan(node, options); } public byte[] randomKey(RedisClusterNode node) { - return doGetClusterKeyCommands().randomKey(node); + return new LettuceClusterKeyCommands(this).randomKey(node); } @Override diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java index b34c95933..a40de2718 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java @@ -95,6 +95,18 @@ public class LettuceConnection extends AbstractRedisConnection { LettuceExceptionConverter.INSTANCE); private static final TypeHints typeHints = new TypeHints(); + private final LettuceGeoCommands geoCommands = new LettuceGeoCommands(this); + private final LettuceHashCommands hashCommands = new LettuceHashCommands(this); + private final LettuceHyperLogLogCommands hllCommands = new LettuceHyperLogLogCommands(this); + private final LettuceKeyCommands keyCommands = new LettuceKeyCommands(this); + private final LettuceListCommands listCommands = new LettuceListCommands(this); + private final LettuceScriptingCommands scriptingCommands = new LettuceScriptingCommands(this); + private final LettuceSetCommands setCommands = new LettuceSetCommands(this); + private final LettuceServerCommands serverCommands = new LettuceServerCommands(this); + private final LettuceStreamCommands streamCommands = new LettuceStreamCommands(this); + private final LettuceStringCommands stringCommands = new LettuceStringCommands(this); + private final LettuceZSetCommands zSetCommands = new LettuceZSetCommands(this); + private final int defaultDbIndex; private int dbIndex; @@ -231,59 +243,64 @@ public class LettuceConnection extends AbstractRedisConnection { return EXCEPTION_TRANSLATION.translate(ex); } + @Override + public org.springframework.data.redis.connection.RedisCommands commands() { + return this; + } + @Override public RedisGeoCommands geoCommands() { - return new LettuceGeoCommands(this); + return geoCommands; } @Override public RedisHashCommands hashCommands() { - return new LettuceHashCommands(this); + return hashCommands; } @Override public RedisHyperLogLogCommands hyperLogLogCommands() { - return new LettuceHyperLogLogCommands(this); + return hllCommands; } @Override public RedisKeyCommands keyCommands() { - return new LettuceKeyCommands(this); + return keyCommands; } @Override public RedisListCommands listCommands() { - return new LettuceListCommands(this); - } - - @Override - public RedisSetCommands setCommands() { - return new LettuceSetCommands(this); + return listCommands; } @Override public RedisScriptingCommands scriptingCommands() { - return new LettuceScriptingCommands(this); + return scriptingCommands; } @Override - public RedisStreamCommands streamCommands() { - return new LettuceStreamCommands(this); - } - - @Override - public RedisStringCommands stringCommands() { - return new LettuceStringCommands(this); + public RedisSetCommands setCommands() { + return setCommands; } @Override public RedisServerCommands serverCommands() { - return new LettuceServerCommands(this); + return serverCommands; + } + + @Override + public RedisStreamCommands streamCommands() { + return streamCommands; + } + + @Override + public RedisStringCommands stringCommands() { + return stringCommands; } @Override public RedisZSetCommands zSetCommands() { - return new LettuceZSetCommands(this); + return zSetCommands; } @Override diff --git a/src/test/java/org/springframework/data/redis/connection/RedisConnectionUnitTests.java b/src/test/java/org/springframework/data/redis/connection/RedisConnectionUnitTests.java index b31c3d954..0036902e8 100644 --- a/src/test/java/org/springframework/data/redis/connection/RedisConnectionUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/RedisConnectionUnitTests.java @@ -101,6 +101,66 @@ class RedisConnectionUnitTests { this.delegate = delegate; } + @Override + public RedisCommands commands() { + return null; + } + + @Override + public RedisGeoCommands geoCommands() { + return null; + } + + @Override + public RedisHashCommands hashCommands() { + return null; + } + + @Override + public RedisHyperLogLogCommands hyperLogLogCommands() { + return null; + } + + @Override + public RedisKeyCommands keyCommands() { + return null; + } + + @Override + public RedisListCommands listCommands() { + return null; + } + + @Override + public RedisSetCommands setCommands() { + return null; + } + + @Override + public RedisScriptingCommands scriptingCommands() { + return null; + } + + @Override + public RedisServerCommands serverCommands() { + return null; + } + + @Override + public RedisStreamCommands streamCommands() { + return null; + } + + @Override + public RedisStringCommands stringCommands() { + return null; + } + + @Override + public RedisZSetCommands zSetCommands() { + return null; + } + @Override protected boolean isActive(RedisNode node) { return ObjectUtils.nullSafeEquals(activeNode, node);