Files
LEDMatrix/scripts/utils/clear_cache.py
T
Claude ab087cf600 fix(scripts): repair broken sys.path setup in utility scripts
clear_cache.py and download_nba_logos.py pointed sys.path at a 'src'
directory relative to the script's own folder (scripts/utils/src and
scripts/src — neither exists), so both crashed on import; they now insert
the project root and import via the src package like the other scripts.
debug_web_manual.py resolved 'project root' to scripts/debug/ instead of
two levels up. fix_nhl_cache.sh is removed: it used Python docstring
syntax in a bash script and invoked clear_nhl_cache.py, which does not
exist anywhere in the repo — it cannot ever have worked in its current
location.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SXb4mKcAkVaxkeTb3YnAdr
2026-08-05 23:55:17 +00:00

128 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""
Cache clearing utility for LEDMatrix
This script allows manual clearing of specific cache keys or all cache data.
"""
import os
import sys
import argparse
# Add the project root to the path so we can import our modules
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from src.cache_manager import CacheManager
def list_cache_keys(cache_manager):
"""List all available cache keys."""
cache_dir = cache_manager.cache_dir
if not cache_dir or not os.path.exists(cache_dir):
print(f"Cache directory does not exist: {cache_dir}")
return []
cache_files = []
for file in os.listdir(cache_dir):
if file.endswith('.json'):
cache_files.append(file[:-5]) # Remove .json extension
return cache_files
def clear_specific_cache(cache_manager, key):
"""Clear a specific cache key."""
try:
cache_manager.clear_cache(key)
print(f"✓ Cleared cache key: {key}")
return True
except Exception as e:
print(f"✗ Error clearing cache key '{key}': {e}")
return False
def clear_all_cache(cache_manager):
"""Clear all cache data."""
try:
cache_manager.clear_cache()
print("✓ Cleared all cache data")
return True
except Exception as e:
print(f"✗ Error clearing all cache: {e}")
return False
def show_cache_info(cache_manager, key=None):
"""Show information about cache entries."""
if key:
try:
data = cache_manager.get(key)
if data is not None:
print(f"Cache key '{key}' exists with data type: {type(data)}")
if isinstance(data, dict):
print(f" Keys: {list(data.keys())}")
if 'games' in data:
print(f" Number of games: {len(data['games']) if isinstance(data['games'], dict) else 'N/A'}")
elif isinstance(data, list):
print(f" Number of items: {len(data)}")
else:
print(f" Data: {str(data)[:100]}...")
else:
print(f"Cache key '{key}' does not exist or is expired")
except Exception as e:
print(f"Error checking cache key '{key}': {e}")
else:
# Show all cache keys
keys = list_cache_keys(cache_manager)
if keys:
print("Available cache keys:")
for key in sorted(keys):
print(f" - {key}")
else:
print("No cache keys found")
def main():
parser = argparse.ArgumentParser(description='Clear LEDMatrix cache data')
parser.add_argument('--list', '-l', action='store_true',
help='List all available cache keys')
parser.add_argument('--clear-all', '-a', action='store_true',
help='Clear all cache data')
parser.add_argument('--clear', '-c', type=str, metavar='KEY',
help='Clear a specific cache key')
parser.add_argument('--info', '-i', type=str, metavar='KEY',
help='Show information about a specific cache key')
args = parser.parse_args()
# Initialize cache manager
cache_manager = CacheManager()
if args.list:
show_cache_info(cache_manager)
elif args.clear_all:
print("Clearing all cache data...")
clear_all_cache(cache_manager)
elif args.clear:
print(f"Clearing cache key: {args.clear}")
clear_specific_cache(cache_manager, args.clear)
elif args.info:
show_cache_info(cache_manager, args.info)
else:
# Default: show available options
print("LEDMatrix Cache Utility")
print("=" * 30)
print()
print("Available commands:")
print(" --list, -l List all cache keys")
print(" --clear-all, -a Clear all cache data")
print(" --clear KEY, -c Clear specific cache key")
print(" --info KEY, -i Show info about cache key")
print()
print("Examples:")
print(" python clear_cache.py --list")
print(" python clear_cache.py --clear milb_live_api_data")
print(" python clear_cache.py --clear-all")
print(" python clear_cache.py --info milb_upcoming_api_data")
print()
# Show current cache status
show_cache_info(cache_manager)
if __name__ == "__main__":
main()