mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-06 16:07:01 +08:00
feat(cache): migrate cachetools usage to unified cache backend
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
import threading
|
import threading
|
||||||
from typing import List, Union, Optional, Generator
|
from typing import List, Union, Optional, Generator
|
||||||
|
|
||||||
from cachetools import cached, TTLCache
|
|
||||||
|
|
||||||
from app.chain import ChainBase
|
from app.chain import ChainBase
|
||||||
|
from app.core.cache import cached
|
||||||
from app.core.config import global_vars
|
from app.core.config import global_vars
|
||||||
from app.db.mediaserver_oper import MediaServerOper
|
from app.db.mediaserver_oper import MediaServerOper
|
||||||
from app.helper.service import ServiceConfigHelper
|
from app.helper.service import ServiceConfigHelper
|
||||||
@@ -94,7 +93,7 @@ class MediaServerChain(ChainBase):
|
|||||||
"""
|
"""
|
||||||
return self.run_module("mediaserver_latest", count=count, server=server, username=username)
|
return self.run_module("mediaserver_latest", count=count, server=server, username=username)
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=3600))
|
@cached(maxsize=1, ttl=3600)
|
||||||
def get_latest_wallpapers(self, server: str = None, count: int = 10,
|
def get_latest_wallpapers(self, server: str = None, count: int = 10,
|
||||||
remote: bool = True, username: str = None) -> List[str]:
|
remote: bool = True, username: str = None) -> List[str]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
+3
-4
@@ -1,10 +1,9 @@
|
|||||||
import random
|
import random
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
from cachetools import cached, TTLCache
|
|
||||||
|
|
||||||
from app import schemas
|
from app import schemas
|
||||||
from app.chain import ChainBase
|
from app.chain import ChainBase
|
||||||
|
from app.core.cache import cached
|
||||||
from app.core.context import MediaInfo
|
from app.core.context import MediaInfo
|
||||||
from app.schemas import MediaType
|
from app.schemas import MediaType
|
||||||
from app.utils.singleton import Singleton
|
from app.utils.singleton import Singleton
|
||||||
@@ -119,7 +118,7 @@ class TmdbChain(ChainBase, metaclass=Singleton):
|
|||||||
"""
|
"""
|
||||||
return self.run_module("tmdb_person_credits", person_id=person_id, page=page)
|
return self.run_module("tmdb_person_credits", person_id=person_id, page=page)
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=3600))
|
@cached(maxsize=1, ttl=3600)
|
||||||
def get_random_wallpager(self) -> Optional[str]:
|
def get_random_wallpager(self) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
获取随机壁纸,缓存1个小时
|
获取随机壁纸,缓存1个小时
|
||||||
@@ -133,7 +132,7 @@ class TmdbChain(ChainBase, metaclass=Singleton):
|
|||||||
return info.backdrop_path
|
return info.backdrop_path
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=3600))
|
@cached(maxsize=1, ttl=3600)
|
||||||
def get_trending_wallpapers(self, num: int = 10) -> List[str]:
|
def get_trending_wallpapers(self, num: int = 10) -> List[str]:
|
||||||
"""
|
"""
|
||||||
获取所有流行壁纸
|
获取所有流行壁纸
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from cachetools import TTLCache, cached
|
|
||||||
|
|
||||||
|
from app.core.cache import cached
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.utils.http import RequestUtils
|
from app.utils.http import RequestUtils
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ class BangumiApi(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@cached(cache=TTLCache(maxsize=settings.CACHE_CONF["bangumi"], ttl=settings.CACHE_CONF["meta"]))
|
@cached(maxsize=settings.CACHE_CONF["bangumi"], ttl=settings.CACHE_CONF["meta"])
|
||||||
def __invoke(cls, url, **kwargs):
|
def __invoke(cls, url, **kwargs):
|
||||||
req_url = cls._base_url + url
|
req_url = cls._base_url + url
|
||||||
params = {}
|
params = {}
|
||||||
@@ -188,7 +188,8 @@ class BangumiApi(object):
|
|||||||
获取人物参演作品
|
获取人物参演作品
|
||||||
"""
|
"""
|
||||||
ret_list = []
|
ret_list = []
|
||||||
result = self.__invoke(self._urls["person_credits"] % person_id, _ts=datetime.strftime(datetime.now(), '%Y%m%d'))
|
result = self.__invoke(self._urls["person_credits"] % person_id,
|
||||||
|
_ts=datetime.strftime(datetime.now(), '%Y%m%d'))
|
||||||
if result:
|
if result:
|
||||||
for item in result:
|
for item in result:
|
||||||
ret_list.append(item)
|
ret_list.append(item)
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ from random import choice
|
|||||||
from urllib import parse
|
from urllib import parse
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from cachetools import TTLCache, cached
|
|
||||||
|
|
||||||
|
from app.core.cache import cached
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.utils.http import RequestUtils
|
from app.utils.http import RequestUtils
|
||||||
from app.utils.singleton import Singleton
|
from app.utils.singleton import Singleton
|
||||||
@@ -174,14 +174,14 @@ class DoubanApi(metaclass=Singleton):
|
|||||||
).digest()
|
).digest()
|
||||||
).decode()
|
).decode()
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=settings.CACHE_CONF["douban"], ttl=settings.CACHE_CONF["meta"]))
|
@cached(maxsize=settings.CACHE_CONF["douban"], ttl=settings.CACHE_CONF["meta"])
|
||||||
def __invoke_recommend(self, url: str, **kwargs) -> dict:
|
def __invoke_recommend(self, url: str, **kwargs) -> dict:
|
||||||
"""
|
"""
|
||||||
推荐/发现类API
|
推荐/发现类API
|
||||||
"""
|
"""
|
||||||
return self.__invoke(url, **kwargs)
|
return self.__invoke(url, **kwargs)
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=settings.CACHE_CONF["douban"], ttl=settings.CACHE_CONF["meta"]))
|
@cached(maxsize=settings.CACHE_CONF["douban"], ttl=settings.CACHE_CONF["meta"])
|
||||||
def __invoke_search(self, url: str, **kwargs) -> dict:
|
def __invoke_search(self, url: str, **kwargs) -> dict:
|
||||||
"""
|
"""
|
||||||
搜索类API
|
搜索类API
|
||||||
@@ -216,7 +216,7 @@ class DoubanApi(metaclass=Singleton):
|
|||||||
return resp.json()
|
return resp.json()
|
||||||
return resp.json() if resp else {}
|
return resp.json() if resp else {}
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=settings.CACHE_CONF["douban"], ttl=settings.CACHE_CONF["meta"]))
|
@cached(maxsize=settings.CACHE_CONF["douban"], ttl=settings.CACHE_CONF["meta"])
|
||||||
def __post(self, url: str, **kwargs) -> dict:
|
def __post(self, url: str, **kwargs) -> dict:
|
||||||
"""
|
"""
|
||||||
POST请求
|
POST请求
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
from typing import Optional, Tuple, Union
|
from typing import Optional, Tuple, Union
|
||||||
|
|
||||||
from cachetools import TTLCache, cached
|
from app.core.cache import cached
|
||||||
|
|
||||||
from app.core.context import MediaInfo, settings
|
from app.core.context import MediaInfo, settings
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
from app.modules import _ModuleBase
|
from app.modules import _ModuleBase
|
||||||
@@ -11,7 +10,6 @@ from app.utils.http import RequestUtils
|
|||||||
|
|
||||||
|
|
||||||
class FanartModule(_ModuleBase):
|
class FanartModule(_ModuleBase):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
{
|
{
|
||||||
"name": "The Wheel of Time",
|
"name": "The Wheel of Time",
|
||||||
@@ -422,7 +420,7 @@ class FanartModule(_ModuleBase):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@cached(cache=TTLCache(maxsize=settings.CACHE_CONF["fanart"], ttl=settings.CACHE_CONF["meta"]))
|
@cached(maxsize=settings.CACHE_CONF["fanart"], ttl=settings.CACHE_CONF["meta"])
|
||||||
def __request_fanart(cls, media_type: MediaType, queryid: Union[str, int]) -> Optional[dict]:
|
def __request_fanart(cls, media_type: MediaType, queryid: Union[str, int]) -> Optional[dict]:
|
||||||
if media_type == MediaType.MOVIE:
|
if media_type == MediaType.MOVIE:
|
||||||
image_url = cls._movie_url % queryid
|
image_url = cls._movie_url % queryid
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ from datetime import datetime
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, List, Dict
|
from typing import Optional, List, Dict
|
||||||
|
|
||||||
from cachetools import cached, TTLCache
|
|
||||||
from requests import Response
|
from requests import Response
|
||||||
|
|
||||||
from app import schemas
|
from app import schemas
|
||||||
|
from app.core.cache import cached
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
from app.modules.filemanager.storages import StorageBase
|
from app.modules.filemanager.storages import StorageBase
|
||||||
@@ -67,7 +67,7 @@ class Alist(StorageBase, metaclass=Singleton):
|
|||||||
return self.__generate_token
|
return self.__generate_token
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=60 * 60 * 24 * 2 - 60 * 5))
|
@cached(maxsize=1, ttl=60 * 60 * 24 * 2 - 60 * 5)
|
||||||
def __generate_token(self) -> str:
|
def __generate_token(self) -> str:
|
||||||
"""
|
"""
|
||||||
使用账号密码生成一个临时token
|
使用账号密码生成一个临时token
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ from pathlib import Path
|
|||||||
from typing import List, Optional, Dict, Tuple, Generator, Any, Union
|
from typing import List, Optional, Dict, Tuple, Generator, Any, Union
|
||||||
from urllib.parse import quote_plus
|
from urllib.parse import quote_plus
|
||||||
|
|
||||||
from cachetools import TTLCache, cached
|
|
||||||
from plexapi import media
|
from plexapi import media
|
||||||
from plexapi.myplex import MyPlexAccount
|
from plexapi.myplex import MyPlexAccount
|
||||||
from plexapi.server import PlexServer
|
from plexapi.server import PlexServer
|
||||||
from requests import Response, Session
|
from requests import Response, Session
|
||||||
|
|
||||||
from app import schemas
|
from app import schemas
|
||||||
|
from app.core.cache import cached
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
from app.schemas import MediaType
|
from app.schemas import MediaType
|
||||||
from app.utils.http import RequestUtils
|
from app.utils.http import RequestUtils
|
||||||
@@ -83,7 +83,7 @@ class Plex:
|
|||||||
logger.error(f"Authentication failed: {e}")
|
logger.error(f"Authentication failed: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=100, ttl=86400))
|
@cached(maxsize=100, ttl=86400)
|
||||||
def __get_library_images(self, library_key: str, mtype: int) -> Optional[List[str]]:
|
def __get_library_images(self, library_key: str, mtype: int) -> Optional[List[str]]:
|
||||||
"""
|
"""
|
||||||
获取媒体服务器最近添加的媒体的图片列表
|
获取媒体服务器最近添加的媒体的图片列表
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ from typing import Optional, List
|
|||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
import zhconv
|
import zhconv
|
||||||
from cachetools import TTLCache, cached
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
|
from app.core.cache import cached
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
from app.schemas.types import MediaType
|
from app.schemas.types import MediaType
|
||||||
@@ -491,7 +491,7 @@ class TmdbApi:
|
|||||||
|
|
||||||
return ret_info
|
return ret_info
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=settings.CACHE_CONF["tmdb"], ttl=settings.CACHE_CONF["meta"]))
|
@cached(maxsize=settings.CACHE_CONF["tmdb"], ttl=settings.CACHE_CONF["meta"])
|
||||||
def match_web(self, name: str, mtype: MediaType) -> Optional[dict]:
|
def match_web(self, name: str, mtype: MediaType) -> Optional[dict]:
|
||||||
"""
|
"""
|
||||||
搜索TMDB网站,直接抓取结果,结果只有一条时才返回
|
搜索TMDB网站,直接抓取结果,结果只有一条时才返回
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
from app.core.cache import cached
|
||||||
from ..tmdb import TMDb
|
from ..tmdb import TMDb
|
||||||
from cachetools import cached, TTLCache
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
@@ -13,7 +13,7 @@ class Discover(TMDb):
|
|||||||
"tv": "/discover/tv"
|
"tv": "/discover/tv"
|
||||||
}
|
}
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=43200))
|
@cached(maxsize=1, ttl=43200)
|
||||||
def discover_movies(self, params_tuple):
|
def discover_movies(self, params_tuple):
|
||||||
"""
|
"""
|
||||||
Discover movies by different types of data like average rating, number of votes, genres and certifications.
|
Discover movies by different types of data like average rating, number of votes, genres and certifications.
|
||||||
@@ -23,7 +23,7 @@ class Discover(TMDb):
|
|||||||
params = dict(params_tuple)
|
params = dict(params_tuple)
|
||||||
return self._request_obj(self._urls["movies"], urlencode(params), key="results", call_cached=False)
|
return self._request_obj(self._urls["movies"], urlencode(params), key="results", call_cached=False)
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=43200))
|
@cached(maxsize=1, ttl=43200)
|
||||||
def discover_tv_shows(self, params_tuple):
|
def discover_tv_shows(self, params_tuple):
|
||||||
"""
|
"""
|
||||||
Discover TV shows by different types of data like average rating, number of votes, genres,
|
Discover TV shows by different types of data like average rating, number of votes, genres,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from cachetools import cached, TTLCache
|
from app.core.cache import cached
|
||||||
|
|
||||||
from ..tmdb import TMDb
|
from ..tmdb import TMDb
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@ from ..tmdb import TMDb
|
|||||||
class Trending(TMDb):
|
class Trending(TMDb):
|
||||||
_urls = {"trending": "/trending/%s/%s"}
|
_urls = {"trending": "/trending/%s/%s"}
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=43200))
|
@cached(maxsize=1, ttl=43200)
|
||||||
def _trending(self, media_type="all", time_window="day", page=1):
|
def _trending(self, media_type="all", time_window="day", page=1):
|
||||||
"""
|
"""
|
||||||
Get trending, TTLCache 12 hours
|
Get trending, TTLCache 12 hours
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ from datetime import datetime
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
import requests.exceptions
|
import requests.exceptions
|
||||||
from cachetools import TTLCache, cached
|
|
||||||
|
|
||||||
|
from app.core.cache import cached
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.utils.http import RequestUtils
|
from app.utils.http import RequestUtils
|
||||||
from .exceptions import TMDbException
|
from .exceptions import TMDbException
|
||||||
@@ -137,7 +137,7 @@ 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)
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=settings.CACHE_CONF["tmdb"], ttl=settings.CACHE_CONF["meta"]))
|
@cached(maxsize=settings.CACHE_CONF["tmdb"], ttl=settings.CACHE_CONF["meta"])
|
||||||
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')):
|
||||||
"""
|
"""
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
from cachetools import TTLCache, cached
|
from app.core.cache import cached
|
||||||
|
|
||||||
from app.utils.http import RequestUtils
|
from app.utils.http import RequestUtils
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ class WebUtils:
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=3600))
|
@cached(maxsize=1, ttl=3600)
|
||||||
def get_bing_wallpaper() -> Optional[str]:
|
def get_bing_wallpaper() -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
获取Bing每日壁纸
|
获取Bing每日壁纸
|
||||||
@@ -93,7 +93,7 @@ class WebUtils:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=3600))
|
@cached(maxsize=1, ttl=3600)
|
||||||
def get_bing_wallpapers(num: int = 7) -> List[str]:
|
def get_bing_wallpapers(num: int = 7) -> List[str]:
|
||||||
"""
|
"""
|
||||||
获取7天的Bing每日壁纸
|
获取7天的Bing每日壁纸
|
||||||
|
|||||||
@@ -65,3 +65,4 @@ aligo~=6.2.4
|
|||||||
aiofiles~=24.1.0
|
aiofiles~=24.1.0
|
||||||
jieba~=0.42.1
|
jieba~=0.42.1
|
||||||
rsa~=4.9
|
rsa~=4.9
|
||||||
|
redis~=5.2.1
|
||||||
Reference in New Issue
Block a user