refactor(cache): 重构缓存机制,使用分文件存储替代单个JSON
This commit is contained in:
@@ -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:
|
|
||||||
logging.error(f"读取缓存文件失败: {e}")
|
|
||||||
return {}
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def save_cache(cache_data):
|
if os.path.exists(cache_file):
|
||||||
"""保存缓存到文件"""
|
try:
|
||||||
try:
|
with open(cache_file, 'r', encoding='utf-8') as f:
|
||||||
with open(CACHE_FILE_PATH, 'w', encoding='utf-8') as f:
|
return f.read()
|
||||||
json.dump(cache_data, f, ensure_ascii=False, indent=4)
|
except IOError as e:
|
||||||
except IOError as e:
|
logging.error(f"读取缓存文件 {cache_file} 失败: {e}")
|
||||||
logging.error(f"写入缓存文件失败: {e}")
|
return None
|
||||||
|
return None
|
||||||
|
|
||||||
# 加载初始缓存
|
def save_cache_for_keyword(keyword: str, data: str):
|
||||||
knowledge_cache = load_cache()
|
"""为指定关键词保存缓存到单独的文件。"""
|
||||||
|
safe_filename = "".join(c for c in keyword if c.isalnum() or c in ('_', '-')).rstrip()
|
||||||
|
cache_file = os.path.join(CACHE_DIR, f"{safe_filename}.txt")
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(cache_file, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(data)
|
||||||
|
except IOError as e:
|
||||||
|
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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user