import argparse
import os
import json
import time
def invalidate_cache(cache_file, api_endpoints, verbose=False):
"""Invalidates the cache for specified API endpoints."""
if not os.path.exists(cache_file):
print(f"Cache file not found: {cache_file}")
return
try:
with open(cache_file, 'r') as f:
cache_data = json.load(f)
except json.JSONDecodeError:
print(f"Error decoding JSON from {cache_file}. Cache file may be corrupted.")
return
if verbose:
print(f"Processing cache file: {cache_file}")
invalidated = False
for endpoint in api_endpoints:
if endpoint in cache_data:
del cache_data[endpoint]
invalidated = True
if verbose:
print(f"Invalidated cache entry for: {endpoint}")
if invalidated:
with open(cache_file, 'w') as f:
json.dump(cache_data, f, indent=4)
print(f"Cache invalidated and saved to: {cache_file}")
else:
print("No cache entries invalidated.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Invalidate API cache.")
parser.add_argument("cache_file", help="Path to the cache file.")
parser.add_argument("endpoints", nargs='+', help="List of API endpoints to invalidate.")
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose output.")
args = parser.parse_args()
invalidate_cache(args.cache_file, args.endpoints, args.verbose)
Add your comment