Files
MoviePilot/app/modules/musicbrainz/cache.py

255 lines
9.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import hashlib
import json
import pickle
import traceback
from math import ceil
from threading import RLock
from time import time
from typing import Optional
from app.domain.context import MusicInfo
from app.domain.meta.metamusic import MetaMusic, music_credit_values
from app.domain.music import music_isrc_codes
from app.foundation.singleton import WeakSingleton
from app.runtime.cache import FileCache, TTLCache
from app.runtime.log import logger
from app.runtime.settings import get_runtime_setting
from app.schemas.types import MUSIC_ENTITY_RECORDING
lock = RLock()
# 发行证据和故障语义已改变,不能继续复用旧规则身份或旧的长时间负缓存。
PERSISTENCE_VERSION = 4
PERSISTENCE_REGION = "recognize"
PERSISTENCE_KEY = "musicbrainz"
class MusicBrainzCache(metaclass=WeakSingleton):
"""
MusicBrainz识别缓存数据
{
"source": '',
"media_id": '',
"title": '',
"artists": [],
"album": '',
"year": '',
"music_type": ''
}
"""
def __init__(self):
"""初始化音乐识别缓存并恢复未过期的持久化数据。"""
self.maxsize = get_runtime_setting('CONF').musicbrainz
self.ttl = get_runtime_setting('CONF').meta
self.region = f"__musicbrainz_cache_v{PERSISTENCE_VERSION}__"
self._cache = TTLCache(region=self.region, maxsize=self.maxsize, ttl=self.ttl)
self._expires_at: dict[str, float] = {}
self._dirty = False
self._file_cache = None
if not self._cache.is_redis():
self._file_cache = FileCache(base=get_runtime_setting('CACHE_PATH'), ttl=self.ttl)
self._restore()
def _restore(self) -> None:
"""从统一文件缓存恢复仍在有效期内的音乐识别数据。"""
try:
content = self._file_cache.get(PERSISTENCE_KEY, region=PERSISTENCE_REGION)
if not content:
return
payload = pickle.loads(content)
now = time()
if (
not isinstance(payload, dict)
or payload.get("version") != PERSISTENCE_VERSION
or not isinstance(payload.get("items"), dict)
):
return
for key, item in payload["items"].items():
if not isinstance(item, dict):
self._dirty = True
continue
value = item.get("value")
expires_at = item.get("expires_at")
if not isinstance(value, dict) or not isinstance(expires_at, (int, float)):
self._dirty = True
continue
remaining_ttl = expires_at - now
if remaining_ttl <= 0:
self._dirty = True
continue
self._cache.set(key, value, ttl=ceil(remaining_ttl))
self._expires_at[key] = expires_at
except Exception as err:
logger.error(f"加载音乐识别缓存失败:{str(err)} - {traceback.format_exc()}")
def _set(self, key: str, value: dict) -> None:
"""无身份结果仅短暂缓存,不能让一次无匹配覆盖普通媒体缓存的整个有效期。"""
ttl = self.ttl if value.get("media_id") else min(self.ttl, 300)
self._cache.set(key, value, ttl=ttl)
if not self._cache.is_redis():
self._expires_at[key] = time() + ttl
self._dirty = True
def clear(self):
"""
清空所有音乐识别缓存
"""
with lock:
self._cache.clear()
self._expires_at.clear()
self._dirty = True
self.save(force=True)
def list_items(self) -> list[dict]:
"""
返回可供管理界面展示的音乐识别缓存列表。
"""
with lock:
cache_items = []
for key, value in self._cache.items():
if not isinstance(value, dict):
continue
cache_items.append({
"key": key,
"media_id": value.get("media_id") or "",
"title": value.get("title") or "",
"artists": value.get("artists") or [],
"album": value.get("album") or "",
"year": value.get("year") or "",
"music_type": value.get("music_type") or MUSIC_ENTITY_RECORDING,
"cover_url": value.get("cover_url") or "",
})
return sorted(cache_items, key=lambda item: item["key"])
@staticmethod
def __get_key(meta: MetaMusic, music_type: Optional[str] = None) -> str:
"""按来源身份或完整识别证据及请求实体编码键,避免版本串用和分隔符碰撞。"""
source = str(meta.media_source) if meta.media_source else None
identity: list[object]
if source and meta.media_id:
identity = ["id", source, str(meta.media_id)]
else:
identity = ["meta", source, meta.title, list(meta.artists or []), meta.album,
meta.year, meta.version, meta.isrc]
credits = {key: value for key, value in music_credit_values(meta).items() if value}
if credits:
identity.append(credits)
payload = json.dumps([music_type, identity], ensure_ascii=False, separators=(",", ":"))
return f"[音乐:v{PERSISTENCE_VERSION}]{hashlib.sha256(payload.encode('utf-8')).hexdigest()}"
def get(self, meta: MetaMusic, music_type: Optional[str] = None) -> Optional[MusicInfo]:
"""
根据元数据获取缓存的音乐识别结果
@param meta: 音乐元数据
@param music_type: 本次请求的实体范围,未指定时与显式单曲、专辑请求隔离
@return: 缓存命中的音乐信息,未命中返回 None
"""
key = self.__get_key(meta, music_type)
with lock:
cache_data = self._cache.get(key)
if not cache_data and self._expires_at.pop(key, None) is not None:
self._dirty = True
if not cache_data:
return None
try:
return MusicInfo.from_dict(cache_data)
except Exception as err:
logger.error(f"解析音乐识别缓存失败:{str(err)}")
return None
def delete(self, key: str) -> dict:
"""
删除缓存信息
@param key: 缓存key
@return: 被删除的缓存内容
"""
with lock:
cache_data = self._cache.get(key)
if cache_data:
self._cache.delete(key)
self._expires_at.pop(key, None)
self._dirty = True
self.save(force=True)
return cache_data
return {}
def update(self, meta: MetaMusic, info: Optional[MusicInfo], music_type: Optional[str] = None) -> None:
"""
新增或更新缓存条目,无远端身份的兜底结果也写入内存负缓存,
避免批量识别时反复请求 MusicBrainz 触发限流
"""
if not meta or not info:
return
if not info.media_id and (info.raw_data.get("recognition") or {}).get("status") in {"service_error", "budget_exhausted"}:
return
key = self.__get_key(meta, music_type)
cache_data = info.to_dict()
# 原始响应不入缓存,只保留核验录音身份必需的有效ISRC集合。
cache_data.pop("raw_data", None)
codes = music_isrc_codes(info)
if codes:
cache_data["raw_data"] = {"isrcs": sorted(codes)}
with lock:
self._set(key, cache_data)
def save(self, force: bool = False) -> None:
"""
使用统一文件缓存保存未过期的音乐识别数据。
"""
if self._cache.is_redis():
return
if not self._file_cache:
return
with lock:
now = time()
cache_items = dict(self._cache.items())
active_keys = set(cache_items)
stale_keys = set(self._expires_at) - active_keys
if stale_keys:
for key in stale_keys:
self._expires_at.pop(key, None)
self._dirty = True
persisted_items = {}
for key, value in cache_items.items():
expires_at = self._expires_at.get(key)
if expires_at is None:
expires_at = now + self.ttl
self._expires_at[key] = expires_at
self._dirty = True
# 负缓存只留在内存,重启后允许重新尝试识别
if expires_at <= now or not value.get("media_id"):
continue
persisted_items[key] = {
"value": value,
"expires_at": expires_at,
}
if not force and not self._dirty:
return
try:
if persisted_items:
payload = {
"version": PERSISTENCE_VERSION,
"items": persisted_items,
}
self._file_cache.set(
PERSISTENCE_KEY,
pickle.dumps(payload, pickle.HIGHEST_PROTOCOL),
region=PERSISTENCE_REGION,
)
else:
self._file_cache.delete(PERSISTENCE_KEY, region=PERSISTENCE_REGION)
self._dirty = False
except Exception as err:
logger.error(f"保存音乐识别缓存失败:{str(err)} - {traceback.format_exc()}")
def __del__(self):
"""实例释放前保存非 Redis 缓存。"""
try:
self.save()
except Exception:
pass