refactor(cache): 重构缓存机制,使用分文件存储替代单个JSON

This commit is contained in:
2025-08-03 15:56:26 +08:00
parent dcc799f767
commit 1ed4284587

View File

@@ -45,28 +45,32 @@ KNOWLEDGE_BASE_PATHS = [
r"C:\Steam\steamapps\common\RimWorld\Data" r"C:\Steam\steamapps\common\RimWorld\Data"
] ]
# 3. --- 缓存管理 --- # 3. --- 缓存管理 (分文件存储) ---
def load_cache(): def load_cache_for_keyword(keyword: str):
"""加载缓存文件""" """为指定关键词加载缓存文件"""
if os.path.exists(CACHE_FILE_PATH): # 清理关键词,使其适合作为文件名
try: safe_filename = "".join(c for c in keyword if c.isalnum() or c in ('_', '-')).rstrip()
with open(CACHE_FILE_PATH, 'r', encoding='utf-8') as f: cache_file = os.path.join(CACHE_DIR, f"{safe_filename}.txt")
return json.load(f)
except (json.JSONDecodeError, IOError) as e: if os.path.exists(cache_file):
logging.error(f"读取缓存文件失败: {e}") try:
return {} with open(cache_file, 'r', encoding='utf-8') as f:
return {} return f.read()
except IOError as e:
logging.error(f"读取缓存文件 {cache_file} 失败: {e}")
return None
return None
def save_cache(cache_data): def save_cache_for_keyword(keyword: str, data: str):
"""保存缓存到文件""" """为指定关键词保存缓存到单独的文件"""
try: safe_filename = "".join(c for c in keyword if c.isalnum() or c in ('_', '-')).rstrip()
with open(CACHE_FILE_PATH, 'w', encoding='utf-8') as f: cache_file = os.path.join(CACHE_DIR, f"{safe_filename}.txt")
json.dump(cache_data, f, ensure_ascii=False, indent=4)
except IOError as e: try:
logging.error(f"写入缓存文件失败: {e}") with open(cache_file, 'w', encoding='utf-8') as f:
f.write(data)
# 加载初始缓存 except IOError as e:
knowledge_cache = load_cache() logging.error(f"写入缓存文件 {cache_file} 失败: {e}")
# 4. --- 向量化与相似度计算 --- # 4. --- 向量化与相似度计算 ---
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
@@ -292,9 +296,9 @@ def get_context(question: str) -> str:
logging.info(f"提取到关键词: {keyword}") logging.info(f"提取到关键词: {keyword}")
# 1. 检查缓存 # 1. 检查缓存 (新逻辑)
if keyword in knowledge_cache: cached_result = load_cache_for_keyword(keyword)
cached_result = knowledge_cache[keyword] if cached_result:
logging.info(f"缓存命中: 关键词 '{keyword}'") logging.info(f"缓存命中: 关键词 '{keyword}'")
return cached_result return cached_result
@@ -369,8 +373,7 @@ def get_context(question: str) -> str:
# 5. 更新缓存并返回结果 # 5. 更新缓存并返回结果
logging.info(f"向量搜索完成。找到了 {len(best_matches)} 个匹配项并成功提取了代码。") logging.info(f"向量搜索完成。找到了 {len(best_matches)} 个匹配项并成功提取了代码。")
knowledge_cache[keyword] = final_output save_cache_for_keyword(keyword, final_output)
save_cache(knowledge_cache)
return final_output return final_output