Add LRC caching mechanism to optimize fetching and broadcasting of lyrics
This commit is contained in:
@@ -632,7 +632,19 @@ class Radio(FastAPI):
|
|||||||
artist: Optional[str] = track_data.get("artist")
|
artist: Optional[str] = track_data.get("artist")
|
||||||
title: Optional[str] = track_data.get("song") # Changed from "title" to "song"
|
title: Optional[str] = track_data.get("song") # Changed from "title" to "song"
|
||||||
duration: Optional[int] = track_data.get("duration")
|
duration: Optional[int] = track_data.get("duration")
|
||||||
|
|
||||||
|
# Check if LRC is already cached
|
||||||
|
cached_lrc = self.lrc_cache.get(station)
|
||||||
|
if cached_lrc:
|
||||||
|
logging.info("LRC found in cache, sending immediately.")
|
||||||
|
lrc_data: dict = {
|
||||||
|
"type": "lrc",
|
||||||
|
"data": cached_lrc,
|
||||||
|
"source": "Cache"
|
||||||
|
}
|
||||||
|
await websocket.send_text(json.dumps(lrc_data))
|
||||||
|
return
|
||||||
|
|
||||||
if artist and title:
|
if artist and title:
|
||||||
logging.info(f"Fetching LRC for {artist} - {title} (duration: {duration})")
|
logging.info(f"Fetching LRC for {artist} - {title} (duration: {duration})")
|
||||||
lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song(
|
lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song(
|
||||||
@@ -646,7 +658,7 @@ class Radio(FastAPI):
|
|||||||
lrc = lrclib_result.lyrics
|
lrc = lrclib_result.lyrics
|
||||||
source = "LRCLib"
|
source = "LRCLib"
|
||||||
logging.info("LRC found via LRCLib fallback")
|
logging.info("LRC found via LRCLib fallback")
|
||||||
self.lrc_cache[station] = lrc
|
self.lrc_cache[station] = lrc # Cache the LRC for future use
|
||||||
logging.info(f"LRC fetched: {lrc is not None}")
|
logging.info(f"LRC fetched: {lrc is not None}")
|
||||||
if lrc:
|
if lrc:
|
||||||
lrc_data: dict = {
|
lrc_data: dict = {
|
||||||
@@ -663,12 +675,34 @@ class Radio(FastAPI):
|
|||||||
"""Broadcast LRC data to all connected clients for a station asynchronously."""
|
"""Broadcast LRC data to all connected clients for a station asynchronously."""
|
||||||
if station not in self.active_connections:
|
if station not in self.active_connections:
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
artist: Optional[str] = track_data.get("artist")
|
artist: Optional[str] = track_data.get("artist")
|
||||||
title: Optional[str] = track_data.get("song") # Changed from "title" to "song"
|
title: Optional[str] = track_data.get("song") # Changed from "title" to "song"
|
||||||
duration: Optional[int] = track_data.get("duration")
|
duration: Optional[int] = track_data.get("duration")
|
||||||
|
|
||||||
|
# Check if LRC is already cached
|
||||||
|
cached_lrc = self.lrc_cache.get(station)
|
||||||
|
if cached_lrc:
|
||||||
|
logging.info("LRC found in cache, broadcasting immediately.")
|
||||||
|
lrc_data: dict = {
|
||||||
|
"type": "lrc",
|
||||||
|
"data": cached_lrc,
|
||||||
|
"source": "Cache"
|
||||||
|
}
|
||||||
|
disconnected_clients = set()
|
||||||
|
for websocket in self.active_connections[station]:
|
||||||
|
try:
|
||||||
|
await websocket.send_text(json.dumps(lrc_data))
|
||||||
|
except Exception as e:
|
||||||
|
logging.warning(f"Failed to send LRC to client: {e}")
|
||||||
|
disconnected_clients.add(websocket)
|
||||||
|
|
||||||
|
# Remove failed connections
|
||||||
|
for websocket in disconnected_clients:
|
||||||
|
self.active_connections[station].discard(websocket)
|
||||||
|
return
|
||||||
|
|
||||||
if artist and title:
|
if artist and title:
|
||||||
logging.info(f"Broadcasting LRC fetch for {artist} - {title} (duration: {duration})")
|
logging.info(f"Broadcasting LRC fetch for {artist} - {title} (duration: {duration})")
|
||||||
lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song(
|
lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song(
|
||||||
@@ -682,7 +716,7 @@ class Radio(FastAPI):
|
|||||||
lrc = lrclib_result.lyrics
|
lrc = lrclib_result.lyrics
|
||||||
source = "LRCLib"
|
source = "LRCLib"
|
||||||
logging.info("LRC found via LRCLib fallback")
|
logging.info("LRC found via LRCLib fallback")
|
||||||
self.lrc_cache[station] = lrc
|
self.lrc_cache[station] = lrc # Cache the LRC for future use
|
||||||
logging.info(f"LRC fetched for broadcast: {lrc is not None}")
|
logging.info(f"LRC fetched for broadcast: {lrc is not None}")
|
||||||
if lrc:
|
if lrc:
|
||||||
lrc_data: dict = {
|
lrc_data: dict = {
|
||||||
@@ -690,7 +724,7 @@ class Radio(FastAPI):
|
|||||||
"data": lrc,
|
"data": lrc,
|
||||||
"source": source
|
"source": source
|
||||||
}
|
}
|
||||||
|
|
||||||
# Send to all connected clients
|
# Send to all connected clients
|
||||||
disconnected_clients = set()
|
disconnected_clients = set()
|
||||||
for websocket in self.active_connections[station]:
|
for websocket in self.active_connections[station]:
|
||||||
@@ -699,7 +733,7 @@ class Radio(FastAPI):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning(f"Failed to send LRC to client: {e}")
|
logging.warning(f"Failed to send LRC to client: {e}")
|
||||||
disconnected_clients.add(websocket)
|
disconnected_clients.add(websocket)
|
||||||
|
|
||||||
# Remove failed connections
|
# Remove failed connections
|
||||||
for websocket in disconnected_clients:
|
for websocket in disconnected_clients:
|
||||||
self.active_connections[station].discard(websocket)
|
self.active_connections[station].discard(websocket)
|
||||||
|
Reference in New Issue
Block a user