This is an automated email from the ASF dual-hosted git repository. albumenj pushed a commit to branch 3.1 in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit fd48e1cc28571f54f9177c7b8c55ee49055fe90b Author: cheese8 <[email protected]> AuthorDate: Wed Jul 6 10:24:05 2022 +0800 using guard clause to refactor CacheFilter (#10274) * using guard clause to refactor CacheFilter * Update CacheFilter.java --- .../org/apache/dubbo/cache/filter/CacheFilter.java | 43 ++++++++++++---------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/filter/CacheFilter.java b/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/filter/CacheFilter.java index 3122a8eee4..209776d3a5 100644 --- a/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/filter/CacheFilter.java +++ b/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/filter/CacheFilter.java @@ -91,26 +91,31 @@ public class CacheFilter implements Filter { */ @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { - if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), CACHE_KEY))) { - Cache cache = cacheFactory.getCache(invoker.getUrl(), invocation); - if (cache != null) { - String key = StringUtils.toArgumentString(invocation.getArguments()); - Object value = cache.get(key); - if (value != null) { - if (value instanceof ValueWrapper) { - return AsyncRpcResult.newDefaultAsyncResult(((ValueWrapper) value).get(), invocation); - } else { - return AsyncRpcResult.newDefaultAsyncResult(value, invocation); - } - } - Result result = invoker.invoke(invocation); - if (!result.hasException()) { - cache.put(key, new ValueWrapper(result.getValue())); - } - return result; - } + if (cacheFactory == null || ConfigUtils.isEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), CACHE_KEY))) { + return invoker.invoke(invocation); } - return invoker.invoke(invocation); + Cache cache = cacheFactory.getCache(invoker.getUrl(), invocation); + if (cache == null) { + return invoker.invoke(invocation); + } + String key = StringUtils.toArgumentString(invocation.getArguments()); + Object value = cache.get(key); + return (value != null) ? onCacheValuePresent(invocation, value) : onCacheValueNotPresent(invoker, invocation, cache, key); + } + + private Result onCacheValuePresent(Invocation invocation, Object value) { + if (value instanceof ValueWrapper) { + return AsyncRpcResult.newDefaultAsyncResult(((ValueWrapper) value).get(), invocation); + } + return AsyncRpcResult.newDefaultAsyncResult(value, invocation); + } + + private Result onCacheValueNotPresent(Invoker<?> invoker, Invocation invocation, Cache cache, String key) { + Result result = invoker.invoke(invocation); + if (!result.hasException()) { + cache.put(key, new ValueWrapper(result.getValue())); + } + return result; } /**
