第98批续2:getScore 去掉永久缓存命中——每次查 DB 保证数据新鲜
Auto Release / build (push) Successful in 4m9s

根因:pullCreditFromPhp 在 db-worker 线程用裸 JDBC UPDATE users + invalidateScoreCache,
但 /credits 在 Server thread 走 getScore() 命中 scoreCache 返回旧值。
DB 实际已更新(90.77),但缓存返回旧值(100.77)→ '同步了个寂寞'。
invalidateScoreCache 和 clearScoreCache 都治标不治本——线程竞争窗口无法消除。

最终修复:getScore 去掉 cache-hit early return,每次查 DB。users 表很小(几十人),
查 DB 开销可忽略。scoreCache 仍由 setScore/addScore 写入供 checkRestrictions 去重,
但 getScore 不再提前返回缓存。

WebManager pullCreditFromPhp 循环结束后保留 clearScoreCache 兜底。
This commit is contained in:
youpaishidifu
2026-11-08 17:04:31 +08:00
parent 2b5870bb9c
commit 59154fefdf
2 changed files with 13112 additions and 13108 deletions
+5 -3
View File
@@ -313,9 +313,11 @@ public class CreditManager {
␍
/** ★ 第58批-问题4:返回 double,支持小数积分 */␍
public double getScore(String playerName) {␍
if (scoreCache.containsKey(playerName)) {␍
return scoreCache.get(playerName);␍
}␍
// ★ 第98批续修复:去掉永久缓存命中——pullCreditFromPhp 用裸 JDBC UPDATE users,␍
// 缓存不知道 DB 已变 → /credits 返回旧值 → "同步了个寂寞"。␍
// users 表很小(几十人),每次查 DB 开销可忽略。␍
// scoreCache 仍由 setScore/addScore 写入,仅供 checkRestrictions 等同 tick 高频调用去重,␍
// 但 getScore 不再命中缓存提前返回——每次查 DB 保证数据新鲜。␍
double score = DEFAULT_INIT_SCORE;␍
try {␍
PreparedStatement ps = db.getConnection().prepareStatement(␍
+2
View File
@@ -12291,6 +12291,8 @@ public class WebManager {
lastCreditPullSignature = sig;
}
}
// ★ 第98批续修复:拉回完成后全清积分缓存(兜底,防线程竞争残留旧值)
if (cm != null) cm.clearScoreCache();
}
// ★ 第64批:同步 credit_rules 规则表