fix #951 不缓存网络错误导致的TMDB信息None

This commit is contained in:
jxxghp 2023-10-26 17:07:01 +08:00
parent 935ad73d32
commit 3bdd96a8ee
3 changed files with 25 additions and 4 deletions

View File

@ -144,7 +144,8 @@ class TmdbCache(metaclass=Singleton):
"backdrop_path": info.get("backdrop_path"), "backdrop_path": info.get("backdrop_path"),
CACHE_EXPIRE_TIMESTAMP_STR: int(time.time()) + EXPIRE_TIMESTAMP CACHE_EXPIRE_TIMESTAMP_STR: int(time.time()) + EXPIRE_TIMESTAMP
} }
else: elif info is not None:
# None时不缓存此时代表网络错误允许重复请求
self._meta_data[self.__get_key(meta)] = {'id': 0} self._meta_data[self.__get_key(meta)] = {'id': 0}
def save(self, force: bool = False) -> None: def save(self, force: bool = False) -> None:

View File

@ -4,11 +4,11 @@ import logging
import os import os
import time import time
from datetime import datetime from datetime import datetime
from functools import lru_cache
import requests import requests
import requests.exceptions import requests.exceptions
from app.utils.common import lru_cache_without_none
from app.utils.http import RequestUtils from app.utils.http import RequestUtils
from .exceptions import TMDbException from .exceptions import TMDbException
@ -137,11 +137,11 @@ class TMDb(object):
def cache(self, cache): def cache(self, cache):
os.environ[self.TMDB_CACHE_ENABLED] = str(cache) os.environ[self.TMDB_CACHE_ENABLED] = str(cache)
@lru_cache(maxsize=REQUEST_CACHE_MAXSIZE) @lru_cache_without_none(maxsize=REQUEST_CACHE_MAXSIZE)
def cached_request(self, method, url, data, json, def cached_request(self, method, url, data, json,
_ts=datetime.strftime(datetime.now(), '%Y%m%d')): _ts=datetime.strftime(datetime.now(), '%Y%m%d')):
""" """
缓存请求时间默认1天 缓存请求时间默认1天None不缓存
""" """
return self.request(method, url, data, json) return self.request(method, url, data, json)

View File

@ -1,5 +1,6 @@
import time import time
from typing import Any from typing import Any
from functools import lru_cache
def retry(ExceptionToCheck: Any, def retry(ExceptionToCheck: Any,
@ -32,3 +33,22 @@ def retry(ExceptionToCheck: Any,
return f_retry return f_retry
return deco_retry return deco_retry
def lru_cache_without_none(maxsize=None, typed=False):
"""
不缓存None的lru_cache
:param maxsize: 缓存大小
:param typed: 是否区分参数类型
"""
def decorator(func):
cache = lru_cache(maxsize=maxsize, typed=typed)(func)
def wrapper(*args, **kwargs):
result = cache(*args, **kwargs)
if result is not None:
return result
return wrapper
return decorator