clear_cache_simple.py 3.8 KB

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