Skip to content

Commit bea3a2b

Browse files
author
codeba
committed
refactor(redis-keeper-spring): refactor CacheTemplateProxy
1 parent 4a9366f commit bea3a2b

File tree

1 file changed

+7
-64
lines changed

1 file changed

+7
-64
lines changed

redis-keeper-spring/src/main/java/org/codeba/redis/keeper/spring/CacheTemplateProxy.java

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,12 @@
2222
import org.codeba.redis.keeper.core.CacheTemplateProvider;
2323
import org.codeba.redis.keeper.core.Provider;
2424

25-
import java.lang.reflect.InvocationHandler;
26-
import java.lang.reflect.Method;
27-
import java.lang.reflect.Proxy;
28-
import java.util.Optional;
29-
3025
/**
3126
* The type Cache template proxy.
3227
*
3328
* @author codeba
3429
*/
35-
public class CacheTemplateProxy implements InvocationHandler {
36-
37-
/**
38-
* The Datasource.
39-
*/
40-
private String datasource;
41-
42-
private CacheDatasourceStatus status;
43-
44-
/**
45-
* The Template provider.
46-
*/
47-
private CacheTemplateProvider<?> templateProvider;
48-
49-
/**
50-
* Instantiates a new Cache template proxy.
51-
*
52-
* @param datasource the datasource
53-
*/
54-
public CacheTemplateProxy(String datasource) {
55-
this.datasource = datasource;
56-
}
57-
58-
/**
59-
* Instantiates a new Cache template proxy.
60-
*
61-
* @param datasource the datasource
62-
* @param status the status
63-
*/
64-
public CacheTemplateProxy(String datasource, CacheDatasourceStatus status) {
65-
this.datasource = datasource;
66-
this.status = status;
67-
}
30+
public class CacheTemplateProxy {
6831

6932
/**
7033
* As template cache template.
@@ -96,10 +59,7 @@ public static CacheTemplate asTemplate(final String datasource, final CacheDatas
9659
* @return the t
9760
*/
9861
public static <T> T asTemplate(final String datasource, Class<T> templateClass) {
99-
return (T) Proxy.newProxyInstance(
100-
templateClass.getClassLoader(),
101-
new Class[]{templateClass},
102-
new CacheTemplateProxy(datasource));
62+
return (T) getInvokeTemplate(datasource, null);
10363
}
10464

10565
/**
@@ -112,30 +72,13 @@ public static <T> T asTemplate(final String datasource, Class<T> templateClass)
11272
* @return the t
11373
*/
11474
public static <T> T asTemplate(final String datasource, final CacheDatasourceStatus status, Class<T> templateClass) {
115-
return (T) Proxy.newProxyInstance(
116-
templateClass.getClassLoader(),
117-
new Class[]{templateClass},
118-
new CacheTemplateProxy(datasource, status));
75+
return (T) getInvokeTemplate(datasource, status);
11976
}
12077

121-
@Override
122-
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
123-
final String key = Provider.keyWithStatus(this.datasource, this.status);
124-
final Optional<?> template = getTemplateProvider().getTemplate(key);
125-
return method.invoke(template.get(), args);
78+
private static Object getInvokeTemplate(final String datasource, final CacheDatasourceStatus status) {
79+
final CacheTemplateProvider provider = ApplicationContextUtil.getBean(CacheTemplateProvider.class);
80+
final String key = Provider.keyWithStatus(datasource, status);
81+
return provider.getTemplate(key).get();
12682
}
12783

128-
/**
129-
* Gets template provider.
130-
*
131-
* @return the template provider
132-
*/
133-
private CacheTemplateProvider<?> getTemplateProvider() {
134-
if (this.templateProvider == null) {
135-
templateProvider = ApplicationContextUtil.getBean(CacheTemplateProvider.class);
136-
}
137-
return this.templateProvider;
138-
}
139-
140-
14184
}

0 commit comments

Comments
 (0)