mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 优化 使用request存储动态租户 避免单请求多次查询redis获取
This commit is contained in:
		@@ -5,10 +5,12 @@ import cn.hutool.core.convert.Convert;
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
 | 
			
		||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
 | 
			
		||||
import jakarta.servlet.http.HttpServletRequest;
 | 
			
		||||
import lombok.AccessLevel;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.dromara.common.core.constant.GlobalConstants;
 | 
			
		||||
import org.dromara.common.core.utils.ServletUtils;
 | 
			
		||||
import org.dromara.common.core.utils.SpringUtils;
 | 
			
		||||
import org.dromara.common.core.utils.StringUtils;
 | 
			
		||||
import org.dromara.common.core.utils.reflect.ReflectUtils;
 | 
			
		||||
@@ -135,6 +137,7 @@ public class TenantHelper {
 | 
			
		||||
        }
 | 
			
		||||
        String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
 | 
			
		||||
        RedisUtils.setCacheObject(cacheKey, tenantId);
 | 
			
		||||
        ServletUtils.getRequest().setAttribute(cacheKey, tenantId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -154,8 +157,15 @@ public class TenantHelper {
 | 
			
		||||
        if (StringUtils.isNotBlank(tenantId)) {
 | 
			
		||||
            return tenantId;
 | 
			
		||||
        }
 | 
			
		||||
        HttpServletRequest request = ServletUtils.getRequest();
 | 
			
		||||
        String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
 | 
			
		||||
        tenantId = (String) request.getAttribute(cacheKey);
 | 
			
		||||
        // 如果为 -1 说明已经查过redis并且不存在值 则直接返回null
 | 
			
		||||
        if (StringUtils.isNotBlank(tenantId)) {
 | 
			
		||||
            return tenantId.equals("-1") ? null : tenantId;
 | 
			
		||||
        }
 | 
			
		||||
        tenantId = RedisUtils.getCacheObject(cacheKey);
 | 
			
		||||
        request.setAttribute(cacheKey, StringUtils.isBlank(tenantId) ? "-1" : tenantId);
 | 
			
		||||
        return tenantId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -173,6 +183,7 @@ public class TenantHelper {
 | 
			
		||||
        TEMP_DYNAMIC_TENANT.remove();
 | 
			
		||||
        String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
 | 
			
		||||
        RedisUtils.deleteObject(cacheKey);
 | 
			
		||||
        ServletUtils.getRequest().removeAttribute(cacheKey);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user