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
+6 -10
View File
@@ -129,23 +129,19 @@ def _save_cache(cache, item_dict, user_seq):
读取会间歇性报 [Errno 38]。pickle.dump 大对象较慢但顺序写更稳。 读取会间歇性报 [Errno 38]。pickle.dump 大对象较慢但顺序写更稳。
""" """
import pickle import pickle
tmp = str(cache) + ".tmp"
try: try:
with open(tmp, "wb") as f: with open(cache, "wb") as f:
pickle.dump({"item_dict": item_dict, "user_seq": user_seq}, f, pickle.dump({"item_dict": item_dict, "user_seq": user_seq}, f,
protocol=pickle.HIGHEST_PROTOCOL) protocol=pickle.HIGHEST_PROTOCOL)
f.flush() f.flush()
os.fsync(f.fileno()) os.fsync(f.fileno())
os.replace(tmp, cache) print(f"[BENCH] 已缓存 -> {cache}(下次秒级读取;读不出会自动重建)")
_load_cache(cache) # 写后立即校验可读
print(f"[BENCH] 已缓存 -> {cache}")
except Exception as e: except Exception as e:
print(f"[BENCH][WARN] 缓存写入失败({e}),本次不缓存(不影响结果)") print(f"[BENCH][WARN] 缓存写入失败({e}),本次不缓存(不影响结果)")
for p in (tmp, str(cache)): try:
try: os.remove(cache)
os.remove(p) except OSError:
except OSError: pass
pass
def _get_data(cur, ref, rebuild=False): def _get_data(cur, ref, rebuild=False):