Przeglądaj źródła

树形结构缓存

gaodm 5 lat temu
rodzic
commit
6f54237340

+ 9 - 0
ltkg-service/pom.xml

@@ -136,6 +136,15 @@
             <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-cache</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.github.ben-manes.caffeine</groupId>
+            <artifactId>caffeine</artifactId>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 34 - 0
ltkg-service/src/main/java/com/diagbot/config/CacheConfigurer.java

@@ -0,0 +1,34 @@
+package com.diagbot.config;
+
+import com.github.benmanes.caffeine.cache.CacheLoader;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@EnableCaching
+public class CacheConfigurer {
+    /**
+     * 必须要指定这个Bean,refreshAfterWrite=xs这个配置属性才生效
+     *
+     * @return
+     */
+    @Bean
+    public CacheLoader<Object, Object> cacheLoader() {
+        CacheLoader<Object, Object> cacheLoader = new CacheLoader<Object, Object>() {
+
+            @Override
+            public Object load(Object key) throws Exception {
+                return null;
+            }
+
+            // 重写这个方法将oldValue值返回回去,进而刷新缓存
+            @Override
+            public Object reload(Object key, Object oldValue) throws Exception {
+                return oldValue;
+            }
+        };
+
+        return cacheLoader;
+    }
+}

+ 18 - 0
ltkg-service/src/main/java/com/diagbot/web/KgController.java

@@ -13,7 +13,9 @@ import com.diagbot.vo.KgTreeVO;
 import com.diagbot.vo.SchemaVO;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -32,7 +34,9 @@ import java.util.List;
 @RequestMapping("/kg")
 @Api(value = "朗通知识图谱API", tags = { "朗通知识图谱API" })
 @SuppressWarnings("unchecked")
+@Slf4j
 public class KgController {
+    private static final String KGTREECACHE = "KgTreeCache";
 
     @Autowired
     private KgFacade kgFacade;
@@ -72,5 +76,19 @@ public class KgController {
     public RespDTO<GraDTO> getTu(@RequestBody KgQueryVO kgQueryVO) {
         return RespDTO.onSuc(kgFacade.getTuFac(kgQueryVO));
     }
+
+    /**
+     * 清理权限所有缓存
+     *
+     * @return
+     */
+    @ApiOperation(value = "获取树形分类缓存", notes = "获取树形分类缓存")
+    @PostMapping("/delKgTreeCache")
+    @SysLogger("delKgTreeCache")
+    @CacheEvict(cacheNames = KGTREECACHE, allEntries = true)
+    public Boolean delKgTreeCache() {
+        log.info("清理服务权限缓存成功");
+        return true;
+    }
 }