登录加锁

This commit is contained in:
none
2023-03-12 10:56:44 +08:00
parent 08a9f5ab73
commit 235e36facc
5 changed files with 121 additions and 56 deletions

View File

@@ -0,0 +1,52 @@
package xyz.playedu.api.util;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/12 10:43
*/
@Component
public class RedisDistributedLock {
private final StringRedisTemplate redisTemplate;
private final ThreadLocal<String> lockValue = new ThreadLocal<>();
public RedisDistributedLock(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public boolean tryLock(String key, long expire, TimeUnit timeUnit) {
String value = UUID.randomUUID().toString();
Boolean success = redisTemplate.opsForValue().setIfAbsent(key, value, expire, timeUnit);
if (Boolean.TRUE.equals(success)) {
lockValue.set(value);
return true;
}
return false;
}
public boolean releaseLock(String key) {
String value = lockValue.get();
if (value == null) {
return false;
}
DefaultRedisScript<Boolean> script = new DefaultRedisScript<>(
"if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end",
Boolean.class
);
Boolean success = redisTemplate.execute(script, Collections.singletonList(key), value);
if (Boolean.TRUE.equals(success)) {
lockValue.remove();
return true;
}
return false;
}
}