Spaces:
Running

File size: 1,845 Bytes
25834e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Redis cache for /api/v1/metadata results.



Keys: ``meta_fetch:{sha256(url)}``. Successes cache for an hour (a

destination's tags rarely change mid-session); failures negative-cache

briefly so a flapping destination doesn't burn the caller's rate limit

AND our outbound fetches. None-redis tolerant like url_cache.

"""

from __future__ import annotations

import hashlib
import json

import redis.asyncio as aioredis

from app.infrastructure.logging import get_logger

log = get_logger(__name__)


class MetaFetchCache:
    def __init__(

        self,

        redis_client: aioredis.Redis | None,

        *,

        ttl_seconds: int = 3600,

        negative_ttl_seconds: int = 300,

        prefix: str = "meta_fetch",

    ) -> None:
        self._redis = redis_client
        self._ttl = ttl_seconds
        self._negative_ttl = negative_ttl_seconds
        self._prefix = prefix

    def _key(self, url: str) -> str:
        return f"{self._prefix}:{hashlib.sha256(url.encode()).hexdigest()}"

    async def get(self, url: str) -> dict | None:
        if self._redis is None:
            return None
        try:
            raw = await self._redis.get(self._key(url))
            return json.loads(raw) if raw else None
        except Exception as exc:
            log.warning("meta_fetch_cache_get_error", error=str(exc))
            return None

    async def set(self, url: str, payload: dict, *, negative: bool = False) -> None:
        if self._redis is None:
            return
        try:
            await self._redis.setex(
                self._key(url),
                self._negative_ttl if negative else self._ttl,
                json.dumps(payload),
            )
        except Exception as exc:
            log.warning("meta_fetch_cache_set_error", error=str(exc))