[重大更新] 使用caffeine重构PlusSaTokenDao层实现 减少将近90%的redis查询提高性能(尝试性更新问题未知 请勿轻易更新尝试)
This commit is contained in:
parent
12b40f2bbe
commit
df5edb67f0
@ -10,7 +10,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.core.constant.CacheConstants;
|
||||
import org.dromara.common.core.constant.Constants;
|
||||
import org.dromara.common.core.domain.dto.UserOnlineDTO;
|
||||
import org.dromara.common.core.domain.model.LoginUser;
|
||||
import org.dromara.common.core.utils.MessageUtils;
|
||||
import org.dromara.common.core.utils.ServletUtils;
|
||||
import org.dromara.common.core.utils.SpringUtils;
|
||||
@ -43,7 +42,6 @@ public class UserActionListener implements SaTokenListener {
|
||||
public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
|
||||
UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
|
||||
String ip = ServletUtils.getClientIP();
|
||||
LoginUser user = LoginHelper.getLoginUser();
|
||||
UserOnlineDTO dto = new UserOnlineDTO();
|
||||
dto.setIpaddr(ip);
|
||||
dto.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
|
||||
@ -51,10 +49,11 @@ public class UserActionListener implements SaTokenListener {
|
||||
dto.setOs(userAgent.getOs().getName());
|
||||
dto.setLoginTime(System.currentTimeMillis());
|
||||
dto.setTokenId(tokenValue);
|
||||
dto.setUserName(user.getUsername());
|
||||
dto.setClientKey(user.getClientKey());
|
||||
dto.setDeviceType(user.getDeviceType());
|
||||
dto.setDeptName(user.getDeptName());
|
||||
String username = (String) loginModel.getExtra(LoginHelper.USER_NAME_KEY);
|
||||
dto.setUserName(username);
|
||||
dto.setClientKey((String) loginModel.getExtra(LoginHelper.CLIENT_KEY));
|
||||
dto.setDeviceType(loginModel.getDevice());
|
||||
dto.setDeptName((String) loginModel.getExtra(LoginHelper.DEPT_NAME_KEY));
|
||||
if(tokenConfig.getTimeout() == -1) {
|
||||
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto);
|
||||
} else {
|
||||
@ -62,14 +61,14 @@ public class UserActionListener implements SaTokenListener {
|
||||
}
|
||||
// 记录登录日志
|
||||
LogininforEvent logininforEvent = new LogininforEvent();
|
||||
logininforEvent.setTenantId(user.getTenantId());
|
||||
logininforEvent.setUsername(user.getUsername());
|
||||
logininforEvent.setTenantId((String) loginModel.getExtra(LoginHelper.TENANT_KEY));
|
||||
logininforEvent.setUsername(username);
|
||||
logininforEvent.setStatus(Constants.LOGIN_SUCCESS);
|
||||
logininforEvent.setMessage(MessageUtils.message("user.login.success"));
|
||||
logininforEvent.setRequest(ServletUtils.getRequest());
|
||||
SpringUtils.context().publishEvent(logininforEvent);
|
||||
// 更新登录信息
|
||||
loginService.recordLoginInfo(user.getUserId(), ip);
|
||||
loginService.recordLoginInfo((Long) loginModel.getExtra(LoginHelper.USER_KEY), ip);
|
||||
log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue);
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@ import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -36,6 +36,11 @@
|
||||
<artifactId>sa-token-jwt</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -2,12 +2,16 @@ package org.dromara.common.satoken.core.dao;
|
||||
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import org.dromara.common.redis.utils.RedisUtils;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Sa-Token持久层接口(使用框架自带RedisUtils实现 协议统一)
|
||||
@ -16,12 +20,23 @@ import java.util.List;
|
||||
*/
|
||||
public class PlusSaTokenDao implements SaTokenDao {
|
||||
|
||||
private static final Cache<String, Object> CAFFEINE = Caffeine.newBuilder()
|
||||
// 设置最后一次写入或访问后经过固定时间过期
|
||||
.expireAfterWrite(10, TimeUnit.SECONDS)
|
||||
// 初始的缓存空间大小
|
||||
.initialCapacity(100)
|
||||
// 缓存的最大条数
|
||||
.maximumSize(1000)
|
||||
.build();
|
||||
|
||||
/**
|
||||
* 获取Value,如无返空
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return RedisUtils.getCacheObject(key);
|
||||
Object o = CAFFEINE.get(key, k -> RedisUtils.getCacheObject(key));
|
||||
Console.log("caffeine -> key:" + key + ",value:" + o);
|
||||
return (String) o;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -38,6 +53,7 @@ public class PlusSaTokenDao implements SaTokenDao {
|
||||
} else {
|
||||
RedisUtils.setCacheObject(key, value, Duration.ofSeconds(timeout));
|
||||
}
|
||||
CAFFEINE.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -47,6 +63,7 @@ public class PlusSaTokenDao implements SaTokenDao {
|
||||
public void update(String key, String value) {
|
||||
if (RedisUtils.hasKey(key)) {
|
||||
RedisUtils.setCacheObject(key, value, true);
|
||||
CAFFEINE.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,7 +98,9 @@ public class PlusSaTokenDao implements SaTokenDao {
|
||||
*/
|
||||
@Override
|
||||
public Object getObject(String key) {
|
||||
return RedisUtils.getCacheObject(key);
|
||||
Object o = CAFFEINE.get(key, k -> RedisUtils.getCacheObject(key));
|
||||
Console.log("caffeine -> key:" + key + ",value:" + o);
|
||||
return o;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,6 +117,7 @@ public class PlusSaTokenDao implements SaTokenDao {
|
||||
} else {
|
||||
RedisUtils.setCacheObject(key, object, Duration.ofSeconds(timeout));
|
||||
}
|
||||
CAFFEINE.put(key, object);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -107,6 +127,7 @@ public class PlusSaTokenDao implements SaTokenDao {
|
||||
public void updateObject(String key, Object object) {
|
||||
if (RedisUtils.hasKey(key)) {
|
||||
RedisUtils.setCacheObject(key, object, true);
|
||||
CAFFEINE.put(key, object);
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,10 +160,14 @@ public class PlusSaTokenDao implements SaTokenDao {
|
||||
/**
|
||||
* 搜索数据
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
|
||||
Collection<String> keys = RedisUtils.keys(prefix + "*" + keyword + "*");
|
||||
String keyStr = prefix + "*" + keyword + "*";
|
||||
return (List<String>) CAFFEINE.get(keyStr, k -> {
|
||||
Collection<String> keys = RedisUtils.keys(keyStr);
|
||||
List<String> list = new ArrayList<>(keys);
|
||||
return SaFoxUtil.searchList(list, start, size, sortType);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -9,12 +9,10 @@ import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.dromara.common.core.constant.TenantConstants;
|
||||
import org.dromara.common.core.constant.UserConstants;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
import org.dromara.common.core.domain.model.LoginUser;
|
||||
import org.dromara.common.core.enums.UserType;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 登录鉴权助手
|
||||
@ -34,9 +32,10 @@ public class LoginHelper {
|
||||
public static final String LOGIN_USER_KEY = "loginUser";
|
||||
public static final String TENANT_KEY = "tenantId";
|
||||
public static final String USER_KEY = "userId";
|
||||
public static final String USER_NAME_KEY = "userName";
|
||||
public static final String DEPT_KEY = "deptId";
|
||||
public static final String DEPT_NAME_KEY = "deptName";
|
||||
public static final String CLIENT_KEY = "clientid";
|
||||
public static final String TENANT_ADMIN_KEY = "isTenantAdmin";
|
||||
|
||||
/**
|
||||
* 登录系统 基于 设备类型
|
||||
@ -46,15 +45,15 @@ public class LoginHelper {
|
||||
* @param model 配置参数
|
||||
*/
|
||||
public static void login(LoginUser loginUser, SaLoginModel model) {
|
||||
ThreadLocalHolder.set(LOGIN_USER_KEY, loginUser);
|
||||
ThreadLocalHolder.set(TENANT_KEY, loginUser.getTenantId());
|
||||
ThreadLocalHolder.set(USER_KEY, loginUser.getUserId());
|
||||
ThreadLocalHolder.set(DEPT_KEY, loginUser.getDeptId());
|
||||
model = ObjectUtil.defaultIfNull(model, new SaLoginModel());
|
||||
StpUtil.login(loginUser.getLoginId(),
|
||||
model.setExtra(TENANT_KEY, loginUser.getTenantId())
|
||||
.setExtra(USER_KEY, loginUser.getUserId())
|
||||
.setExtra(DEPT_KEY, loginUser.getDeptId()));
|
||||
.setExtra(DEPT_KEY, loginUser.getDeptId())
|
||||
.setExtra(TENANT_KEY, loginUser.getTenantId())
|
||||
.setExtra(DEPT_NAME_KEY, loginUser.getDeptName())
|
||||
.setExtra(USER_NAME_KEY, loginUser.getUsername())
|
||||
);
|
||||
SaSession tokenSession = StpUtil.getTokenSession();
|
||||
tokenSession.updateTimeout(model.getTimeout());
|
||||
tokenSession.set(LOGIN_USER_KEY, loginUser);
|
||||
@ -64,13 +63,11 @@ public class LoginHelper {
|
||||
* 获取用户(多级缓存)
|
||||
*/
|
||||
public static LoginUser getLoginUser() {
|
||||
return (LoginUser) getStorageIfAbsentSet(LOGIN_USER_KEY, () -> {
|
||||
SaSession session = StpUtil.getTokenSession();
|
||||
if (ObjectUtil.isNull(session)) {
|
||||
return null;
|
||||
}
|
||||
return session.get(LOGIN_USER_KEY);
|
||||
});
|
||||
return (LoginUser) session.get(LOGIN_USER_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,7 +103,12 @@ public class LoginHelper {
|
||||
}
|
||||
|
||||
private static Object getExtra(String key) {
|
||||
return getStorageIfAbsentSet(key, () -> StpUtil.getExtra(key));
|
||||
try {
|
||||
return StpUtil.getExtra(key);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -149,26 +151,11 @@ public class LoginHelper {
|
||||
}
|
||||
|
||||
public static boolean isTenantAdmin() {
|
||||
Object value = getStorageIfAbsentSet(TENANT_ADMIN_KEY, () -> {
|
||||
return isTenantAdmin(getLoginUser().getRolePermission());
|
||||
});
|
||||
return Convert.toBool(value);
|
||||
return Convert.toBool(isTenantAdmin(getLoginUser().getRolePermission()));
|
||||
}
|
||||
|
||||
public static boolean isLogin() {
|
||||
return getLoginUser() != null;
|
||||
}
|
||||
|
||||
public static Object getStorageIfAbsentSet(String key, Supplier<Object> handle) {
|
||||
try {
|
||||
Object obj = ThreadLocalHolder.get(key);
|
||||
if (ObjectUtil.isNull(obj)) {
|
||||
obj = handle.get();
|
||||
ThreadLocalHolder.set(key, obj);
|
||||
}
|
||||
return obj;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,17 +4,14 @@ import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import cn.dev33.satoken.router.SaRouter;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.satoken.utils.LoginHelper;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
import org.dromara.common.security.config.properties.SecurityProperties;
|
||||
import org.dromara.common.security.handler.AllUrlHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
@ -63,7 +60,7 @@ public class SecurityConfig implements WebMvcConfigurer {
|
||||
}
|
||||
|
||||
// 保存用户信息
|
||||
ThreadLocalHolder.set(LoginHelper.LOGIN_USER_KEY, LoginHelper.getLoginUser());
|
||||
// ThreadLocalHolder.set(LoginHelper.LOGIN_USER_KEY, LoginHelper.getLoginUser());
|
||||
|
||||
// 有效率影响 用于临时测试
|
||||
// if (log.isDebugEnabled()) {
|
||||
@ -73,12 +70,13 @@ public class SecurityConfig implements WebMvcConfigurer {
|
||||
|
||||
});
|
||||
})
|
||||
{
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
ThreadLocalHolder.remove(LoginHelper.LOGIN_USER_KEY);
|
||||
}
|
||||
}).addPathPatterns("/**")
|
||||
// {
|
||||
// @Override
|
||||
// public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
// ThreadLocalHolder.clear();
|
||||
// }
|
||||
// }
|
||||
).addPathPatterns("/**")
|
||||
// 排除不需要拦截的路径
|
||||
.excludePathPatterns(securityProperties.getExcludes());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user