mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-19 01:48:47 +08:00
redis锁工具类编写。
This commit is contained in:
parent
0e79a81070
commit
0786110414
@ -0,0 +1,112 @@
|
||||
package com.ruoyi.common.core.redis;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.redisson.api.RCountDownLatch;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
public class RedisLockManager {
|
||||
|
||||
@Resource
|
||||
private RedissonClient redissonClient;
|
||||
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
/**
|
||||
* 获取锁(不用设置超时时间,一直等待)
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public RLock getLock(String key){
|
||||
Assert.isTrue(StringUtils.isNotEmpty(key));
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
lock.tryLock();
|
||||
return lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置过期时间
|
||||
* @param key
|
||||
* @param time 过期时间
|
||||
* @param expireUnit 时间单位
|
||||
* @return
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public RLock getLock(String key,Long time, TimeUnit expireUnit) throws InterruptedException {
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
lock.tryLock(time,expireUnit);
|
||||
return lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置过期时间
|
||||
* @param key
|
||||
* @param waitTime 获取锁等待时间
|
||||
* @param leaseTime 保留锁的时间
|
||||
* @param unit 时间单位
|
||||
* @return
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public RLock getLock(String key,long waitTime, long leaseTime, TimeUnit unit)throws InterruptedException{
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
lock.tryLock(waitTime,leaseTime,unit);
|
||||
return lock;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key : 锁的key值 不能为空
|
||||
* @param count :countDownLatch 的数量 必须大于等于0
|
||||
* @return
|
||||
*/
|
||||
public RCountDownLatch countdownLatch(String key,long count){
|
||||
Assert.isTrue(StringUtils.isNotEmpty(key));
|
||||
Assert.isTrue(count>=0);
|
||||
RCountDownLatch rCountDownLatch = redissonClient.getCountDownLatch(key);
|
||||
rCountDownLatch.trySetCount(count);
|
||||
return rCountDownLatch ;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公平锁
|
||||
* @param key
|
||||
* @param waitTime 获取锁等待时间
|
||||
* @param leaseTime 持有锁的时间
|
||||
* @param unit
|
||||
* @return
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public RLock getFairLock(String key,long waitTime, long leaseTime, TimeUnit unit)throws InterruptedException{
|
||||
RLock lock = redissonClient.getFairLock(key);
|
||||
lock.tryLock(waitTime,leaseTime,unit);
|
||||
return lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公平锁
|
||||
* @param key
|
||||
* @param leaseTime 持有锁的时间
|
||||
* @param unit
|
||||
* @return
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public RLock getFairLock(String key, long leaseTime, TimeUnit unit)throws InterruptedException{
|
||||
RLock lock = redissonClient.getFairLock(key);
|
||||
lock.tryLock(leaseTime,unit);
|
||||
return lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放锁(统一释放)
|
||||
* @param lock
|
||||
*/
|
||||
public void closeLock(RLock lock){
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
@ -20,13 +20,6 @@ public class RedisLockUtil {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(RedisLockUtil.class);
|
||||
|
||||
/* public boolean getLock(String key){
|
||||
key = LOCK_TITLE + key;
|
||||
RLock mylock = redissonClient.getLock(key);
|
||||
System.err.println("======lock======" + Thread.currentThread().getName());
|
||||
return true;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 加锁 (RLock)带超时时间的
|
||||
* @param key
|
||||
|
||||
@ -0,0 +1,39 @@
|
||||
package com.ruoyi.demo.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.redis.RedisLockManager;
|
||||
import com.ruoyi.demo.vo.TestTreeVo;
|
||||
import org.redisson.api.RLock;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalTime;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* redis 分布式锁测试
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/demo/redisLock")
|
||||
public class TestRedisLockController {
|
||||
|
||||
@Resource
|
||||
private RedisLockManager redisLockManager;
|
||||
|
||||
/**
|
||||
* 测试锁
|
||||
* @param key
|
||||
* @param time
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getLock")
|
||||
public AjaxResult<Void> testLock(String key,Long time){
|
||||
try {
|
||||
RLock rLock = redisLockManager.getLock(key,time, TimeUnit.SECONDS);
|
||||
System.out.println(LocalTime.now().toString() +"获取到锁:"+key);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return AjaxResult.success();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user