This is an automated email from the ASF dual-hosted git repository.

jerrick pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new e6b0bc8   Support for caching null values (#2480)
e6b0bc8 is described below

commit e6b0bc8a856abd7f2d7a2e0f06ccc78dc90e2e0a
Author: 54hechuan <[email protected]>
AuthorDate: Fri Sep 21 10:35:53 2018 +0800

     Support for caching null values (#2480)
---
 .../org/apache/dubbo/cache/filter/CacheFilter.java | 26 +++++++++++++++++++---
 .../apache/dubbo/cache/filter/CacheFilterTest.java |  4 ++--
 2 files changed, 25 insertions(+), 5 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 f7e362b..62a739d 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 c51bce7..7d571dd 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);
     }
 }

Reply via email to