From 84e62246b70b8afdd8e2928c7e5e64a855632af9 Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Tue, 26 Aug 2025 16:18:04 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(backend)=20add=20lobby=20cache=20clea?= =?UTF-8?q?ring=20method=20for=20room=20and=20participant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce new method on lobby system to clear lobby cache for specific room and participant combinations. Enables targeted cleanup of lobby state when participants leave or are removed, improving cache management and preventing stale lobby entries. --- src/backend/core/services/lobby.py | 6 ++++ src/backend/core/tests/services/test_lobby.py | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/backend/core/services/lobby.py b/src/backend/core/services/lobby.py index bce85eae..55fab912 100644 --- a/src/backend/core/services/lobby.py +++ b/src/backend/core/services/lobby.py @@ -342,3 +342,9 @@ class LobbyService: return cache.delete_many(keys) + + def clear_participant_cache(self, room_id: UUID, participant_id: str) -> None: + """Clear a given participant entry from the cache for a specific room.""" + + cache_key = self._get_cache_key(room_id, participant_id) + cache.delete(cache_key) diff --git a/src/backend/core/tests/services/test_lobby.py b/src/backend/core/tests/services/test_lobby.py index 600d3220..43f745de 100644 --- a/src/backend/core/tests/services/test_lobby.py +++ b/src/backend/core/tests/services/test_lobby.py @@ -839,3 +839,35 @@ def test_clear_room_empty(settings, lobby_service): assert cache.keys(f"test-lobby_{room_id!s}_*") == [] lobby_service.clear_room_cache(room_id) assert cache.keys(f"test-lobby_{room_id!s}_*") == [] + + +def test_clear_participant_cache(lobby_service): + """Test clearing a specific participant entry from cache.""" + room_id = uuid.uuid4() + participant_id = "test-participant-id" + + cache_key = f"{settings.LOBBY_KEY_PREFIX}_{room_id!s}_{participant_id}" + participant_data = { + "status": "waiting", + "username": "test-username", + "id": participant_id, + "color": "#123456", + } + cache.set(cache_key, participant_data, timeout=settings.LOBBY_WAITING_TIMEOUT) + assert cache.get(cache_key) is not None + + lobby_service.clear_participant_cache(room_id, participant_id) + assert cache.get(cache_key) is None + + +def test_clear_participant_cache_nonexistent(lobby_service): + """Test clearing a participant that doesn't exist in cache.""" + room_id = uuid.uuid4() + participant_id = "nonexistent-participant" + + cache_key = f"{settings.LOBBY_KEY_PREFIX}_{room_id!s}_{participant_id}" + assert cache.get(cache_key) is None + + lobby_service.clear_participant_cache(room_id, participant_id) + + assert cache.get(cache_key) is None