parent
b9098dadc5
commit
94a73090ba
@ -0,0 +1,21 @@
|
||||
package com.luoo.user.util;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DistributedLock {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
public boolean tryLock(String key, String value) {
|
||||
Boolean success = redisTemplate.opsForValue().setIfAbsent(key, value);
|
||||
return success != null && success;
|
||||
}
|
||||
|
||||
public void unlock(String key) {
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue