Pārlūkot izejas kodu

增加core-cache组件依赖与演示代码

luoshi 4 gadi atpakaļ
vecāks
revīzija
2c76ce1c75

+ 4 - 0
api-demo/pom.xml

@@ -21,6 +21,10 @@
             <groupId>com.qmth.boot</groupId>
             <artifactId>core-uid</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>core-cache</artifactId>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>

+ 2 - 0
api-demo/src/main/java/com/qmth/demo/api/ApiDemoApplication.java

@@ -2,11 +2,13 @@ package com.qmth.demo.api;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cache.annotation.EnableCaching;
 
 /**
  * api demo application
  */
 @SpringBootApplication(scanBasePackages = "com.qmth.*")
+@EnableCaching
 public class ApiDemoApplication {
 
     public static void main(String[] args) {

+ 27 - 0
api-demo/src/main/java/com/qmth/demo/api/cache/DemoCacheService.java

@@ -0,0 +1,27 @@
+package com.qmth.demo.api.cache;
+
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.CachePut;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Component;
+
+@Component
+public class DemoCacheService {
+
+    public static final String CACHE_NAME = "demo-cache";
+
+    @Cacheable(value = CACHE_NAME, key = "#key")
+    public Object get(String key) {
+        return "null";
+    }
+
+    @CachePut(value = CACHE_NAME, key = "#key")
+    public Object put(String key, String value) {
+        return value;
+    }
+
+    @CacheEvict(value = CACHE_NAME, key = "#key", beforeInvocation = true)
+    public Object clear(String key) {
+        return true;
+    }
+}

+ 45 - 0
api-demo/src/main/java/com/qmth/demo/api/controller/CacheController.java

@@ -0,0 +1,45 @@
+package com.qmth.demo.api.controller;
+
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.annotation.BOOL;
+import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.boot.core.cache.service.CacheService;
+import com.qmth.demo.api.cache.DemoCacheService;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+@RestController
+@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/cache")
+@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
+public class CacheController {
+
+    @Resource
+    private DemoCacheService demoCacheService;
+
+    @Resource
+    private CacheService cacheService;
+
+    @RequestMapping("/list")
+    public Object list() {
+        return cacheService.endpoints();
+    }
+
+    @RequestMapping("/get")
+    public Object get(@RequestParam String key) {
+        return demoCacheService.get(key);
+    }
+
+    @RequestMapping("/put")
+    public Object put(@RequestParam String key, @RequestParam String value) {
+        return demoCacheService.put(key, value);
+    }
+
+    @RequestMapping("/clear")
+    public Object clear(@RequestParam String key) {
+        return demoCacheService.clear(key);
+    }
+
+}

+ 9 - 0
api-demo/src/main/java/com/qmth/demo/api/controller/IndexController.java

@@ -5,6 +5,7 @@ import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.config.ApiConfig;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.core.enums.Platform;
+import com.qmth.boot.core.security.config.SecurityConfig;
 import com.qmth.boot.core.uid.service.UidService;
 import org.apache.commons.lang3.RandomUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -24,6 +25,9 @@ public class IndexController {
     @Resource
     private UidService uidService;
 
+    @Resource
+    private SecurityConfig securityConfig;
+
     @RequestMapping("/strict")
     @Aac(strict = BOOL.TRUE)
     public Object strict() {
@@ -53,6 +57,11 @@ public class IndexController {
         return apiConfig;
     }
 
+    @RequestMapping("/security")
+    public Object security() {
+        return securityConfig;
+    }
+
     @RequestMapping("/uid")
     public Object getUid() {
         return uidService.getId();

+ 5 - 2
api-demo/src/main/resources/application.properties

@@ -1,10 +1,13 @@
 server.port=8080
 
-#om.qmth.api.uri-prefix=/aaa
+#com.qmth.api.uri-prefix=/aaa
 #com.qmth.api.metrics-endpoint=/metrics
 #com.qmth.api.global-auth=false
 #com.qmth.api.global-strict=false
 #com.qmth.api.global-rate-limit=1/5s
 
+#com.qmth.logging.pattern=
 #com.qmth.logging.root-level=info
-#com.qmth.logging.file-path=/Users/luoshi/Downloads/demo.log
+#com.qmth.logging.file-path=/Users/luoshi/Downloads/demo.log
+
+#com.qmth.cache.expire-after-write=5s

+ 5 - 0
pom.xml

@@ -31,6 +31,11 @@
                 <artifactId>core-uid</artifactId>
                 <version>${qmth.boot.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.qmth.boot</groupId>
+                <artifactId>core-cache</artifactId>
+                <version>${qmth.boot.version}</version>
+            </dependency>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>