mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-13 05:12:07 +08:00
1、RedisLockUtil加锁解锁修复sonar.LocksNotUnlockedCheck;2、api、文件导入生产单数据加解锁基于多租户问题修复;
This commit is contained in:
+2
-3
@@ -3,7 +3,6 @@ package com.cf.imes.framework.redis.config;
|
||||
import cn.hutool.core.text.StrPool;
|
||||
import com.cf.imes.framework.redis.core.TimeoutRedisCacheManager;
|
||||
import com.cf.imes.framework.redis.util.RedisLockUtil;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@@ -82,7 +81,7 @@ public class ChenfengCacheAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisLockUtil lockUtil(ChenfengCacheProperties chenfengCacheProperties, RedissonClient client, RedisTemplate redisTemplate) {
|
||||
return new RedisLockUtil(chenfengCacheProperties, client, redisTemplate);
|
||||
public RedisLockUtil lockUtil(RedisTemplate redisTemplate) {
|
||||
return new RedisLockUtil(redisTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
-60
@@ -1,11 +1,8 @@
|
||||
package com.cf.imes.framework.redis.util;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.cf.imes.framework.redis.config.ChenfengCacheProperties;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -20,65 +17,8 @@ import java.util.concurrent.TimeUnit;
|
||||
@Slf4j
|
||||
public class RedisLockUtil {
|
||||
|
||||
private ChenfengCacheProperties chenfengCacheProperties;
|
||||
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 加锁
|
||||
*
|
||||
* @param key rediskey
|
||||
* @return true拿到锁,反之没有做对应的业务提醒
|
||||
*/
|
||||
public boolean acquireLock(String key) {
|
||||
boolean getLock = false;
|
||||
try {
|
||||
RLock rLock = redissonClient.getLock(key);
|
||||
return rLock.tryLock(chenfengCacheProperties.getLockWaitTime(), chenfengCacheProperties.getLockTimeout(), TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
log.error("[RedisLockUtil][lock]加锁失败", e);
|
||||
return getLock;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加锁
|
||||
*
|
||||
* @param key rediskey
|
||||
* @param timeout 锁的超时时间,传空则按照默认配置
|
||||
* @param waitTime 等待获取锁的时间,传空则按照默认配置
|
||||
* @return true拿到锁,反之没有做对应的业务提醒
|
||||
*/
|
||||
public boolean acquireLock(String key, Integer waitTime, Integer timeout) {
|
||||
boolean getLock = false;
|
||||
try {
|
||||
RLock rLock = redissonClient.getLock(key);
|
||||
int wt = ObjectUtil.isNull(waitTime) ? chenfengCacheProperties.getLockWaitTime() : waitTime;
|
||||
int to = ObjectUtil.isNull(timeout) ? chenfengCacheProperties.getLockTimeout() : timeout;
|
||||
return rLock.tryLock(wt, to, TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
log.error("[RedisLockUtil][lock]加锁失败", e);
|
||||
return getLock;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解锁
|
||||
* 使用注意:配合以上两个redisson的lock只能在当前线程中unlock,否则失败!!!
|
||||
*
|
||||
* @param key rediskey
|
||||
*/
|
||||
public void releaseLock(String key) {
|
||||
try {
|
||||
RLock rLock = redissonClient.getLock(key);
|
||||
rLock.unlock();
|
||||
} catch (Exception e) {
|
||||
log.error("[RedisLockUtil][unlock]解锁失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动set nx ex加锁
|
||||
* 使用注意:可用于跨服务
|
||||
|
||||
Reference in New Issue
Block a user