Spaces:
Running

File size: 3,980 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Redis-backed read-through cache for feature flag documents.



Stores ``FeatureFlagDoc`` as JSON. Negative cache (sentinel string ``MISS``)

prevents repo hammering for unregistered flag names. Cache invalidation is

purely TTL-based — flags mutate via direct mongosh edits (no app event to

trigger active invalidation), so flips propagate within ``ttl_seconds``.



Mirrors ``infrastructure/cache/url_cache.UrlCache`` shape: tolerates

``redis_client is None`` so the app runs without Redis (self-hosters,

local dev) by always missing through to the repo.

"""

from __future__ import annotations

import redis.asyncio as aioredis

from app.infrastructure.logging import get_logger
from app.schemas.models.feature_flag import FeatureFlagDoc

log = get_logger(__name__)

_NEGATIVE_SENTINEL = "MISS"


class FeatureFlagCache:
    def __init__(

        self,

        redis_client: aioredis.Redis | None,

        ttl_seconds: int = 60,

        negative_ttl_seconds: int = 30,

    ) -> None:
        self._redis = redis_client
        self.ttl_seconds = ttl_seconds
        self.negative_ttl_seconds = negative_ttl_seconds

    def _key(self, name: str) -> str:
        return f"flag:{name}"

    async def get(self, name: str) -> FeatureFlagDoc | NegativeMiss | None:
        """Return cached doc, ``NEGATIVE_MISS`` if cached negative, or ``None`` on real miss.



        Distinguishing "cached miss" from "uncached" lets the service skip

        the repo round-trip for known-absent flags.

        """
        if self._redis is None:
            return None
        try:
            raw = await self._redis.get(self._key(name))
        except Exception as e:
            log.warning("feature_flag_cache_get_error", name=name, error=str(e))
            return None
        if raw is None:
            return None
        if raw == _NEGATIVE_SENTINEL or raw == _NEGATIVE_SENTINEL.encode():
            return NEGATIVE_MISS
        # Pydantic model_validate_json parses bytes/str transparently.
        try:
            return FeatureFlagDoc.model_validate_json(raw)
        except Exception as e:
            log.warning("feature_flag_cache_decode_error", name=name, error=str(e))
            return None

    async def set(self, name: str, doc: FeatureFlagDoc) -> None:
        if self._redis is None:
            return
        try:
            await self._redis.setex(
                self._key(name),
                self.ttl_seconds,
                doc.model_dump_json(by_alias=True),
            )
        except Exception as e:
            log.error("feature_flag_cache_set_error", name=name, error=str(e))

    async def set_negative(self, name: str) -> None:
        """Cache a negative result (no doc found) with a shorter TTL."""
        if self._redis is None:
            return
        try:
            await self._redis.setex(
                self._key(name),
                self.negative_ttl_seconds,
                _NEGATIVE_SENTINEL,
            )
        except Exception as e:
            log.error("feature_flag_cache_set_negative_error", name=name, error=str(e))

    async def invalidate(self, name: str) -> None:
        """Drop a flag from cache. Used by admin scripts after manual edits.



        Not called from the request path — flips rely on TTL expiry.

        """
        if self._redis is None:
            return
        try:
            await self._redis.delete(self._key(name))
        except Exception as e:
            log.error("feature_flag_cache_invalidate_error", name=name, error=str(e))


class NegativeMiss:
    """Sentinel type for "cache says this flag does not exist".



    Distinct from ``None`` (real cache miss / no Redis). Service compares

    via ``is NEGATIVE_MISS`` to decide whether to skip the repo lookup.

    """


# Module-level singleton — `is` comparison decides "cached miss".
NEGATIVE_MISS = NegativeMiss()