admin
2021-07-10 29918ba877731850d001fb2d1a3f3774698241c1
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
32
33
package com.ks.lijin.controller;
 
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;
        try {
            if (redisTemplate.opsForValue().setIfAbsent(key, "1", 120, TimeUnit.SECONDS)) {
                return "锁定成功";
            } else {
                return "服务器繁忙,请稍后再试";
            }
        } finally {
            redisTemplate.delete(key);
        }
    }
 
}