perf(core): 改用线程安全的方式从缓存中获取lambda对象

This commit is contained in:
wy 2022-12-11 10:46:29 +08:00
parent 58d28c0767
commit 74db3733a2

View File

@ -66,11 +66,8 @@ public class LambdaFactory {
Assert.notNull(functionInterfaceType);
Assert.notNull(method);
Tuple cacheKey = new Tuple(functionInterfaceType, method);
Object cacheValue = CACHE.get(cacheKey);
if (null != cacheValue) {
//noinspection unchecked
return (F) cacheValue;
}
return (F) CACHE.computeIfAbsent(cacheKey, key -> {
List<Method> abstractMethods = Arrays.stream(functionInterfaceType.getMethods())
.filter(m -> Modifier.isAbstract(m.getModifiers()))
.collect(Collectors.toList());
@ -107,11 +104,11 @@ public class LambdaFactory {
try {
//noinspection unchecked
F lambda = (F) callSite.getTarget().invoke();
CACHE.put(cacheKey, lambda);
return lambda;
return (F) callSite.getTarget().invoke();
} catch (Throwable e) {
throw new RuntimeException(e);
}
});
}
}