mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
[重大更新] 使用caffeine重构PlusSaTokenDao层实现 减少将近90%的redis查询提高性能(尝试性更新问题未知 请勿轻易更新尝试)
This commit is contained in:
@ -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 + "*");
|
||||
List<String> list = new ArrayList<>(keys);
|
||||
return SaFoxUtil.searchList(list, start, size, sortType);
|
||||
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);
|
||||
});
|
||||
SaSession session = StpUtil.getTokenSession();
|
||||
if (ObjectUtil.isNull(session)) {
|
||||
return null;
|
||||
}
|
||||
return (LoginUser) session.get(LOGIN_USER_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,7 +85,7 @@ public class LoginHelper {
|
||||
* 获取用户id
|
||||
*/
|
||||
public static Long getUserId() {
|
||||
return Convert.toLong(getExtra(USER_KEY));
|
||||
return Convert.toLong(getExtra(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user