fix: 缓存直接写+fsync,去掉会误删的写后校验

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
OwnerSunshine530
2026-06-14 22:32:59 +08:00
parent a7234e577a
commit 8855a75cc3
+3 -7
View File
@@ -129,21 +129,17 @@ def _save_cache(cache, item_dict, user_seq):
读取会间歇性报 [Errno 38]。pickle.dump 大对象较慢但顺序写更稳。
"""
import pickle
tmp = str(cache) + ".tmp"
try:
with open(tmp, "wb") as f:
with open(cache, "wb") as f:
pickle.dump({"item_dict": item_dict, "user_seq": user_seq}, f,
protocol=pickle.HIGHEST_PROTOCOL)
f.flush()
os.fsync(f.fileno())
os.replace(tmp, cache)
_load_cache(cache) # 写后立即校验可读
print(f"[BENCH] 已缓存 -> {cache}")
print(f"[BENCH] 已缓存 -> {cache}(下次秒级读取;读不出会自动重建)")
except Exception as e:
print(f"[BENCH][WARN] 缓存写入失败({e}),本次不缓存(不影响结果)")
for p in (tmp, str(cache)):
try:
os.remove(p)
os.remove(cache)
except OSError:
pass