Explorar o código

improvement: 使用caffeine缓存代替guava缓存

ChenSiTong %!s(int64=6) %!d(string=hai) anos
pai
achega
efda4f8756

+ 9 - 4
pom.xml

@@ -64,10 +64,10 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-websocket</artifactId>
         </dependency>
-<!--        <dependency>-->
-<!--            <groupId>org.springframework.boot</groupId>-->
-<!--            <artifactId>spring-boot-starter-cache</artifactId>-->
-<!--        </dependency>-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-cache</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
@@ -140,6 +140,11 @@
             <artifactId>java-jwt</artifactId>
             <version>3.10.2</version>
         </dependency>
+        <dependency>
+            <groupId>com.github.ben-manes.caffeine</groupId>
+            <artifactId>caffeine</artifactId>
+            <version>2.8.1</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 3 - 3
src/main/java/nju/seec/helper/api/bok/KnowledgeApi.java

@@ -4,7 +4,7 @@ import lombok.AllArgsConstructor;
 import lombok.Data;
 import nju.seec.helper.util.Consts;
 import nju.seec.helper.util.RestRequestUtil;
-import nju.seec.helper.util.cache.GuavaCacheUtils;
+import nju.seec.helper.util.cache.CaffeineCacheUtils;
 import nju.seec.helper.vo.knowledge.BokKnowledgeNodeVO;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
@@ -17,7 +17,7 @@ import java.util.List;
 @Component
 public class KnowledgeApi {
     private final RestRequestUtil restRequestUtil;
-    private final GuavaCacheUtils cacheUtils;
+    private final CaffeineCacheUtils cacheUtils;
 
     @Value("${bok.knowledgeUrl}")
     private String knowledgeUrl;
@@ -27,7 +27,7 @@ public class KnowledgeApi {
 
     private static final String BOK_KNOWLEDGE_CACHE_NAME = Consts.SYS_NAME + "_bok_knowledge";
 
-    public KnowledgeApi(RestRequestUtil restRequestUtil, GuavaCacheUtils cacheUtils) {
+    public KnowledgeApi(RestRequestUtil restRequestUtil, CaffeineCacheUtils cacheUtils) {
         this.restRequestUtil = restRequestUtil;
         this.cacheUtils = cacheUtils;
     }

+ 3 - 3
src/main/java/nju/seec/helper/api/bok/QuestionApi.java

@@ -13,7 +13,7 @@ import nju.seec.helper.enums.QuestionType;
 import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.util.Consts;
 import nju.seec.helper.util.RestRequestUtil;
-import nju.seec.helper.util.cache.GuavaCacheUtils;
+import nju.seec.helper.util.cache.CaffeineCacheUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
@@ -34,7 +34,7 @@ import java.util.stream.Collectors;
 @Component
 public class QuestionApi {
     private final RestRequestUtil restRequestUtil;
-    private final GuavaCacheUtils cacheUtils;
+    private final CaffeineCacheUtils cacheUtils;
     @Value("${bok.tqUrl}")
     private String tqUrl;
     @Value("${bok.tqStemSearchUrl}")
@@ -42,7 +42,7 @@ public class QuestionApi {
     @Value("${bok.tqIdSearchUrl}")
     private String tqIdSearchUrl;
 
-    public QuestionApi(RestRequestUtil restRequestUtil, GuavaCacheUtils cacheUtils) {
+    public QuestionApi(RestRequestUtil restRequestUtil, CaffeineCacheUtils cacheUtils) {
         this.restRequestUtil = restRequestUtil;
         this.cacheUtils = cacheUtils;
     }

+ 63 - 0
src/main/java/nju/seec/helper/util/cache/CaffeineCacheUtils.java

@@ -0,0 +1,63 @@
+package nju.seec.helper.util.cache;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * @author xst
+ * <p>
+ * updated by cst
+ */
+@Component
+public class CaffeineCacheUtils {
+    private final Cache<String, Object> cache;
+
+    public CaffeineCacheUtils(@Value("${guavaCache.maximumSize}") Long maximumSize,
+                              @Value("${guavaCache.expireAfterAccessInSeconds}") Long expireAfterAccessInSeconds,
+                              @Value("${guavaCache.expireAfterWriteInSeconds}") Long expireAfterWriteInSeconds) {
+        cache = Caffeine
+                .newBuilder()
+                .maximumSize(maximumSize)
+                .expireAfterAccess(expireAfterAccessInSeconds, TimeUnit.SECONDS)
+                .expireAfterWrite(expireAfterWriteInSeconds, TimeUnit.SECONDS)
+                .build();
+    }
+
+    public void set(String cacheName, String key, Object value) {
+        cache.put(combineKey(cacheName, key), value);
+    }
+
+    public void setAll(String cacheName, Map<String, Object> putAll) {
+        final Map<String, Object> all = putAll.entrySet().stream().collect(Collectors.toMap(entry -> combineKey(cacheName, entry.getKey()), Map.Entry::getValue));
+        cache.putAll(all);
+    }
+
+    public Object get(String cacheName, String key) {
+        cache.get(key, k -> k);
+        return cache.getIfPresent(combineKey(cacheName, key));
+    }
+
+    public Map<String, Object> multiGet(String cacheName, Collection<String> keys) {
+        Collection<String> body = keys.stream().map(s -> combineKey(cacheName, s)).collect(Collectors.toList());
+        return cache.getAllPresent(body);
+    }
+
+    public void invalidate(String cacheName, String key) {
+        cache.invalidate(combineKey(cacheName, key));
+    }
+
+    public void invalidateAll() {
+        cache.invalidateAll();
+    }
+
+    private String combineKey(String cacheName, String key) {
+        return cacheName + ":" + key;
+    }
+}

+ 3 - 3
src/main/java/nju/seec/helper/util/oss/OssObjectUrlUtils.java

@@ -1,7 +1,7 @@
 package nju.seec.helper.util.oss;
 
 import nju.seec.helper.util.Consts;
-import nju.seec.helper.util.cache.GuavaCacheUtils;
+import nju.seec.helper.util.cache.CaffeineCacheUtils;
 import org.springframework.stereotype.Component;
 
 /**
@@ -11,10 +11,10 @@ import org.springframework.stereotype.Component;
 public class OssObjectUrlUtils {
     private static final String OSS_OBJECT_URL_NAME = Consts.SYS_NAME + "_oss_object_url";
     private static final long OSS_OBJECT_URL_LIVING_SECONDS = 20 * 60;
-    private final GuavaCacheUtils cacheUtils;
+    private final CaffeineCacheUtils cacheUtils;
     private final OssUtils ossUtils;
 
-    public OssObjectUrlUtils(GuavaCacheUtils cacheUtils, OssUtils ossUtils) {
+    public OssObjectUrlUtils(CaffeineCacheUtils cacheUtils, OssUtils ossUtils) {
         this.cacheUtils = cacheUtils;
         this.ossUtils = ossUtils;
     }