fix(api): share operator summary cache through redis
Some checks failed
CD Pipeline / tests (push) Successful in 1m20s
Code Review / ai-code-review (push) Successful in 13s
CD Pipeline / build-and-deploy (push) Successful in 3m35s
CD Pipeline / post-deploy-checks (push) Failing after 20s

This commit is contained in:
Your Name
2026-06-01 09:38:08 +08:00
parent 8938706062
commit d4483e730e
4 changed files with 177 additions and 8 deletions

View File

@@ -1,12 +1,33 @@
from datetime import UTC, datetime
import pytest
from src.services.operator_summary_cache import (
clear_operator_summary_cache,
get_cached_operator_summary_async,
get_cached_operator_summary,
store_operator_summary_async,
store_operator_summary,
)
class _FakeRedis:
def __init__(self) -> None:
self.values: dict[str, str] = {}
self.expirations: dict[str, int] = {}
async def get(self, key: str) -> str | None:
return self.values.get(key)
async def set(self, key: str, value: str, ex: int) -> bool:
self.values[key] = value
self.expirations[key] = ex
return True
async def delete(self, key: str) -> int:
return 1 if self.values.pop(key, None) is not None else 0
def test_operator_summary_cache_returns_copy_with_hit_metadata() -> None:
clear_operator_summary_cache()
key = {"project_id": "awoooi", "limit": 30}
@@ -47,6 +68,38 @@ def test_operator_summary_cache_expires_by_ttl() -> None:
now_utc=datetime(2026, 6, 1, tzinfo=UTC),
)
@pytest.mark.asyncio
async def test_operator_summary_cache_uses_shared_redis(monkeypatch) -> None:
clear_operator_summary_cache()
fake_redis = _FakeRedis()
monkeypatch.setattr("src.core.redis_client.get_redis", lambda: fake_redis)
key = {"project_id": "awoooi", "per_page": 1}
stored = await store_operator_summary_async(
"callback_replies",
key,
{"total": 4, "items": []},
ttl_seconds=20,
now_monotonic=300.0,
now_utc=datetime(2026, 6, 1, tzinfo=UTC),
)
assert stored["cache"]["status"] == "miss"
assert fake_redis.values
clear_operator_summary_cache()
cached = await get_cached_operator_summary_async(
"callback_replies",
key,
ttl_seconds=20,
now_monotonic=302.0,
)
assert cached is not None
assert cached["cache"]["status"] == "hit"
assert cached["total"] == 4
assert (
get_cached_operator_summary(
"callback_replies",