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 f7e362ba58..62a739d186 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 @@ -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; + } + } } diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/filter/CacheFilterTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/filter/CacheFilterTest.java index c51bce79b9..7d571dd644 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/filter/CacheFilterTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/filter/CacheFilterTest.java @@ -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); } }