Files
leocrm/app/core/cache.py
T
Agent Zero e9164979b5 feat(B-RED): Zentraler Redis Pool — cache.py, monitoring.py, worker.py auf get_redis() umgestellt
B-RED: 5 direkte aioredis.from_url() Konstruktoren auf get_redis() umgestellt
- cache.py: get_cache() delegiert auf get_redis(), _cache_redis Singleton entfernt
- monitoring.py: check_redis() und check_worker() nutzen get_redis()
- worker.py: _acquire_cron_lock() und _release_cron_lock() nutzen get_redis()
- 0 verbleibende direkte aioredis.from_url() außerhalb auth.py

B-RED-TEST: 8 Tests in test_redis_pool.py — alle grün
- Singleton, get_cache delegation, SET/GET, parallel, reset, no-direct-from_url checks
2026-08-13 16:25:17 +02:00

45 lines
1012 B
Python

"""Redis cache wrapper."""
from __future__ import annotations
import json
from typing import Any
import redis.asyncio as aioredis
from app.core.auth import get_redis
def get_cache() -> aioredis.Redis:
"""Get the cache Redis client (delegates to the global ``get_redis()`` singleton)."""
return get_redis()
async def cache_get(key: str) -> Any | None:
"""Get a value from cache."""
r = get_cache()
raw = await r.get(key)
if raw is None:
return None
return json.loads(raw)
async def cache_set(key: str, value: Any, ttl: int = 300) -> None:
"""Set a value in cache with TTL."""
r = get_cache()
await r.setex(key, ttl, json.dumps(value))
async def cache_delete(key: str) -> None:
"""Delete a key from cache."""
r = get_cache()
await r.delete(key)
async def cache_flush_pattern(pattern: str) -> None:
"""Delete all keys matching a pattern."""
r = get_cache()
keys = await r.keys(pattern)
if keys:
await r.delete(*keys)