File size: 540 Bytes
22ec23d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | """Caching to avoid duplicate requests."""
from typing import Dict, Optional
import hashlib
_cache: Dict[str, any] = {}
def get_cache_key(url: str) -> str:
"""Generate cache key from URL."""
return hashlib.md5(url.encode()).hexdigest()
def get_cached(url: str) -> Optional[any]:
"""Get cached response."""
return _cache.get(get_cache_key(url))
def set_cached(url: str, data: any):
"""Cache response."""
_cache[get_cache_key(url)] = data
def clear_cache():
"""Clear all cached data."""
_cache.clear() |