manage_cache.py•1.83 kB
#!/usr/bin/env python3
"""
缓存管理工具
"""
import asyncio
from pubchem_mcp.services.cache_service import CacheService
async def main():
"""缓存管理主函数"""
print("🗂️ PubChem MCP 缓存管理工具")
print("=" * 40)
cache_service = CacheService()
await cache_service.initialize()
# 获取缓存统计
stats = cache_service.get_cache_stats()
print(f"📊 缓存统计:")
print(f" 内存缓存条目: {stats.get('memory_cache_size', 0)}")
print(f" 文件缓存数量: {stats.get('file_cache_count', 0)}")
print(f" 总缓存大小: {stats.get('total_cache_size_bytes', 0)} 字节")
print(f" 缓存目录: {stats.get('cache_directory', 'N/A')}")
if stats.get('file_cache_count', 0) > 0:
print(f"\n💡 提示:")
print(f" - 缓存文件存储在 {stats.get('cache_directory')} 目录")
print(f" - 化合物信息缓存2小时")
print(f" - 安全信息和毒性数据缓存1小时")
print(f" - 过期文件会自动删除")
# 询问是否清空缓存(仅在交互环境下)
try:
import sys
if sys.stdin.isatty():
choice = input(f"\n❓ 是否清空所有缓存? (y/N): ").strip().lower()
if choice in ['y', 'yes']:
cache_service.clear_all_cache()
print("✅ 缓存已清空")
else:
print("ℹ️ 缓存保持不变")
else:
print(f"\nℹ️ 非交互环境,缓存保持不变")
except (KeyboardInterrupt, EOFError):
print("\n👋 操作取消")
else:
print("\n📭 暂无缓存数据")
await cache_service.close()
if __name__ == "__main__":
asyncio.run(main())