Skip to content

Commit db95cae

Browse files
Polishing.
Avoid duplicate method lookup by keeping reference to Method. Add missing native image hints for command proxies. Original Pull Request: #2887
1 parent 8a305be commit db95cae

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/main/java/org/springframework/data/redis/aot/RedisRuntimeHints.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.redis.cache.RedisCacheManager;
3636
import org.springframework.data.redis.connection.*;
3737
import org.springframework.data.redis.core.*;
38+
import org.springframework.data.redis.core.RedisConnectionUtils.RedisConnectionProxy;
3839
import org.springframework.data.redis.core.convert.KeyspaceConfiguration;
3940
import org.springframework.data.redis.core.convert.MappingConfiguration;
4041
import org.springframework.data.redis.core.convert.MappingRedisConverter;
@@ -157,6 +158,20 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
157158
boundOperationsProxy(BoundStreamOperations.class, classLoader, hints);
158159
boundOperationsProxy(BoundValueOperations.class, classLoader, hints);
159160
boundOperationsProxy(BoundZSetOperations.class, classLoader, hints);
161+
162+
// Connection Splitting
163+
registerRedisConnectionProxy(TypeReference.of(RedisCommands.class), hints);
164+
registerRedisConnectionProxy(TypeReference.of(RedisGeoCommands.class), hints);
165+
registerRedisConnectionProxy(TypeReference.of(RedisHashCommands.class), hints);
166+
registerRedisConnectionProxy(TypeReference.of(RedisHyperLogLogCommands.class), hints);
167+
registerRedisConnectionProxy(TypeReference.of(RedisKeyCommands.class), hints);
168+
registerRedisConnectionProxy(TypeReference.of(RedisListCommands.class), hints);
169+
registerRedisConnectionProxy(TypeReference.of(RedisSetCommands.class), hints);
170+
registerRedisConnectionProxy(TypeReference.of(RedisScriptingCommands.class), hints);
171+
registerRedisConnectionProxy(TypeReference.of(RedisServerCommands.class), hints);
172+
registerRedisConnectionProxy(TypeReference.of(RedisStreamCommands.class), hints);
173+
registerRedisConnectionProxy(TypeReference.of(RedisStringCommands.class), hints);
174+
registerRedisConnectionProxy(TypeReference.of(RedisZSetCommands.class), hints);
160175
}
161176

162177
static void boundOperationsProxy(Class<?> type, ClassLoader classLoader, RuntimeHints hints) {
@@ -179,4 +194,13 @@ static void boundOperationsProxy(TypeReference typeReference, ClassLoader classL
179194
TypeReference.of("org.springframework.aop.framework.Advised"), //
180195
TypeReference.of("org.springframework.core.DecoratingProxy"));
181196
}
197+
198+
static void registerRedisConnectionProxy(TypeReference typeReference, RuntimeHints hints) {
199+
200+
hints.proxies().registerJdkProxy(TypeReference.of(RedisConnectionProxy.class), //
201+
typeReference, //
202+
TypeReference.of("org.springframework.aop.SpringProxy"), //
203+
TypeReference.of("org.springframework.aop.framework.Advised"), //
204+
TypeReference.of("org.springframework.core.DecoratingProxy"));
205+
}
182206
}

src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.transaction.support.TransactionSynchronizationManager;
3434
import org.springframework.util.Assert;
3535
import org.springframework.util.ReflectionUtils;
36-
import org.springframework.util.StringUtils;
3736

3837
/**
3938
* Helper class that provides static methods for obtaining {@link RedisConnection} from a
@@ -451,16 +450,16 @@ public void afterCompletion(int status) {
451450
static class ConnectionSplittingInterceptor implements MethodInterceptor {
452451

453452
private final RedisConnectionFactory factory;
454-
private final @Nullable String commandInterface;
453+
private final @Nullable Method commandInterfaceMethod;
455454

456455
public ConnectionSplittingInterceptor(RedisConnectionFactory factory) {
457456
this.factory = factory;
458-
this.commandInterface = null;
457+
this.commandInterfaceMethod = null;
459458
}
460459

461-
public ConnectionSplittingInterceptor(RedisConnectionFactory factory, String commandInterface) {
460+
private ConnectionSplittingInterceptor(RedisConnectionFactory factory, Method commandInterfaceMethod) {
462461
this.factory = factory;
463-
this.commandInterface = commandInterface;
462+
this.commandInterfaceMethod = commandInterfaceMethod;
464463
}
465464

466465
@Override
@@ -484,7 +483,7 @@ public Object intercept(Object obj, Method method, Object[] args) throws Throwab
484483

485484
ProxyFactory proxyFactory = new ProxyFactory(ReflectionUtils.invokeMethod(method, obj));
486485

487-
proxyFactory.addAdvice(new ConnectionSplittingInterceptor(factory, method.getName()));
486+
proxyFactory.addAdvice(new ConnectionSplittingInterceptor(factory, method));
488487
proxyFactory.addInterface(RedisConnectionProxy.class);
489488
proxyFactory.addInterface(returnType);
490489

@@ -510,8 +509,8 @@ public Object intercept(Object obj, Method method, Object[] args) throws Throwab
510509
Object target = connection;
511510
try {
512511

513-
if (StringUtils.hasText(commandInterface)) {
514-
target = ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(RedisConnection.class, commandInterface),
512+
if (commandInterfaceMethod != null) {
513+
target = ReflectionUtils.invokeMethod(commandInterfaceMethod,
515514
connection);
516515
}
517516

0 commit comments

Comments
 (0)