Skip to content

Commit 45a076d

Browse files
committed
update RisingStonesService add redis cache
1 parent 088b21c commit 45a076d

1 file changed

Lines changed: 54 additions & 4 deletions

File tree

src/main/java/com/phantoms/phantomsbackend/service/impl/RisingStonesServiceImpl.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class RisingStonesServiceImpl implements RisingStonesService {
3030
private long tokenObtainTime;
3131

3232
// Redis缓存键前缀
33+
private static final String USER_INFO_CACHE_PREFIX = "user:info:";
34+
private static final String GUILD_INFO_CACHE_PREFIX = "guild:info:";
3335
private static final String GUILD_MEMBER_CACHE_PREFIX = "guild:member:";
3436
private static final String GUILD_MEMBER_DYNAMIC_CACHE_PREFIX = "guild:member:dynamic:";
3537

@@ -55,14 +57,62 @@ private synchronized void ensureTokenAndCookie() throws IOException {
5557

5658
@Override
5759
public JSONObject getUserInfo(String uuid) throws IOException {
58-
ensureTokenAndCookie();
59-
return RisingStonesUtils.getUserInfo(uuid, daoyuToken, cookie);
60+
String cacheKey = USER_INFO_CACHE_PREFIX + uuid;
61+
JSONObject result = null;
62+
63+
try {
64+
// 先尝试从叨鱼工具查询
65+
ensureTokenAndCookie();
66+
result = RisingStonesUtils.getUserInfo(uuid, daoyuToken, cookie);
67+
68+
// 如果查询成功,异步写入缓存
69+
if (result != null && result.getInteger("code") == 10000) {
70+
asyncCacheResult(cacheKey, result);
71+
}
72+
} catch (Exception e) {
73+
// 查询失败,从缓存获取
74+
logger.error("Failed to get user info from RisingStonesUtils, trying cache", e);
75+
result = (JSONObject) redisUtil.get(cacheKey);
76+
77+
// 如果缓存也没有,返回空或错误
78+
if (result == null) {
79+
result = new JSONObject();
80+
result.put("code", 500);
81+
result.put("message", "Failed to get user info");
82+
}
83+
}
84+
85+
return result;
6086
}
6187

6288
@Override
6389
public JSONObject getGuildInfo(String guildId) throws IOException {
64-
ensureTokenAndCookie();
65-
return RisingStonesUtils.getGuildInfo(guildId, daoyuToken, cookie);
90+
String cacheKey = GUILD_INFO_CACHE_PREFIX + guildId;
91+
JSONObject result = null;
92+
93+
try {
94+
// 先尝试从叨鱼工具查询
95+
ensureTokenAndCookie();
96+
result = RisingStonesUtils.getGuildInfo(guildId, daoyuToken, cookie);
97+
98+
// 如果查询成功,异步写入缓存
99+
if (result != null && result.getInteger("code") == 10000) {
100+
asyncCacheResult(cacheKey, result);
101+
}
102+
} catch (Exception e) {
103+
// 查询失败,从缓存获取
104+
logger.error("Failed to get guild info from RisingStonesUtils, trying cache", e);
105+
result = (JSONObject) redisUtil.get(cacheKey);
106+
107+
// 如果缓存也没有,返回空或错误
108+
if (result == null) {
109+
result = new JSONObject();
110+
result.put("code", 500);
111+
result.put("message", "Failed to get guild info");
112+
}
113+
}
114+
115+
return result;
66116
}
67117

68118
@Override

0 commit comments

Comments
 (0)