Polishing.

Avoid duplicate method lookup by keeping reference to Method.
Add missing native image hints for command proxies.

Original Pull Request: #2887
This commit is contained in:
Christoph Strobl
2024-04-10 14:24:50 +02:00
parent 8a305be790
commit db95caee5a
2 changed files with 31 additions and 8 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFa
import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.*; import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.core.*; import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.RedisConnectionUtils.RedisConnectionProxy;
import org.springframework.data.redis.core.convert.KeyspaceConfiguration; import org.springframework.data.redis.core.convert.KeyspaceConfiguration;
import org.springframework.data.redis.core.convert.MappingConfiguration; import org.springframework.data.redis.core.convert.MappingConfiguration;
import org.springframework.data.redis.core.convert.MappingRedisConverter; import org.springframework.data.redis.core.convert.MappingRedisConverter;
@@ -157,6 +158,20 @@ public class RedisRuntimeHints implements RuntimeHintsRegistrar {
boundOperationsProxy(BoundStreamOperations.class, classLoader, hints); boundOperationsProxy(BoundStreamOperations.class, classLoader, hints);
boundOperationsProxy(BoundValueOperations.class, classLoader, hints); boundOperationsProxy(BoundValueOperations.class, classLoader, hints);
boundOperationsProxy(BoundZSetOperations.class, classLoader, hints); boundOperationsProxy(BoundZSetOperations.class, classLoader, hints);
// Connection Splitting
registerRedisConnectionProxy(TypeReference.of(RedisCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisGeoCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisHashCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisHyperLogLogCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisKeyCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisListCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisSetCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisScriptingCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisServerCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisStreamCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisStringCommands.class), hints);
registerRedisConnectionProxy(TypeReference.of(RedisZSetCommands.class), hints);
} }
static void boundOperationsProxy(Class<?> type, ClassLoader classLoader, RuntimeHints hints) { static void boundOperationsProxy(Class<?> type, ClassLoader classLoader, RuntimeHints hints) {
@@ -179,4 +194,13 @@ public class RedisRuntimeHints implements RuntimeHintsRegistrar {
TypeReference.of("org.springframework.aop.framework.Advised"), // TypeReference.of("org.springframework.aop.framework.Advised"), //
TypeReference.of("org.springframework.core.DecoratingProxy")); TypeReference.of("org.springframework.core.DecoratingProxy"));
} }
static void registerRedisConnectionProxy(TypeReference typeReference, RuntimeHints hints) {
hints.proxies().registerJdkProxy(TypeReference.of(RedisConnectionProxy.class), //
typeReference, //
TypeReference.of("org.springframework.aop.SpringProxy"), //
TypeReference.of("org.springframework.aop.framework.Advised"), //
TypeReference.of("org.springframework.core.DecoratingProxy"));
}
} }

View File

@@ -33,7 +33,6 @@ import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/** /**
* Helper class that provides static methods for obtaining {@link RedisConnection} from a * Helper class that provides static methods for obtaining {@link RedisConnection} from a
@@ -451,16 +450,16 @@ public abstract class RedisConnectionUtils {
static class ConnectionSplittingInterceptor implements MethodInterceptor { static class ConnectionSplittingInterceptor implements MethodInterceptor {
private final RedisConnectionFactory factory; private final RedisConnectionFactory factory;
private final @Nullable String commandInterface; private final @Nullable Method commandInterfaceMethod;
public ConnectionSplittingInterceptor(RedisConnectionFactory factory) { public ConnectionSplittingInterceptor(RedisConnectionFactory factory) {
this.factory = factory; this.factory = factory;
this.commandInterface = null; this.commandInterfaceMethod = null;
} }
public ConnectionSplittingInterceptor(RedisConnectionFactory factory, String commandInterface) { private ConnectionSplittingInterceptor(RedisConnectionFactory factory, Method commandInterfaceMethod) {
this.factory = factory; this.factory = factory;
this.commandInterface = commandInterface; this.commandInterfaceMethod = commandInterfaceMethod;
} }
@Override @Override
@@ -484,7 +483,7 @@ public abstract class RedisConnectionUtils {
ProxyFactory proxyFactory = new ProxyFactory(ReflectionUtils.invokeMethod(method, obj)); ProxyFactory proxyFactory = new ProxyFactory(ReflectionUtils.invokeMethod(method, obj));
proxyFactory.addAdvice(new ConnectionSplittingInterceptor(factory, method.getName())); proxyFactory.addAdvice(new ConnectionSplittingInterceptor(factory, method));
proxyFactory.addInterface(RedisConnectionProxy.class); proxyFactory.addInterface(RedisConnectionProxy.class);
proxyFactory.addInterface(returnType); proxyFactory.addInterface(returnType);
@@ -510,8 +509,8 @@ public abstract class RedisConnectionUtils {
Object target = connection; Object target = connection;
try { try {
if (StringUtils.hasText(commandInterface)) { if (commandInterfaceMethod != null) {
target = ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(RedisConnection.class, commandInterface), target = ReflectionUtils.invokeMethod(commandInterfaceMethod,
connection); connection);
} }