🐛 用户缓存反序列化失败时清理并回源,避免列表翻译姓名时报 500

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-10 14:28:31 +08:00
parent 4e7193893f
commit a4287d8b15
@@ -25,6 +25,7 @@
*/
package org.springblade.system.cache;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
@@ -42,6 +43,7 @@ import static org.springblade.core.launch.constant.FlowConstant.TASK_USR_PREFIX;
*
* @author Chill
*/
@Slf4j
public class UserCache {
private static final String USER_CACHE_ID = "user:id:";
private static final String USER_CACHE_ACCOUNT = "user:account:";
@@ -73,10 +75,18 @@ public class UserCache {
* @return
*/
public static User getUser(Long userId) {
return CacheUtil.get(USER_CACHE, USER_CACHE_ID, userId, () -> {
R<User> result = getUserClient().userInfoById(userId);
return result.getData();
});
try {
return CacheUtil.get(USER_CACHE, USER_CACHE_ID, userId, () -> loadUserById(userId));
} 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) {
return StringPool.EMPTY;
}
User user = getUser(userId);
if (user == null) {
try {
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);
}
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
*/
public static User getUser(String tenantId, String account) {
return CacheUtil.get(USER_CACHE, USER_CACHE_ACCOUNT, tenantId + StringPool.DASH + account, () -> {
R<User> result = getUserClient().userByAccount(tenantId, account);
return result.getData();
});
String accountKey = tenantId + StringPool.DASH + account;
try {
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);
}
}