#!/usr/bin/env python3 """ Cache Management Utility Clear or check the status of the ML service cache """ import os import sys import shutil from pathlib import Path CACHE_DIR = Path(__file__).parent / "cache" def get_cache_size(path: Path) -> int: """Get size of cache directory in bytes""" total = 0 if path.exists(): for file in path.rglob('*'): if file.is_file(): total += file.stat().st_size return total def format_size(bytes: int) -> str: """Format bytes to human readable string""" for unit in ['B', 'KB', 'MB', 'GB']: if bytes < 1024: return f"{bytes:.2f} {unit}" bytes /= 1024 return f"{bytes:.2f} TB" def check_cache(): """Check cache status""" print(f"Cache directory: {CACHE_DIR}") if not CACHE_DIR.exists(): print("āŒ Cache directory does not exist") return faiss_index = CACHE_DIR / "faiss.index" metadata = CACHE_DIR / "metadata.pkl" embeddings = CACHE_DIR / "embeddings.npy" print("\nšŸ“Š Cache Status:") print(f" Directory exists: āœ…") files_found = [] total_size = 0 for file, name in [(faiss_index, "FAISS index"), (metadata, "Metadata"), (embeddings, "Embeddings")]: if file.exists(): size = file.stat().st_size total_size += size files_found.append(name) print(f" {name}: āœ… ({format_size(size)})") else: print(f" {name}: āŒ (not found)") if files_found: print(f"\nšŸ“¦ Total cache size: {format_size(total_size)}") print(f"āœ… Cache is ready ({len(files_found)} file(s) found)") else: print("\nāš ļø Cache is empty - will be built on first run") def clear_cache(): """Clear cache directory""" if not CACHE_DIR.exists(): print("āœ… Cache directory does not exist - nothing to clear") return print(f"šŸ—‘ļø Clearing cache directory: {CACHE_DIR}") try: shutil.rmtree(CACHE_DIR) print("āœ… Cache cleared successfully") print("ā„¹ļø Cache will be rebuilt on next service startup") except Exception as e: print(f"āŒ Failed to clear cache: {e}") sys.exit(1) def main(): if len(sys.argv) < 2: print("Usage:") print(" python manage_cache.py check - Check cache status") print(" python manage_cache.py clear - Clear cache") sys.exit(1) command = sys.argv[1].lower() if command == "check": check_cache() elif command == "clear": confirm = input("āš ļø Are you sure you want to clear the cache? [y/N]: ") if confirm.lower() in ['y', 'yes']: clear_cache() else: print("āŒ Cancelled") else: print(f"āŒ Unknown command: {command}") print("Available commands: check, clear") sys.exit(1) if __name__ == "__main__": main()