mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-03-20 03:57:30 +08:00
上下文构建并非复杂任务, 移除缓存
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import ast, json, re
|
||||
from functools import wraps
|
||||
from typing import Any, Literal, Optional, List, Dict, Union
|
||||
|
||||
from cachetools import TTLCache
|
||||
@@ -14,79 +13,6 @@ from app.utils.string import StringUtils
|
||||
from app.log import logger
|
||||
|
||||
|
||||
# 缓存键字段白名单
|
||||
CACHE_KEY_FIELDS = ["title", "tmdb_id", "bangumi_id", "douban_id", "type", "name",\
|
||||
"subtitle", "description", "overview", "site_name", "file_list"]
|
||||
|
||||
|
||||
def extract_cache_key_values(obj: Any, field_names: List[str]) -> Dict[str, Any]:
|
||||
"""
|
||||
提取对象中指定字段的值(只保留存在的非空字段)
|
||||
:param obj: 目标对象
|
||||
:param field_names: 字段名列表(按优先级顺序)
|
||||
:return: 包含有效字段值的字典
|
||||
"""
|
||||
result = {}
|
||||
for field in field_names:
|
||||
if hasattr(obj, field):
|
||||
value = getattr(obj, field)
|
||||
if value is not None and value != "":
|
||||
if isinstance(value, (str, int, float)):
|
||||
result[field] = str(value)
|
||||
elif isinstance(value, list):
|
||||
result[field] = ",".join(map(str, value))
|
||||
return result
|
||||
|
||||
|
||||
def build_cache_key(*args, **kwargs) -> str:
|
||||
"""
|
||||
缓存键构建
|
||||
"""
|
||||
key_dict = {}
|
||||
|
||||
for i, arg in enumerate(args):
|
||||
if hasattr(arg, "__dict__"):
|
||||
key_dict[f"arg_{i}"] = extract_cache_key_values(arg, CACHE_KEY_FIELDS)
|
||||
elif isinstance(arg, list):
|
||||
key_dict[f"arg_{i}"] = {"list": [extract_cache_key_values(item, CACHE_KEY_FIELDS) for item in arg]}
|
||||
|
||||
for k, v in kwargs.items():
|
||||
if hasattr(v, "__dict__"):
|
||||
key_dict[k] = extract_cache_key_values(v, CACHE_KEY_FIELDS)
|
||||
|
||||
key_str = json.dumps(key_dict, sort_keys=True, ensure_ascii=False)
|
||||
return StringUtils.md5_hash(key_str)
|
||||
|
||||
def build_context_cache(cache_name: str = 'cache'):
|
||||
"""
|
||||
上下文缓存装饰器
|
||||
"""
|
||||
def decorator(func):
|
||||
@wraps(func)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
|
||||
# 获取缓存键
|
||||
cache_key = build_cache_key(*args, **kwargs)
|
||||
|
||||
# 获取缓存实例
|
||||
cache = getattr(self, cache_name)
|
||||
|
||||
if cache_key in cache:
|
||||
# 命中缓存,更新上下文
|
||||
self._context.update(cache[cache_key])
|
||||
return
|
||||
|
||||
# 执行原方法
|
||||
result = func(self, *args, **kwargs)
|
||||
|
||||
if isinstance(result, dict):
|
||||
cache[cache_key] = result
|
||||
self._context.update(result)
|
||||
return result
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
class TemplateHelper(metaclass=SingletonClass):
|
||||
"""
|
||||
模板格式渲染帮助类
|
||||
@@ -294,7 +220,6 @@ class TemplateContextBuilder:
|
||||
|
||||
return self._context
|
||||
|
||||
@build_context_cache(cache_name='cache')
|
||||
def _add_media_info(self, mediainfo: MediaInfo):
|
||||
"""
|
||||
增加媒体信息
|
||||
@@ -339,9 +264,8 @@ class TemplateContextBuilder:
|
||||
# 豆瓣ID
|
||||
"doubanid": mediainfo.douban_id,
|
||||
}
|
||||
return {**base_info, **media_info}
|
||||
self._context.update({**base_info, **media_info})
|
||||
|
||||
@build_context_cache(cache_name='cache')
|
||||
def _add_episode_details(self, meta: Optional[MetaBase], episodes: Optional[List[TmdbEpisode]]):
|
||||
"""添加剧集详细信息"""
|
||||
if not meta:
|
||||
@@ -398,9 +322,8 @@ class TemplateContextBuilder:
|
||||
# 音频编码
|
||||
"audioCodec": meta.audio_encode,
|
||||
}
|
||||
return {**meta_info, **tech_metadata, **episode_data}
|
||||
self._context.update({**meta_info, **tech_metadata, **episode_data})
|
||||
|
||||
@build_context_cache(cache_name='cache')
|
||||
def _add_torrent_info(self, torrentinfo: Optional[TorrentInfo]):
|
||||
if not torrentinfo: return
|
||||
if torrentinfo.size:
|
||||
@@ -436,9 +359,8 @@ class TemplateContextBuilder:
|
||||
# 种子大小
|
||||
"size": size,
|
||||
}
|
||||
return torrent_info
|
||||
self._context.update(torrent_info)
|
||||
|
||||
@build_context_cache(cache_name='cache')
|
||||
def _add_transfer_info(self, transferinfo: Optional[TransferInfo]) -> Optional[Dict]:
|
||||
"""添加文件转移上下文"""
|
||||
if not transferinfo: return
|
||||
@@ -457,9 +379,8 @@ class TemplateContextBuilder:
|
||||
# 文件后缀
|
||||
"fileExt": file_extension,
|
||||
}
|
||||
return file_info
|
||||
self._context.update(file_info)
|
||||
|
||||
@build_context_cache(cache_name='cache')
|
||||
def _add_raw_objects(
|
||||
self,
|
||||
meta: Optional[MetaBase],
|
||||
@@ -481,7 +402,7 @@ class TemplateContextBuilder:
|
||||
# 当前季的全部集信息
|
||||
"__episodes_info__": episodes_info,
|
||||
}
|
||||
return {k: v for k, v in raw_objects.items() if v is not None}
|
||||
self._context.update({k: v for k, v in raw_objects.items() if v is not None})
|
||||
|
||||
@staticmethod
|
||||
def __convert_invalid_characters(filename: str):
|
||||
|
||||
Reference in New Issue
Block a user