Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for caching null values #2480

Merged
merged 6 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,16 +51,34 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void testNull() {
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);
}
}