import hashlib
import os
import re
from urllib.parse import urlparse
def invalidate_cache(url, staging_envs, cache_dir="cache"):
"""
Invalidates the cache for a given URL if it's in a staging environment.
Args:
url (str): The URL of the HTML document.
staging_envs (list): A list of staging environment names (e.g., ["staging", "dev"]).
cache_dir (str): The directory where the cache is stored.
"""
if not isinstance(url, str):
raise TypeError("URL must be a string.")
if not isinstance(staging_envs, list):
raise TypeError("staging_envs must be a list.")
if not all(isinstance(env, str) for env in staging_envs):
raise TypeError("All elements in staging_envs must be strings.")
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
try:
parsed_url = urlparse(url)
filename = parsed_url.path
if not filename:
filename = "index.html" # Default filename
# Generate a hash of the URL for cache key
cache_key = hashlib.md5(filename.encode('utf-8')).hexdigest()
cache_file_path = os.path.join(cache_dir, cache_key)
# Check if the cache file exists
if os.path.exists(cache_file_path):
# Check if the URL is in a staging environment
if any(env in url.lower() for env in staging_envs):
# Invalidate the cache by deleting the file
os.remove(cache_file_path)
print(f"Invalidated cache for: {url}")
else:
print(f"Cache valid for: {url}")
else:
print(f"Cache file not found for: {url}")
except Exception as e:
print(f"Error processing URL {url}: {e}")
if __name__ == '__main__':
#Example Usage
invalidate_cache("https://staging.example.com/index.html", ["staging"])
invalidate_cache("https://dev.example.com/about.html", ["dev"])
invalidate_cache("https://example.com/contact.html", ["staging", "dev"])
invalidate_cache("https://example.com/index.html") #Normal environment
invalidate_cache(123) #Test input validation
Add your comment