mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 10:13:28 +08:00
update 更新 RepeatSubmitAspect, LogAspect, TenantHelper 整合 ThreadLocal -> ThreadLocalHolder ;
This commit is contained in:
parent
8cd74b83dd
commit
daa6ebb1bd
@ -5,6 +5,7 @@ import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import org.dromara.common.core.constant.GlobalConstants;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
import org.dromara.common.core.domain.R;
|
||||
import org.dromara.common.core.exception.ServiceException;
|
||||
import org.dromara.common.core.utils.MessageUtils;
|
||||
@ -36,7 +37,7 @@ import java.util.StringJoiner;
|
||||
@Aspect
|
||||
public class RepeatSubmitAspect {
|
||||
|
||||
private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
|
||||
private static final String KEY_CACHE = "keyCache";
|
||||
|
||||
@Before("@annotation(repeatSubmit)")
|
||||
public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
|
||||
@ -59,7 +60,7 @@ public class RepeatSubmitAspect {
|
||||
// 唯一标识(指定key + url + 消息头)
|
||||
String cacheRepeatKey = GlobalConstants.REPEAT_SUBMIT_KEY + url + submitKey;
|
||||
if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) {
|
||||
KEY_CACHE.set(cacheRepeatKey);
|
||||
ThreadLocalHolder.set(KEY_CACHE, cacheRepeatKey);
|
||||
} else {
|
||||
String message = repeatSubmit.message();
|
||||
if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) {
|
||||
@ -82,9 +83,9 @@ public class RepeatSubmitAspect {
|
||||
if (r.getCode() == R.SUCCESS) {
|
||||
return;
|
||||
}
|
||||
RedisUtils.deleteObject(KEY_CACHE.get());
|
||||
RedisUtils.deleteObject((String) ThreadLocalHolder.get(KEY_CACHE));
|
||||
} finally {
|
||||
KEY_CACHE.remove();
|
||||
ThreadLocalHolder.remove(KEY_CACHE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,8 +98,8 @@ public class RepeatSubmitAspect {
|
||||
*/
|
||||
@AfterThrowing(value = "@annotation(repeatSubmit)", throwing = "e")
|
||||
public void doAfterThrowing(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Exception e) {
|
||||
RedisUtils.deleteObject(KEY_CACHE.get());
|
||||
KEY_CACHE.remove();
|
||||
RedisUtils.deleteObject((String) ThreadLocalHolder.get(KEY_CACHE));
|
||||
ThreadLocalHolder.remove(KEY_CACHE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -4,7 +4,7 @@ import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
import org.dromara.common.core.domain.model.LoginUser;
|
||||
import org.dromara.common.core.utils.ServletUtils;
|
||||
import org.dromara.common.core.utils.SpringUtils;
|
||||
@ -49,9 +49,9 @@ public class LogAspect {
|
||||
|
||||
|
||||
/**
|
||||
* 计算操作消耗时间
|
||||
* 计时 key
|
||||
*/
|
||||
private static final ThreadLocal<StopWatch> TIME_THREADLOCAL = new TransmittableThreadLocal<>();
|
||||
private static final String LOG_STOP_WATCH_KEY = "logStopwatch";
|
||||
|
||||
/**
|
||||
* 处理请求前执行
|
||||
@ -59,7 +59,7 @@ public class LogAspect {
|
||||
@Before(value = "@annotation(controllerLog)")
|
||||
public void boBefore(JoinPoint joinPoint, Log controllerLog) {
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
TIME_THREADLOCAL.set(stopWatch);
|
||||
ThreadLocalHolder.set(LOG_STOP_WATCH_KEY, stopWatch);
|
||||
stopWatch.start();
|
||||
}
|
||||
|
||||
@ -112,7 +112,7 @@ public class LogAspect {
|
||||
// 处理设置注解上的参数
|
||||
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
||||
// 设置消耗时间
|
||||
StopWatch stopWatch = TIME_THREADLOCAL.get();
|
||||
StopWatch stopWatch = ThreadLocalHolder.get(LOG_STOP_WATCH_KEY);
|
||||
stopWatch.stop();
|
||||
operLog.setCostTime(stopWatch.getTime());
|
||||
// 发布事件保存数据库
|
||||
@ -122,7 +122,7 @@ public class LogAspect {
|
||||
log.error("异常信息:{}", exp.getMessage());
|
||||
exp.printStackTrace();
|
||||
} finally {
|
||||
TIME_THREADLOCAL.remove();
|
||||
ThreadLocalHolder.remove(LOG_STOP_WATCH_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,6 @@ package org.dromara.common.tenant.helper;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||
import lombok.AccessLevel;
|
||||
@ -28,7 +27,7 @@ public class TenantHelper {
|
||||
|
||||
private static final String DYNAMIC_TENANT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "dynamicTenant";
|
||||
|
||||
private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>();
|
||||
private static final String TENANT_ID_KEY = "tempDynamicTenant";
|
||||
|
||||
/**
|
||||
* 租户功能是否启用
|
||||
@ -89,7 +88,7 @@ public class TenantHelper {
|
||||
return;
|
||||
}
|
||||
if (!isLogin()) {
|
||||
TEMP_DYNAMIC_TENANT.set(tenantId);
|
||||
ThreadLocalHolder.set(TENANT_ID_KEY, tenantId);
|
||||
return;
|
||||
}
|
||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||
@ -107,7 +106,7 @@ public class TenantHelper {
|
||||
return null;
|
||||
}
|
||||
if (!isLogin()) {
|
||||
return TEMP_DYNAMIC_TENANT.get();
|
||||
return ThreadLocalHolder.get(TENANT_ID_KEY);
|
||||
}
|
||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||
String tenantId = ThreadLocalHolder.get(cacheKey);
|
||||
@ -127,7 +126,7 @@ public class TenantHelper {
|
||||
return;
|
||||
}
|
||||
if (!isLogin()) {
|
||||
TEMP_DYNAMIC_TENANT.remove();
|
||||
ThreadLocalHolder.remove(TENANT_ID_KEY);
|
||||
return;
|
||||
}
|
||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user