using guard clause to refactor CacheFilter (#10274)

* using guard clause to refactor CacheFilter

* Update CacheFilter.java
This commit is contained in:
cheese8 2022-07-06 10:24:05 +08:00 committed by GitHub
parent cf552844b2
commit 5bb7ec02b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 19 deletions

View File

@ -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;
}
/**