当前位置:网站首页>redis实现分布式锁
redis实现分布式锁
2022-06-22 00:28:00 【温温top】
package com.demo.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Component
public class LockUtil {
@Autowired
RedisTemplate<String, String> redisTemplate;
/**
* 获取锁的默认超时时间
*/
private static final long timeout = 300;
/**
* 加锁,无阻塞
* @param key
* @param expireTime 加锁锁时间
* @return
*/
public Boolean lock(String key, long expireTime) {
String requestId = UUID.randomUUID().toString();
Long start = System.currentTimeMillis();
//自旋,在一定时间内获取锁,超时则返回错误
for (; ; ) {
//Set命令返回OK,则证明获取锁成功
Boolean ret = redisTemplate.opsForValue().setIfAbsent(key, requestId, expireTime,
TimeUnit.SECONDS);
if (ret) {
return true;
}
//否则循环等待,在timeout时间内仍未获取到锁,则获取失败
long end = System.currentTimeMillis() - start;
if (end >= timeout) {
return false;
}
}
}
/**
* 加锁,无阻塞
* @param key
* @param expireTime 加锁锁时间 秒
* @param waitTime 等待时间 毫秒
* @return
*/
public boolean lock(String key, long expireTime, long waitTime) {
String value = "trade_task";
long start = System.currentTimeMillis();
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
// 自旋,在一定时间内获取锁,超时则返回错误
for (; ; ) {
//Set命令返回OK,则证明获取锁成功
Boolean ret = opsForValue.setIfAbsent(key, value, expireTime, TimeUnit.SECONDS);
if (ret != null && ret) {
return true;
}
//否则循环等待,在timeout时间内仍未获取到锁,则获取失败
long end = System.currentTimeMillis() - start;
if (end >= waitTime) {
return false;
}
}
}
public void unlock(String key) {
redisTemplate.delete(key);
}
}
边栏推荐
猜你喜欢

Using SSM framework to realize user login

Pytorch learning 04: creation of tensor

RNN的简单整理

编译原理-递归下降子程序法

颜值、空间、安全、动力齐升级,新捷途X70S 8.79万元起售上市
![[dailyfresh] problems encountered in sending activation email](/img/08/b292f6c98615e51e666b78f305e92c.png)
[dailyfresh] problems encountered in sending activation email

Spark RDD案例:词频统计

Special survey of moving average strategy

Pytorch learning 03: tensor data types and some operations
![[cyw20189] VII. Detailed explanation of HCI command format](/img/ba/c61d4868e6c5da8460541c62649880.png)
[cyw20189] VII. Detailed explanation of HCI command format
随机推荐
Tensorflow environment setup
[dailyfresh] course record 3 -- product search related
Pytorch learning 01: gradient descent for simple linear regression
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Yolov3 3D semantic point cloud paper reading
clean,compile,build,install,package区别
Pytorch learning 07:broadcast broadcast - automatic extension
HDOJ - Is It A Tree?
动态规划-01背包,分割等和子集,最后一块石头的重量
PAT(甲) - 1001 A+B Format
52类110 个常用的组件和框架
English grammar_ Adverb_ fairly / rather
【环境踩坑】pycharm使用qt时报错
Sed technique
【ÑÖÏ模拟赛】花萎(矩阵加速,循环卷积,高斯消元)
3746. 牛的学术圈 II
==And equals
Pytorch learning 03: tensor data types and some operations
Precautions for using timestamp type of SQLite3 database
field. setAccessible(true); Code scanning has security vulnerability, solution