admin
2021-07-05 2f071296b9e1d7a3aa6b6f3818196aaa40af3300
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
 
@Controller
@RequestMapping("test")
public class TestController {
 
    @Resource
    private RedisTemplate redisTemplate;
 
    @ResponseBody
    @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, "服务器繁忙,请稍后再试");
        }
 
    }
 
}