xiaowei-system/tmp/add_cache.py

41 lines
1.6 KiB
Python

with open('/www/wwwroot/gaokao/gaokao-chat-server-v2.py', 'r') as f:
content = f.read()
old = """ raw_reply, db_used = call_deepseek(messages), bool(db_context)
# \u2500\u2500 \u8bca\u65ad\u65e5\u5fd7"""
new = """ # \u2500\u2500 AI \u56de\u590d\u7f13\u5b58\uff1a\u76f8\u540c\u5206\u6570+\u79d1\u7c7b\u590d\u7528\uff0c\u65e0\u9700\u91cd\u590d\u8c03 API \u2500\u2500
cache_key = None
if db_has_real_data and score and subject:
cache_key = "rec_cache:{}:{}".format(score, subject)
cached = _redis.get(cache_key)
if cached:
raw_reply = cached
import datetime as _dt
with open('/tmp/gaokao_diag.log', 'a') as _f:
_f.write("[{}] CACHE_HIT score={} subject={} key={}\\n".format(
_dt.datetime.now().isoformat(), score, subject, cache_key))
return raw_reply, True
raw_reply, db_used = call_deepseek(messages), bool(db_context)
# \u7f13\u5b58\u7ed3\u679c\uff08\u4ec5\u5728\u6570\u636e\u5e93\u6709\u771f\u5b9e\u63a8\u8350\u6570\u636e\u65f6\uff09
if cache_key:
_redis.setex(cache_key, 3600, raw_reply)
# \u2500\u2500 \u8bca\u65ad\u65e5\u5fd7"""
if old in content:
content = content.replace(old, new)
with open('/www/wwwroot/gaokao/gaokao-chat-server-v2.py', 'w') as f:
f.write(content)
print('Cache logic added successfully!')
else:
print('Could not find insertion point!')
# Show the actual content for debugging
idx = content.find('raw_reply, db_used = call_deepseek')
if idx >= 0:
print('Found at index:', idx)
print(repr(content[idx-20:idx+100]))