🐛 用户缓存反序列化失败时清理并回源,避免列表翻译姓名时报 500
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+59
-20
@@ -25,6 +25,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.springblade.system.cache;
|
package org.springblade.system.cache;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springblade.core.cache.utils.CacheUtil;
|
import org.springblade.core.cache.utils.CacheUtil;
|
||||||
import org.springblade.core.tool.api.R;
|
import org.springblade.core.tool.api.R;
|
||||||
import org.springblade.core.tool.utils.Func;
|
import org.springblade.core.tool.utils.Func;
|
||||||
@@ -42,6 +43,7 @@ import static org.springblade.core.launch.constant.FlowConstant.TASK_USR_PREFIX;
|
|||||||
*
|
*
|
||||||
* @author Chill
|
* @author Chill
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class UserCache {
|
public class UserCache {
|
||||||
private static final String USER_CACHE_ID = "user:id:";
|
private static final String USER_CACHE_ID = "user:id:";
|
||||||
private static final String USER_CACHE_ACCOUNT = "user:account:";
|
private static final String USER_CACHE_ACCOUNT = "user:account:";
|
||||||
@@ -73,10 +75,18 @@ public class UserCache {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static User getUser(Long userId) {
|
public static User getUser(Long userId) {
|
||||||
return CacheUtil.get(USER_CACHE, USER_CACHE_ID, userId, () -> {
|
try {
|
||||||
R<User> result = getUserClient().userInfoById(userId);
|
return CacheUtil.get(USER_CACHE, USER_CACHE_ID, userId, () -> loadUserById(userId));
|
||||||
return result.getData();
|
} catch (RuntimeException exception) {
|
||||||
});
|
log.warn("用户缓存读取失败,清理损坏缓存后重试, userId={}", userId, exception);
|
||||||
|
clearBrokenUserCache();
|
||||||
|
try {
|
||||||
|
return CacheUtil.get(USER_CACHE, USER_CACHE_ID, userId, () -> loadUserById(userId));
|
||||||
|
} catch (RuntimeException retryException) {
|
||||||
|
log.warn("用户缓存重试仍失败,改为直连查询, userId={}", userId, retryException);
|
||||||
|
return loadUserById(userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,20 +99,25 @@ public class UserCache {
|
|||||||
if (userId == null) {
|
if (userId == null) {
|
||||||
return StringPool.EMPTY;
|
return StringPool.EMPTY;
|
||||||
}
|
}
|
||||||
User user = getUser(userId);
|
try {
|
||||||
if (user == null) {
|
User user = getUser(userId);
|
||||||
|
if (user == null) {
|
||||||
|
return Func.toStr(userId);
|
||||||
|
}
|
||||||
|
if (StringUtil.isNotBlank(user.getRealName())) {
|
||||||
|
return user.getRealName();
|
||||||
|
}
|
||||||
|
if (StringUtil.isNotBlank(user.getName())) {
|
||||||
|
return user.getName();
|
||||||
|
}
|
||||||
|
if (StringUtil.isNotBlank(user.getAccount())) {
|
||||||
|
return user.getAccount();
|
||||||
|
}
|
||||||
|
return Func.toStr(userId);
|
||||||
|
} catch (RuntimeException exception) {
|
||||||
|
log.warn("翻译用户姓名失败,回退为用户ID, userId={}", userId, exception);
|
||||||
return Func.toStr(userId);
|
return Func.toStr(userId);
|
||||||
}
|
}
|
||||||
if (StringUtil.isNotBlank(user.getRealName())) {
|
|
||||||
return user.getRealName();
|
|
||||||
}
|
|
||||||
if (StringUtil.isNotBlank(user.getName())) {
|
|
||||||
return user.getName();
|
|
||||||
}
|
|
||||||
if (StringUtil.isNotBlank(user.getAccount())) {
|
|
||||||
return user.getAccount();
|
|
||||||
}
|
|
||||||
return Func.toStr(userId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -113,10 +128,34 @@ public class UserCache {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static User getUser(String tenantId, String account) {
|
public static User getUser(String tenantId, String account) {
|
||||||
return CacheUtil.get(USER_CACHE, USER_CACHE_ACCOUNT, tenantId + StringPool.DASH + account, () -> {
|
String accountKey = tenantId + StringPool.DASH + account;
|
||||||
R<User> result = getUserClient().userByAccount(tenantId, account);
|
try {
|
||||||
return result.getData();
|
return CacheUtil.get(USER_CACHE, USER_CACHE_ACCOUNT, accountKey, () -> loadUserByAccount(tenantId, account));
|
||||||
});
|
} catch (RuntimeException exception) {
|
||||||
|
log.warn("用户缓存读取失败,清理损坏缓存后重试, accountKey={}", accountKey, exception);
|
||||||
|
clearBrokenUserCache();
|
||||||
|
try {
|
||||||
|
return CacheUtil.get(USER_CACHE, USER_CACHE_ACCOUNT, accountKey, () -> loadUserByAccount(tenantId, account));
|
||||||
|
} catch (RuntimeException retryException) {
|
||||||
|
log.warn("用户缓存重试仍失败,改为直连查询, accountKey={}", accountKey, retryException);
|
||||||
|
return loadUserByAccount(tenantId, account);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static User loadUserById(Long userId) {
|
||||||
|
R<User> result = getUserClient().userInfoById(userId);
|
||||||
|
return result.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static User loadUserByAccount(String tenantId, String account) {
|
||||||
|
R<User> result = getUserClient().userByAccount(tenantId, account);
|
||||||
|
return result.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void clearBrokenUserCache() {
|
||||||
|
CacheUtil.clear(USER_CACHE);
|
||||||
|
CacheUtil.clear(USER_CACHE, Boolean.FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user