Support for caching null values (#2480)
This commit is contained in:
parent
4776efba34
commit
e6b0bc8a85
|
|
@ -16,6 +16,8 @@
|
|||
*/
|
||||
package org.apache.dubbo.cache.filter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.dubbo.cache.Cache;
|
||||
import org.apache.dubbo.cache.CacheFactory;
|
||||
import org.apache.dubbo.common.Constants;
|
||||
|
|
@ -49,16 +51,34 @@ public class CacheFilter implements Filter {
|
|||
String key = StringUtils.toArgumentString(invocation.getArguments());
|
||||
Object value = cache.get(key);
|
||||
if (value != null) {
|
||||
return new RpcResult(value);
|
||||
if (value instanceof ValueWrapper) {
|
||||
return new RpcResult(((ValueWrapper)value).get());
|
||||
} else {
|
||||
return new RpcResult(value);
|
||||
}
|
||||
}
|
||||
Result result = invoker.invoke(invocation);
|
||||
if (!result.hasException() && result.getValue() != null) {
|
||||
cache.put(key, result.getValue());
|
||||
if (!result.hasException()) {
|
||||
cache.put(key, new ValueWrapper(result.getValue()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return invoker.invoke(invocation);
|
||||
}
|
||||
|
||||
static class ValueWrapper implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = -1777337318019193256L;
|
||||
|
||||
private final Object value;
|
||||
|
||||
public ValueWrapper(Object value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Object get() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ public class CacheFilterTest {
|
|||
cacheFilter.invoke(invoker4, invocation);
|
||||
RpcResult rpcResult1 = (RpcResult) cacheFilter.invoke(invoker1, invocation);
|
||||
RpcResult rpcResult2 = (RpcResult) cacheFilter.invoke(invoker2, invocation);
|
||||
Assert.assertEquals(rpcResult1.getValue(), "value1");
|
||||
Assert.assertEquals(rpcResult2.getValue(), "value1");
|
||||
Assert.assertEquals(rpcResult1.getValue(), null);
|
||||
Assert.assertEquals(rpcResult2.getValue(), null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue