perf(tmdb): 目录名 tmdbid 命中改为每进程只记一次,并加进程内实体缓存
- 原来每集都打一行「⚡ TMDB 命中(目录名 tmdbid)」,且每次都重新读取
并解析 320KB 的 tmdb_data.json(snapshot 加锁),40 集就是 40 次
- 现在同一 id 在本进程内只记一次日志;实体本身也缓存,后续集数直接复用
- 实测:我的三体 40 集 → 1 行;苍兰诀动漫 50 集 → 1 行
This commit is contained in:
@@ -19,6 +19,8 @@ class TMDBService:
|
|||||||
# 失败结果不记住的话,一个 60 集的剧会白跑 180 次请求。
|
# 失败结果不记住的话,一个 60 集的剧会白跑 180 次请求。
|
||||||
_dead_ids = set() # 详情 404 的 tmdb id
|
_dead_ids = set() # 详情 404 的 tmdb id
|
||||||
_failed_searches = set() # (类型, 归一化标题, 年份) 搜索无结果
|
_failed_searches = set() # (类型, 归一化标题, 年份) 搜索无结果
|
||||||
|
_id_hits_notified = set() # 目录名 tmdbid 命中:每个 id 只记一次日志
|
||||||
|
_id_entity_cache = {} # 进程内实体缓存,避免每集重读缓存文件
|
||||||
|
|
||||||
# ------------------------------------------------------------ HTTP
|
# ------------------------------------------------------------ HTTP
|
||||||
|
|
||||||
@@ -114,10 +116,20 @@ class TMDBService:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def _from_id(cls, path, mtype, tmdb_id, title, year_s):
|
def _from_id(cls, path, mtype, tmdb_id, title, year_s):
|
||||||
key = store.entity_key(mtype, tmdb_id)
|
key = store.entity_key(mtype, tmdb_id)
|
||||||
|
|
||||||
|
# 同一目录的每一集都会走这里,进程内记住即可:
|
||||||
|
# 既不用每集重读 320KB 的缓存文件,日志也只留一行。
|
||||||
|
cached = cls._id_entity_cache.get(key)
|
||||||
|
if cached is not None:
|
||||||
|
return cached
|
||||||
|
|
||||||
db = store.snapshot(path)
|
db = store.snapshot(path)
|
||||||
entity = db["entities"].get(key)
|
entity = db["entities"].get(key)
|
||||||
if entity is not None:
|
if entity is not None:
|
||||||
|
if key not in cls._id_hits_notified:
|
||||||
log.debug(f"⚡ TMDB 命中(目录名 tmdbid): {key}")
|
log.debug(f"⚡ TMDB 命中(目录名 tmdbid): {key}")
|
||||||
|
cls._id_hits_notified.add(key)
|
||||||
|
cls._id_entity_cache[key] = entity
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
session = cls._get_session()
|
session = cls._get_session()
|
||||||
@@ -126,6 +138,7 @@ class TMDBService:
|
|||||||
return None
|
return None
|
||||||
entity = store.build_entity(mtype, tmdb_id, payload)
|
entity = store.build_entity(mtype, tmdb_id, payload)
|
||||||
cls._save(path, [entity], title, year_s, chosen=entity)
|
cls._save(path, [entity], title, year_s, chosen=entity)
|
||||||
|
cls._id_entity_cache[key] = entity
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user