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