فهرست منبع

增加core-concurrent与data-redis组件依赖,以及简单测试Controller代码

luoshi 4 سال پیش
والد
کامیت
c0bd4b0da7

+ 8 - 2
api-demo/pom.xml

@@ -19,12 +19,18 @@
         </dependency>
         <dependency>
             <groupId>com.qmth.boot</groupId>
-            <artifactId>core-uid</artifactId>
+            <artifactId>data-redis</artifactId>
         </dependency>
+        <!--
         <dependency>
             <groupId>com.qmth.boot</groupId>
-            <artifactId>core-cache</artifactId>
+            <artifactId>core-concurrent</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>core-uid</artifactId>
         </dependency>
+        -->
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>

+ 47 - 0
api-demo/src/main/java/com/qmth/demo/api/controller/LockController.java

@@ -0,0 +1,47 @@
+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.concurrent.annotation.Lockable;
+import com.qmth.boot.core.concurrent.annotation.Locks;
+import com.qmth.boot.core.concurrent.model.LockType;
+import com.qmth.boot.core.concurrent.service.ConcurrentService;
+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 + "/lock")
+@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
+public class LockController {
+
+    @Resource
+    private ConcurrentService concurrentService;
+
+    @RequestMapping("/lock")
+    @Locks({ @Lockable(name = "test", key = "#key", type = LockType.READ),
+            @Lockable(name = "test", key = "#key", timeout = 0) })
+    public Object lock(@RequestParam String key) {
+        return key;
+    }
+
+    @RequestMapping("/tryLock")
+    @Lockable(name = "test", timeout = 0)
+    public Object tryLock(@RequestParam String key) {
+        return key;
+    }
+
+    @RequestMapping("/tryWriteLock")
+    public Object tryReadLock() throws InterruptedException {
+        if (!concurrentService.getReadWriteLock("test").writeLock().tryLock()) {
+            return false;
+        }
+        Thread.sleep(5000);
+        concurrentService.getReadWriteLock("test").writeLock().unlock();
+        return true;
+    }
+
+}

+ 57 - 0
api-demo/src/main/java/com/qmth/demo/api/controller/RedisController.java

@@ -0,0 +1,57 @@
+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 org.redisson.api.RedissonClient;
+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;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+
+@RestController
+@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/redis")
+@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
+public class RedisController {
+
+    //@Resource
+    //private RedisTemplate<String, Object> redisTemplate;
+
+    @Resource
+    private RedissonClient redissonClient;
+
+    @RequestMapping("/get")
+    public Object get(@RequestParam String key) {
+        return redissonClient.getBucket(key).get();
+    }
+
+    @RequestMapping("/put")
+    public Object put(@RequestParam String key, @RequestParam String value) {
+        //redisTemplate.opsForValue().set(key, value);
+        redissonClient.getBucket(key).set(value);
+        return true;
+    }
+
+    @RequestMapping("/hget")
+    public Object hget(@RequestParam String[] key, @RequestParam String hashKey) {
+        //return redisTemplate.opsForHash().multiGet(hashKey, Arrays.asList(key));
+        return redissonClient.getMap(hashKey).getAll(new HashSet<>(Arrays.asList(key)));
+    }
+
+    @RequestMapping("/hput")
+    public Object hput(@RequestParam String hashKey, @RequestParam String[] key, @RequestParam String[] value) {
+        Map<String, Object> map = new HashMap<>();
+        for (int i = 0; i < key.length; i++) {
+            map.put(key[i], value[i]);
+        }
+        //        redisTemplate.opsForHash().putAll(hashKey, map);
+        redissonClient.getMap(hashKey).putAll(map);
+        return true;
+    }
+
+}

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

@@ -10,4 +10,6 @@ server.port=8080
 #com.qmth.logging.root-level=info
 #com.qmth.logging.file-path=/Users/luoshi/Downloads/demo.log
 
-#com.qmth.cache.expire-after-write=5s
+#com.qmth.cache.expire-after-write=30s
+
+com.qmth.redis.host=127.0.0.1

+ 10 - 0
pom.xml

@@ -36,6 +36,16 @@
                 <artifactId>core-cache</artifactId>
                 <version>${qmth.boot.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.qmth.boot</groupId>
+                <artifactId>core-concurrent</artifactId>
+                <version>${qmth.boot.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>com.qmth.boot</groupId>
+                <artifactId>data-redis</artifactId>
+                <version>${qmth.boot.version}</version>
+            </dependency>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>