clear_cache.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. 清除缓存工具
  3. """
  4. import os
  5. import sys
  6. import shutil
  7. # 添加路径
  8. current_dir = os.path.dirname(os.path.abspath(__file__))
  9. root_dir = os.path.dirname(current_dir)
  10. sys.path.insert(0, root_dir)
  11. from knowledge_v2.cache_manager import CacheManager
  12. def clear_all_cache():
  13. """清除所有缓存"""
  14. cache = CacheManager()
  15. if os.path.exists(cache.base_cache_dir):
  16. print(f"缓存目录: {cache.base_cache_dir}")
  17. # 统计信息
  18. total_size = 0
  19. file_count = 0
  20. for root, dirs, files in os.walk(cache.base_cache_dir):
  21. for file in files:
  22. file_path = os.path.join(root, file)
  23. total_size += os.path.getsize(file_path)
  24. file_count += 1
  25. print(f"文件数量: {file_count}")
  26. print(f"总大小: {total_size / 1024:.2f} KB")
  27. # 确认删除
  28. response = input("\n确认清除所有缓存?(yes/no): ")
  29. if response.lower() == 'yes':
  30. shutil.rmtree(cache.base_cache_dir)
  31. os.makedirs(cache.base_cache_dir)
  32. print("✓ 已清除所有缓存")
  33. else:
  34. print("取消操作")
  35. else:
  36. print("缓存目录不存在")
  37. def clear_question_cache(question: str):
  38. """清除特定问题的缓存"""
  39. cache = CacheManager()
  40. import hashlib
  41. question_hash = hashlib.md5(question.encode('utf-8')).hexdigest()[:12]
  42. question_dir = os.path.join(cache.base_cache_dir, question_hash)
  43. if os.path.exists(question_dir):
  44. # 显示问题文本
  45. question_file = os.path.join(question_dir, 'question.txt')
  46. if os.path.exists(question_file):
  47. with open(question_file, 'r', encoding='utf-8') as f:
  48. cached_question = f.read()
  49. print(f"找到缓存的问题: {cached_question}")
  50. # 确认删除
  51. response = input("\n确认清除此问题的缓存?(yes/no): ")
  52. if response.lower() == 'yes':
  53. shutil.rmtree(question_dir)
  54. print("✓ 已清除该问题的缓存")
  55. else:
  56. print("取消操作")
  57. else:
  58. print("未找到该问题的缓存")
  59. def list_cached_questions():
  60. """列出所有缓存的问题"""
  61. cache = CacheManager()
  62. if not os.path.exists(cache.base_cache_dir):
  63. print("缓存目录不存在")
  64. return
  65. print("缓存的问题列表:")
  66. print("=" * 60)
  67. count = 0
  68. for dir_name in os.listdir(cache.base_cache_dir):
  69. question_dir = os.path.join(cache.base_cache_dir, dir_name)
  70. if os.path.isdir(question_dir):
  71. question_file = os.path.join(question_dir, 'question.txt')
  72. if os.path.exists(question_file):
  73. with open(question_file, 'r', encoding='utf-8') as f:
  74. question = f.read()
  75. count += 1
  76. print(f"{count}. [{dir_name}] {question[:50]}...")
  77. print("=" * 60)
  78. print(f"总计: {count} 个缓存问题")
  79. if __name__ == "__main__":
  80. print("缓存管理工具")
  81. print("=" * 60)
  82. print("1. 列出所有缓存")
  83. print("2. 清除所有缓存")
  84. print("3. 清除特定问题的缓存")
  85. print("=" * 60)
  86. choice = input("请选择操作 (1/2/3): ")
  87. if choice == "1":
  88. list_cached_questions()
  89. elif choice == "2":
  90. clear_all_cache()
  91. elif choice == "3":
  92. question = input("请输入问题的完整文本(需要与原问题完全一致): ")
  93. clear_question_cache(question)
  94. else:
  95. print("无效的选择")