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