소스 검색

增加cache相关测试代码

luoshi 3 년 전
부모
커밋
14de2eab59

+ 33 - 0
api-demo/src/main/java/com/qmth/demo/api/entity/DemoCacheString.java

@@ -0,0 +1,33 @@
+package com.qmth.demo.api.entity;
+
+public class DemoCacheString {
+
+    private long time;
+
+    private String value;
+
+    public DemoCacheString() {
+
+    }
+
+    public DemoCacheString(String value) {
+        this.time = System.currentTimeMillis();
+        this.value = value;
+    }
+
+    public long getTime() {
+        return time;
+    }
+
+    public void setTime(long time) {
+        this.time = time;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+}

+ 8 - 7
api-demo/src/main/java/com/qmth/demo/api/service/DemoCacheService.java

@@ -1,5 +1,6 @@
 package com.qmth.demo.api.service;
 
+import com.qmth.demo.api.entity.DemoCacheString;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.CachePut;
 import org.springframework.cache.annotation.Cacheable;
@@ -10,18 +11,18 @@ public class DemoCacheService {
 
     public static final String CACHE_NAME = "demo-cache";
 
-    @Cacheable(value = CACHE_NAME, key = "#key")
-    public Object get(String key) {
-        return "null";
+    @Cacheable(value = CACHE_NAME, key = "#key", unless = "#result == null")
+    public DemoCacheString get(String key) {
+        return key.equals("null") ? null : new DemoCacheString(key);
     }
 
-    @CachePut(value = CACHE_NAME, key = "#key")
-    public Object put(String key, String value) {
-        return value;
+    @CachePut(value = CACHE_NAME, key = "#key", unless = "#result == null")
+    public DemoCacheString put(String key, String value) {
+        return new DemoCacheString(value);
     }
 
     @CacheEvict(value = CACHE_NAME, key = "#key", beforeInvocation = true)
-    public Object clear(String key) {
+    public boolean clear(String key) {
         return true;
     }
 }

+ 1 - 0
api-demo/src/main/resources/application.properties

@@ -15,6 +15,7 @@ com.qmth.datasource.username=root
 com.qmth.datasource.password=root
 com.qmth.mybatis.log-level=debug
 
+com.qmth.cache.allow-null-value=false
 com.qmth.cache.expire-after-write=30m
 
 com.qmth.redis.host=127.0.0.1