feat: updateOrderStatus异步执行方法增加锁,等待异步方法执行完毕
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
package com.sqx.common.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
/**
|
||||
* 接口访问次数 月为单位
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class ApiAccessLimitUtil {
|
||||
|
||||
private static final String ACCESS_COUNT_KEY_PREFIX = "sys:limit:";
|
||||
@@ -122,6 +128,48 @@ public class ApiAccessLimitUtil {
|
||||
}
|
||||
|
||||
|
||||
public static<T> T runFunAndCheckKey(Supplier<T> supplier, String lockKey, Integer seconds) {
|
||||
try{
|
||||
// 创建线程id, 用作判断
|
||||
String clientId = UUID.randomUUID().toString();
|
||||
// 设置分布式锁
|
||||
boolean lock = Boolean.TRUE.equals(redisUtils.setIfAbsent(lockKey, clientId, seconds));
|
||||
int count = 0;
|
||||
while (!lock) {
|
||||
if (count++ > 100) {
|
||||
throw new RuntimeException("系统繁忙, 稍后再试");
|
||||
}
|
||||
Thread.sleep(20);
|
||||
lock = Boolean.TRUE.equals(redisUtils.setIfAbsent(lockKey, clientId, seconds));
|
||||
}
|
||||
return supplier.get();
|
||||
} catch (RuntimeException e){
|
||||
log.info("执行出错:{}", e.getMessage());
|
||||
throw e;
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally{
|
||||
redisUtils.delete(lockKey);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isAccessAllowed(String id, String key, Integer count, Integer seconds) {
|
||||
String redisKey = generateRedisKey(key, id);
|
||||
Object countObj = redisUtils.get(redisKey);
|
||||
if (countObj == null) {
|
||||
// 根据不同时间周期设置过期时间并初始化访问次数为1
|
||||
redisUtils.set(redisKey, 1, seconds);
|
||||
return true;
|
||||
}
|
||||
if (Integer.parseInt(countObj.toString()) < count) {
|
||||
// 访问次数未达上限,次数加1
|
||||
redisUtils.incr(redisKey);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private static String generateRedisKey(String key, String id) {
|
||||
return ACCESS_COUNT_KEY_PREFIX + key + ":" + id;
|
||||
}
|
||||
@@ -142,4 +190,4 @@ public class ApiAccessLimitUtil {
|
||||
long currentTimeStamp = DateUtil.currentSeconds();
|
||||
return endTimeStamp - currentTimeStamp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import cn.hutool.core.date.DateUtil;
|
||||
public class RedisKeys {
|
||||
|
||||
public static final String FREE_WATCH_KEY = "free:watch:";
|
||||
public static final String LOCK_KEY = "SYS:LOCK:";
|
||||
|
||||
|
||||
public static String getSysConfigKey(String key){
|
||||
return "sys:config:" + key;
|
||||
@@ -28,4 +30,15 @@ public class RedisKeys {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(DateUtil.today());
|
||||
}
|
||||
|
||||
|
||||
public static String getLockKey(String sign, Object... args) {
|
||||
StringBuilder key = new StringBuilder(LOCK_KEY + ":" + sign + ":");
|
||||
for (Object arg : args) {
|
||||
if (arg != null) {
|
||||
key.append(":").append(arg);
|
||||
}
|
||||
}
|
||||
return key.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user