admin
2021-07-10 29918ba877731850d001fb2d1a3f3774698241c1
service-lijin/src/main/java/com/ks/lijin/controller/TestController.java
@@ -1,6 +1,5 @@
package com.ks.lijin.controller;
import com.ks.lijin.exception.LiJinException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -20,12 +19,15 @@
    @RequestMapping("redis")
    public String testRedis(String uid) throws Exception {
        String key = "createtblijin-" + uid;
        if (redisTemplate.opsForValue().setIfAbsent(key, "1", 120, TimeUnit.SECONDS)) {
            return "锁定成功";
        } else {
            throw new LiJinException(LiJinException.CODE_SERVER_BUSY, "服务器繁忙,请稍后再试");
        try {
            if (redisTemplate.opsForValue().setIfAbsent(key, "1", 120, TimeUnit.SECONDS)) {
                return "锁定成功";
            } else {
                return "服务器繁忙,请稍后再试";
            }
        } finally {
            redisTemplate.delete(key);
        }
    }
}