39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
import asyncio
|
|
from typing import Dict, List
|
|
|
|
|
|
class OfflineAudioCache:
|
|
def __init__(self):
|
|
self.offline_audio: Dict[str, List[str]] = {}
|
|
self.lock = asyncio.Lock()
|
|
|
|
async def add_audio_url(self, device_id: str, audio_url: str):
|
|
async with self.lock:
|
|
if device_id not in self.offline_audio:
|
|
self.offline_audio[device_id] = []
|
|
self.offline_audio[device_id].append(audio_url)
|
|
|
|
async def get_audio_urls(self, device_id: str) -> List[str]:
|
|
async with self.lock:
|
|
return list(self.offline_audio.get(device_id, []))
|
|
|
|
async def pop_audio_urls(self, device_id: str) -> List[str]:
|
|
async with self.lock:
|
|
return self.offline_audio.pop(device_id, [])
|
|
|
|
async def clear_audio_urls(self, device_id: str):
|
|
async with self.lock:
|
|
if device_id in self.offline_audio:
|
|
del self.offline_audio[device_id]
|
|
|
|
async def has_pending_audio(self, device_id: str) -> bool:
|
|
async with self.lock:
|
|
return device_id in self.offline_audio and len(self.offline_audio[device_id]) > 0
|
|
|
|
async def get_all_audio_cache(self) -> List[str]:
|
|
async with self.lock:
|
|
return self.offline_audio
|
|
|
|
# 单例实例
|
|
offline_audio_cache = OfflineAudioCache()
|