feat(generator): add no-op cache wrapper (enables Redis later via env)

This commit is contained in:
shay
2026-04-17 16:44:38 -04:00
parent 361c938985
commit a0c5e8a86e
2 changed files with 52 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
"""Tiny cache wrapper. v1 = no-op (REDIS_URL empty). v2 = real Redis."""
from typing import Optional
from . import config
class Cache:
def __init__(self):
self.enabled = bool(config.REDIS_URL)
self._client = None
if self.enabled:
import redis # local import so v1 doesn't require redis in runtime path
self._client = redis.from_url(config.REDIS_URL, decode_responses=True)
def get(self, key: str) -> Optional[str]:
if not self.enabled:
return None
try:
return self._client.get(key)
except Exception:
return None
def set(self, key: str, value: str, ttl_seconds: int) -> None:
if not self.enabled:
return
try:
self._client.setex(key, ttl_seconds, value)
except Exception:
pass
def delete(self, *keys: str) -> None:
if not self.enabled or not keys:
return
try:
self._client.delete(*keys)
except Exception:
pass