Skip to content

Commit 04c9772

Browse files
committed
optimize query cache
1 parent 6497224 commit 04c9772

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

spring-boot-data-aggregator-autoconfigure/src/main/java/io/github/lvyahui8/spring/annotation/DataProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@
3232
*/
3333
long timeout() default -1;
3434

35+
/**
36+
* The call to this data providing method should be idempotent,
37+
* which determines whether its execution result will be cached.
38+
*/
39+
boolean idempotent() default true;
3540
}

spring-boot-data-aggregator-autoconfigure/src/main/java/io/github/lvyahui8/spring/autoconfigure/BeanAggregateAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ private void dealProvideMethod(DataProviderRepository repository, Method method)
107107
Assert.isTrue(! StringUtils.isEmpty(dataId),"data id must be not null!");
108108
provider.setId(dataId);
109109
provider.setMethod(method);
110+
provider.setIdempotent(beanProvider.idempotent());
110111
provider.setTimeout(beanProvider.timeout() > 0 ? beanProvider.timeout() : properties.getDefaultTimeout());
111112
Parameter[] parameters = provider.getMethod().getParameters();
112113
List<MethodArg> methodArgs = new ArrayList<>(method.getParameterCount());

spring-boot-data-aggregator-core/src/main/java/io/github/lvyahui8/spring/aggregate/model/DataProvideDefinition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ public class DataProvideDefinition {
1717
private List<DataConsumeDefinition> depends;
1818
private List<InvokeParameterDefinition> params;
1919
private List<MethodArg> methodArgs;
20+
private boolean idempotent;
2021
}

spring-boot-data-aggregator-core/src/main/java/io/github/lvyahui8/spring/aggregate/service/impl/DataBeanAggregateQueryServiceImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ public <T> T get(String id, Map<String, Object> invokeParams, Class<T> resultTyp
9595
/* 如果调用方法是幂等的, 那么当方法签名和方法参数完全一致时, 可以直接使用缓存结果 */
9696
InvokeSignature invokeSignature = new InvokeSignature(provider.getMethod(),args);
9797
Object resultModel;
98-
if(queryCache.containsKey(invokeSignature)) {
98+
if(provider.isIdempotent() && queryCache.containsKey(invokeSignature)) {
9999
resultModel = queryCache.get(invokeSignature);
100100
}
101101
else {
102102
resultModel = provider.getMethod()
103103
.invoke(applicationContext.getBean(provider.getMethod().getDeclaringClass()), args);
104-
/* Map 中可能不能放空value */
105-
queryCache.put(invokeSignature,resultModel != null ? resultModel : AggregatorConstant.EMPTY_MODEL);
104+
if(provider.isIdempotent()) {
105+
/* Map 中可能不能放空value */
106+
queryCache.put(invokeSignature,resultModel != null ? resultModel : AggregatorConstant.EMPTY_MODEL);
107+
}
106108
}
107109

108110
return resultType.cast(resultModel != AggregatorConstant.EMPTY_MODEL ? resultModel : null);

0 commit comments

Comments
 (0)