之前 service.translate 写 cache 无条件,导致:
- 第一次翻译失败时,'[翻译失败: ...]' 占位符被写进 cache
- 30 天内相同文本的请求(新文章 title 与老文章 title 相同时)全部返回占位符
- 触发 200+ 文章 title_zh 字段被永久污染
修法:仅在 engine ∈ {tencent, nllb, cache} 且文本不含错误标记时,才写 cache。
18 lines
570 B
Python
18 lines
570 B
Python
|
|
import redis
|
|
r = redis.Redis(host="localhost", port=6379, password="b5eb4d10f12a5b1f82ab0a581105d5192a0a0b22366934dc", decode_responses=True)
|
|
to_del = []
|
|
n = 0
|
|
for k in r.scan_iter("translation:cache:*", count=200):
|
|
v = r.get(k)
|
|
if v and ("[翻译失败" in v or "[本条未翻译" in v):
|
|
to_del.append(k)
|
|
n += 1
|
|
print(f" found {n} bad keys, deleting...")
|
|
if to_del:
|
|
r.delete(*to_del)
|
|
print(f" deleted {len(to_del)}")
|
|
# 总数
|
|
total = sum(1 for _ in r.scan_iter("translation:cache:*", count=200))
|
|
print(f" remaining cache keys: {total}")
|