Add LRC caching mechanism to optimize fetching and broadcasting of lyrics
This commit is contained in:
@@ -633,6 +633,18 @@ class Radio(FastAPI):
|
|||||||
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 = {
|
||||||
@@ -669,6 +681,28 @@ class Radio(FastAPI):
|
|||||||
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 = {
|
||||||
|
Reference in New Issue
Block a user