diff --git a/app/chain/mediaserver.py b/app/chain/mediaserver.py index 07dba726..ead78f89 100644 --- a/app/chain/mediaserver.py +++ b/app/chain/mediaserver.py @@ -1,9 +1,8 @@ import threading from typing import List, Union, Optional, Generator -from cachetools import cached, TTLCache - from app.chain import ChainBase +from app.core.cache import cached from app.core.config import global_vars from app.db.mediaserver_oper import MediaServerOper 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) - @cached(cache=TTLCache(maxsize=1, ttl=3600)) + @cached(maxsize=1, ttl=3600) def get_latest_wallpapers(self, server: str = None, count: int = 10, remote: bool = True, username: str = None) -> List[str]: """ diff --git a/app/chain/tmdb.py b/app/chain/tmdb.py index 6699b5b4..bd84aa94 100644 --- a/app/chain/tmdb.py +++ b/app/chain/tmdb.py @@ -1,10 +1,9 @@ import random from typing import Optional, List -from cachetools import cached, TTLCache - from app import schemas from app.chain import ChainBase +from app.core.cache import cached from app.core.context import MediaInfo from app.schemas import MediaType 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) - @cached(cache=TTLCache(maxsize=1, ttl=3600)) + @cached(maxsize=1, ttl=3600) def get_random_wallpager(self) -> Optional[str]: """ 获取随机壁纸,缓存1个小时 @@ -133,7 +132,7 @@ class TmdbChain(ChainBase, metaclass=Singleton): return info.backdrop_path return None - @cached(cache=TTLCache(maxsize=1, ttl=3600)) + @cached(maxsize=1, ttl=3600) def get_trending_wallpapers(self, num: int = 10) -> List[str]: """ 获取所有流行壁纸 diff --git a/app/modules/bangumi/bangumi.py b/app/modules/bangumi/bangumi.py index abf9330b..fc1db92b 100644 --- a/app/modules/bangumi/bangumi.py +++ b/app/modules/bangumi/bangumi.py @@ -1,8 +1,8 @@ from datetime import datetime import requests -from cachetools import TTLCache, cached +from app.core.cache import cached from app.core.config import settings from app.utils.http import RequestUtils @@ -29,7 +29,7 @@ class BangumiApi(object): pass @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): req_url = cls._base_url + url params = {} @@ -188,7 +188,8 @@ class BangumiApi(object): 获取人物参演作品 """ 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: for item in result: ret_list.append(item) diff --git a/app/modules/douban/apiv2.py b/app/modules/douban/apiv2.py index 56a21b2e..478c625e 100644 --- a/app/modules/douban/apiv2.py +++ b/app/modules/douban/apiv2.py @@ -7,8 +7,8 @@ from random import choice from urllib import parse import requests -from cachetools import TTLCache, cached +from app.core.cache import cached from app.core.config import settings from app.utils.http import RequestUtils from app.utils.singleton import Singleton @@ -174,14 +174,14 @@ class DoubanApi(metaclass=Singleton): ).digest() ).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: """ 推荐/发现类API """ 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: """ 搜索类API @@ -216,7 +216,7 @@ class DoubanApi(metaclass=Singleton): return resp.json() 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: """ POST请求 diff --git a/app/modules/fanart/__init__.py b/app/modules/fanart/__init__.py index abfe4ab9..93d13ecc 100644 --- a/app/modules/fanart/__init__.py +++ b/app/modules/fanart/__init__.py @@ -1,8 +1,7 @@ import re 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.log import logger from app.modules import _ModuleBase @@ -11,7 +10,6 @@ from app.utils.http import RequestUtils class FanartModule(_ModuleBase): - """ { "name": "The Wheel of Time", @@ -384,7 +382,7 @@ class FanartModule(_ModuleBase): continue if not isinstance(images, list): continue - + # 图片属性xx_path image_name = self.__name(name) if image_name.startswith("season"): @@ -422,7 +420,7 @@ class FanartModule(_ModuleBase): return result @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]: if media_type == MediaType.MOVIE: image_url = cls._movie_url % queryid diff --git a/app/modules/filemanager/storages/alist.py b/app/modules/filemanager/storages/alist.py index cc4eadfb..023c7a20 100644 --- a/app/modules/filemanager/storages/alist.py +++ b/app/modules/filemanager/storages/alist.py @@ -4,10 +4,10 @@ from datetime import datetime from pathlib import Path from typing import Optional, List, Dict -from cachetools import cached, TTLCache from requests import Response from app import schemas +from app.core.cache import cached from app.core.config import settings from app.log import logger from app.modules.filemanager.storages import StorageBase @@ -67,7 +67,7 @@ class Alist(StorageBase, metaclass=Singleton): return self.__generate_token @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: """ 使用账号密码生成一个临时token diff --git a/app/modules/plex/plex.py b/app/modules/plex/plex.py index b04d2753..48e4be62 100644 --- a/app/modules/plex/plex.py +++ b/app/modules/plex/plex.py @@ -3,13 +3,13 @@ from pathlib import Path from typing import List, Optional, Dict, Tuple, Generator, Any, Union from urllib.parse import quote_plus -from cachetools import TTLCache, cached from plexapi import media from plexapi.myplex import MyPlexAccount from plexapi.server import PlexServer from requests import Response, Session from app import schemas +from app.core.cache import cached from app.log import logger from app.schemas import MediaType from app.utils.http import RequestUtils @@ -83,7 +83,7 @@ class Plex: logger.error(f"Authentication failed: {e}") 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]]: """ 获取媒体服务器最近添加的媒体的图片列表 @@ -293,7 +293,7 @@ class Plex: season_episodes[episode.seasonNumber].append(episode.index) return videos.key, season_episodes - def get_remote_image_by_id(self, + def get_remote_image_by_id(self, item_id: str, image_type: str, depth: int = 0, diff --git a/app/modules/themoviedb/tmdbapi.py b/app/modules/themoviedb/tmdbapi.py index b27ca808..178bd4f1 100644 --- a/app/modules/themoviedb/tmdbapi.py +++ b/app/modules/themoviedb/tmdbapi.py @@ -3,9 +3,9 @@ from typing import Optional, List from urllib.parse import quote import zhconv -from cachetools import TTLCache, cached from lxml import etree +from app.core.cache import cached from app.core.config import settings from app.log import logger from app.schemas.types import MediaType @@ -491,7 +491,7 @@ class TmdbApi: 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]: """ 搜索TMDB网站,直接抓取结果,结果只有一条时才返回 @@ -678,14 +678,14 @@ class TmdbApi: else: en_title = __get_tmdb_lang_title(tmdb_info, "US") tmdb_info['en_title'] = en_title or org_title - + # 查找香港台湾译名 tmdb_info['hk_title'] = __get_tmdb_lang_title(tmdb_info, "HK") tmdb_info['tw_title'] = __get_tmdb_lang_title(tmdb_info, "TW") # 查找新加坡名(用于替代中文名) tmdb_info['sg_title'] = __get_tmdb_lang_title(tmdb_info, "SG") or org_title - + def __get_movie_detail(self, tmdbid: int, append_to_response: str = "images," diff --git a/app/modules/themoviedb/tmdbv3api/objs/discover.py b/app/modules/themoviedb/tmdbv3api/objs/discover.py index a79891a5..70f1ff41 100644 --- a/app/modules/themoviedb/tmdbv3api/objs/discover.py +++ b/app/modules/themoviedb/tmdbv3api/objs/discover.py @@ -1,5 +1,5 @@ +from app.core.cache import cached from ..tmdb import TMDb -from cachetools import cached, TTLCache try: from urllib import urlencode @@ -13,7 +13,7 @@ class Discover(TMDb): "tv": "/discover/tv" } - @cached(cache=TTLCache(maxsize=1, ttl=43200)) + @cached(maxsize=1, ttl=43200) def discover_movies(self, params_tuple): """ 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) 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): """ Discover TV shows by different types of data like average rating, number of votes, genres, diff --git a/app/modules/themoviedb/tmdbv3api/objs/trending.py b/app/modules/themoviedb/tmdbv3api/objs/trending.py index 7338792b..49ba53bd 100644 --- a/app/modules/themoviedb/tmdbv3api/objs/trending.py +++ b/app/modules/themoviedb/tmdbv3api/objs/trending.py @@ -1,4 +1,4 @@ -from cachetools import cached, TTLCache +from app.core.cache import cached from ..tmdb import TMDb @@ -6,7 +6,7 @@ from ..tmdb import TMDb class Trending(TMDb): _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): """ Get trending, TTLCache 12 hours diff --git a/app/modules/themoviedb/tmdbv3api/tmdb.py b/app/modules/themoviedb/tmdbv3api/tmdb.py index e6fb1bb0..02e164cd 100644 --- a/app/modules/themoviedb/tmdbv3api/tmdb.py +++ b/app/modules/themoviedb/tmdbv3api/tmdb.py @@ -7,8 +7,8 @@ from datetime import datetime import requests import requests.exceptions -from cachetools import TTLCache, cached +from app.core.cache import cached from app.core.config import settings from app.utils.http import RequestUtils from .exceptions import TMDbException @@ -137,7 +137,7 @@ class TMDb(object): def cache(self, 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, _ts=datetime.strftime(datetime.now(), '%Y%m%d')): """ diff --git a/app/utils/web.py b/app/utils/web.py index 6145adb7..9e83070c 100644 --- a/app/utils/web.py +++ b/app/utils/web.py @@ -1,6 +1,6 @@ from typing import Optional, List -from cachetools import TTLCache, cached +from app.core.cache import cached from app.utils.http import RequestUtils @@ -75,7 +75,7 @@ class WebUtils: return "" @staticmethod - @cached(cache=TTLCache(maxsize=1, ttl=3600)) + @cached(maxsize=1, ttl=3600) def get_bing_wallpaper() -> Optional[str]: """ 获取Bing每日壁纸 @@ -93,7 +93,7 @@ class WebUtils: return None @staticmethod - @cached(cache=TTLCache(maxsize=1, ttl=3600)) + @cached(maxsize=1, ttl=3600) def get_bing_wallpapers(num: int = 7) -> List[str]: """ 获取7天的Bing每日壁纸 diff --git a/requirements.in b/requirements.in index deea47a6..3601052d 100644 --- a/requirements.in +++ b/requirements.in @@ -64,4 +64,5 @@ python-cookietools==0.0.2.1 aligo~=6.2.4 aiofiles~=24.1.0 jieba~=0.42.1 -rsa~=4.9 \ No newline at end of file +rsa~=4.9 +redis~=5.2.1 \ No newline at end of file