Source code for url_utils.cache
"""In-memory TTL cache for URL metadata."""
from __future__ import annotations
import time
from typing import Any
[docs]
class URLMetadataCache:
"""In-memory TTL cache for URL metadata (default 24 h)."""
DEFAULT_TTL = 86_400
def __init__(self, ttl: int | None = None) -> None:
self._cache: dict[str, tuple[Any, float]] = {}
self.ttl = ttl or self.DEFAULT_TTL
self.hits = 0
self.misses = 0
[docs]
def get(self, url: str) -> Any | None:
if url in self._cache:
data, ts = self._cache[url]
if time.time() - ts < self.ttl:
self.hits += 1
return data
del self._cache[url]
self.misses += 1
return None
[docs]
def clear_expired(self) -> int:
now = time.time()
expired = [
k for k, (_, ts) in self._cache.items() if now - ts >= self.ttl
]
for k in expired:
del self._cache[k]
return len(expired)
_url_cache = URLMetadataCache()