This commit is contained in:
jxxghp
2025-08-21 16:02:50 +08:00
parent 03c757bba6
commit c6c84fe65b
10 changed files with 31 additions and 31 deletions
+3 -3
View File
@@ -17,7 +17,7 @@ from fastapi.responses import StreamingResponse
from app import schemas from app import schemas
from app.chain.search import SearchChain from app.chain.search import SearchChain
from app.chain.system import SystemChain from app.chain.system import SystemChain
from app.core.cache import get_async_file_cache_backend from app.core.cache import AsyncFileCache
from app.core.config import global_vars, settings from app.core.config import global_vars, settings
from app.core.event import eventmanager from app.core.event import eventmanager
from app.core.metainfo import MetaInfo from app.core.metainfo import MetaInfo
@@ -74,8 +74,8 @@ async def fetch_image(
cache_path = cache_path.with_suffix(".jpg") cache_path = cache_path.with_suffix(".jpg")
# 缓存对像,缓存过期时间为全局图片缓存天数 # 缓存对像,缓存过期时间为全局图片缓存天数
cache_backend = get_async_file_cache_backend(base=settings.CACHE_PATH, cache_backend = AsyncFileCache(base=settings.CACHE_PATH,
ttl=settings.GLOBAL_IMAGE_CACHE_DAYS * 24 * 3600) ttl=settings.GLOBAL_IMAGE_CACHE_DAYS * 24 * 3600)
if use_cache: if use_cache:
content = await cache_backend.get(cache_path.as_posix(), region="images") content = await cache_backend.get(cache_path.as_posix(), region="images")
+3 -3
View File
@@ -11,7 +11,7 @@ from fastapi.concurrency import run_in_threadpool
from qbittorrentapi import TorrentFilesList from qbittorrentapi import TorrentFilesList
from transmission_rpc import File from transmission_rpc import File
from app.core.cache import get_file_cache_backend, get_async_file_cache_backend from app.core.cache import FileCache, AsyncFileCache
from app.core.config import settings from app.core.config import settings
from app.core.context import Context, MediaInfo, TorrentInfo from app.core.context import Context, MediaInfo, TorrentInfo
from app.core.event import EventManager from app.core.event import EventManager
@@ -46,8 +46,8 @@ class ChainBase(metaclass=ABCMeta):
send_callback=self.run_module send_callback=self.run_module
) )
self.pluginmanager = PluginManager() self.pluginmanager = PluginManager()
self.filecache = get_file_cache_backend() self.filecache = FileCache()
self.async_filecache = get_async_file_cache_backend() self.async_filecache = AsyncFileCache()
def load_cache(self, filename: str) -> Any: def load_cache(self, filename: str) -> Any:
""" """
+2 -2
View File
@@ -8,7 +8,7 @@ from typing import List, Optional, Tuple, Set, Dict, Union
from app import schemas from app import schemas
from app.chain import ChainBase from app.chain import ChainBase
from app.core.cache import get_file_cache_backend from app.core.cache import FileCache
from app.core.config import settings, global_vars from app.core.config import settings, global_vars
from app.core.context import MediaInfo, TorrentInfo, Context from app.core.context import MediaInfo, TorrentInfo, Context
from app.core.event import eventmanager, Event from app.core.event import eventmanager, Event
@@ -222,7 +222,7 @@ class DownloadChain(ChainBase):
torrent_content = torrent_file.read_bytes() torrent_content = torrent_file.read_bytes()
else: else:
# 缓存处理器 # 缓存处理器
cache_backend = get_file_cache_backend() cache_backend = FileCache()
# 读取缓存的种子文件 # 读取缓存的种子文件
torrent_content = cache_backend.get(torrent_file.as_posix(), region="torrents") torrent_content = cache_backend.get(torrent_file.as_posix(), region="torrents")
+3 -3
View File
@@ -9,7 +9,7 @@ from app.chain import ChainBase
from app.chain.bangumi import BangumiChain from app.chain.bangumi import BangumiChain
from app.chain.douban import DoubanChain from app.chain.douban import DoubanChain
from app.chain.tmdb import TmdbChain from app.chain.tmdb import TmdbChain
from app.core.cache import cached, get_file_cache_backend from app.core.cache import cached, FileCache
from app.core.config import settings, global_vars from app.core.config import settings, global_vars
from app.log import logger from app.log import logger
from app.schemas import MediaType from app.schemas import MediaType
@@ -111,8 +111,8 @@ class RecommendChain(ChainBase, metaclass=Singleton):
cache_path = cache_path.with_suffix(".jpg") cache_path = cache_path.with_suffix(".jpg")
# 获取缓存后端,并设置缓存时间为全局配置的缓存天数 # 获取缓存后端,并设置缓存时间为全局配置的缓存天数
cache_backend = get_file_cache_backend(base=settings.CACHE_PATH, cache_backend = FileCache(base=settings.CACHE_PATH,
ttl=settings.GLOBAL_IMAGE_CACHE_DAYS * 24 * 3600) ttl=settings.GLOBAL_IMAGE_CACHE_DAYS * 24 * 3600)
# 本地存在缓存图片,则直接跳过 # 本地存在缓存图片,则直接跳过
if cache_backend.get(cache_path.as_posix(), region="images"): if cache_backend.get(cache_path.as_posix(), region="images"):
+5 -5
View File
@@ -762,7 +762,7 @@ class AsyncFileBackend(AsyncCacheBackend):
pass pass
def get_file_cache_backend(base: Path = settings.TEMP_PATH, ttl: Optional[int] = None) -> CacheBackend: def FileCache(base: Path = settings.TEMP_PATH, ttl: Optional[int] = None) -> CacheBackend:
""" """
获取文件缓存后端实例(Redis或文件系统) 获取文件缓存后端实例(Redis或文件系统)
""" """
@@ -774,7 +774,7 @@ def get_file_cache_backend(base: Path = settings.TEMP_PATH, ttl: Optional[int] =
return FileBackend(base=base) return FileBackend(base=base)
def get_async_file_cache_backend(base: Path = settings.TEMP_PATH, ttl: Optional[int] = None) -> AsyncCacheBackend: def AsyncFileCache(base: Path = settings.TEMP_PATH, ttl: Optional[int] = None) -> AsyncCacheBackend:
""" """
获取文件异步缓存后端实例(Redis或文件系统) 获取文件异步缓存后端实例(Redis或文件系统)
""" """
@@ -786,7 +786,7 @@ def get_async_file_cache_backend(base: Path = settings.TEMP_PATH, ttl: Optional[
return AsyncFileBackend(base=base) return AsyncFileBackend(base=base)
def get_cache_backend(maxsize: Optional[int] = 512, ttl: Optional[int] = 1800) -> CacheBackend: def Cache(maxsize: Optional[int] = 512, ttl: Optional[int] = 1800) -> CacheBackend:
""" """
根据配置获取缓存后端实例(内存或Redis) 根据配置获取缓存后端实例(内存或Redis)
@@ -822,7 +822,7 @@ class TTLCache:
self.region = region self.region = region
self.maxsize = maxsize self.maxsize = maxsize
self.ttl = ttl self.ttl = ttl
self._backend = get_cache_backend(maxsize=maxsize, ttl=ttl) self._backend = Cache(maxsize=maxsize, ttl=ttl)
def __getitem__(self, key: str): def __getitem__(self, key: str):
""" """
@@ -927,7 +927,7 @@ def cached(region: Optional[str] = None, maxsize: Optional[int] = 512, ttl: Opti
:return: 装饰器函数 :return: 装饰器函数
""" """
# 缓存后端实例 # 缓存后端实例
cache_backend = get_cache_backend(maxsize=maxsize, ttl=ttl) cache_backend = Cache(maxsize=maxsize, ttl=ttl)
def should_cache(value: Any) -> bool: def should_cache(value: Any) -> bool:
""" """
+2 -2
View File
@@ -6,7 +6,7 @@ from urllib.parse import unquote
from torrentool.api import Torrent from torrentool.api import Torrent
from app.core.cache import get_file_cache_backend from app.core.cache import FileCache
from app.core.config import settings from app.core.config import settings
from app.core.context import Context, TorrentInfo, MediaInfo from app.core.context import Context, TorrentInfo, MediaInfo
from app.core.meta import MetaBase from app.core.meta import MetaBase
@@ -43,7 +43,7 @@ class TorrentHelper(metaclass=WeakSingleton):
# 构建 torrent 种子文件的缓存路径 # 构建 torrent 种子文件的缓存路径
cache_path = Path(StringUtils.md5_hash(url)).with_suffix(".torrent") cache_path = Path(StringUtils.md5_hash(url)).with_suffix(".torrent")
# 缓存处理器 # 缓存处理器
cache_backend = get_file_cache_backend() cache_backend = FileCache()
# 读取缓存的种子文件 # 读取缓存的种子文件
torrent_content = cache_backend.get(cache_path.as_posix(), region="torrents") torrent_content = cache_backend.get(cache_path.as_posix(), region="torrents")
if torrent_content: if torrent_content:
+2 -2
View File
@@ -4,7 +4,7 @@ from pathlib import Path
from threading import RLock from threading import RLock
from typing import Optional from typing import Optional
from app.core.cache import get_cache_backend from app.core.cache import Cache
from app.core.config import settings from app.core.config import settings
from app.core.meta import MetaBase from app.core.meta import MetaBase
from app.core.metainfo import MetaInfo from app.core.metainfo import MetaInfo
@@ -34,7 +34,7 @@ class DoubanCache(metaclass=WeakSingleton):
self.region = "__douban_cache__" self.region = "__douban_cache__"
self._meta_filepath = settings.TEMP_PATH / self.region self._meta_filepath = settings.TEMP_PATH / self.region
# 初始化缓存 # 初始化缓存
self._cache = get_cache_backend(maxsize=self.maxsize, ttl=self.ttl) self._cache = Cache(maxsize=self.maxsize, ttl=self.ttl)
# 非Redis加载本地缓存数据 # 非Redis加载本地缓存数据
if not self._cache.is_redis(): if not self._cache.is_redis():
for key, value in self.__load(self._meta_filepath).items(): for key, value in self.__load(self._meta_filepath).items():
+5 -5
View File
@@ -5,10 +5,10 @@ from qbittorrentapi import TorrentFilesList
from torrentool.torrent import Torrent from torrentool.torrent import Torrent
from app import schemas from app import schemas
from app.core.cache import get_file_cache_backend from app.core.cache import FileCache
from app.core.config import settings from app.core.config import settings
from app.core.metainfo import MetaInfo
from app.core.event import eventmanager, Event from app.core.event import eventmanager, Event
from app.core.metainfo import MetaInfo
from app.log import logger from app.log import logger
from app.modules import _ModuleBase, _DownloaderBase from app.modules import _ModuleBase, _DownloaderBase
from app.modules.qbittorrent.qbittorrent import Qbittorrent from app.modules.qbittorrent.qbittorrent import Qbittorrent
@@ -119,7 +119,7 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
torrent_content = content.read_bytes() torrent_content = content.read_bytes()
else: else:
# 缓存处理器 # 缓存处理器
cache_backend = get_file_cache_backend() cache_backend = FileCache()
# 读取缓存的种子文件 # 读取缓存的种子文件
torrent_content = cache_backend.get(content.as_posix(), region="torrents") torrent_content = cache_backend.get(content.as_posix(), region="torrents")
else: else:
@@ -137,7 +137,7 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
return None, None, None, "下载内容为空" return None, None, None, "下载内容为空"
# 读取种子的名称 # 读取种子的名称
torrent, content = __get_torrent_info() torrent, content = __get_torrent_info()
if not torrent: if not torrent:
return None, None, None, f"添加种子任务失败:无法读取种子文件" return None, None, None, f"添加种子任务失败:无法读取种子文件"
@@ -337,7 +337,7 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
del torrents del torrents
else: else:
return None return None
return ret_torrents # noqa return ret_torrents # noqa
def transfer_completed(self, hashs: str, downloader: Optional[str] = None) -> None: def transfer_completed(self, hashs: str, downloader: Optional[str] = None) -> None:
""" """
+2 -2
View File
@@ -3,7 +3,7 @@ import traceback
from pathlib import Path from pathlib import Path
from threading import RLock from threading import RLock
from app.core.cache import get_cache_backend from app.core.cache import Cache
from app.core.config import settings from app.core.config import settings
from app.core.meta import MetaBase from app.core.meta import MetaBase
from app.log import logger from app.log import logger
@@ -32,7 +32,7 @@ class TmdbCache(metaclass=WeakSingleton):
self.region = "__tmdb_cache__" self.region = "__tmdb_cache__"
self._meta_filepath = settings.TEMP_PATH / self.region self._meta_filepath = settings.TEMP_PATH / self.region
# 初始化缓存 # 初始化缓存
self._cache = get_cache_backend(maxsize=self.maxsize, ttl=self.ttl) self._cache = Cache(maxsize=self.maxsize, ttl=self.ttl)
# 非Redis加载本地缓存数据 # 非Redis加载本地缓存数据
if not self._cache.is_redis(): if not self._cache.is_redis():
for key, value in self.__load(self._meta_filepath).items(): for key, value in self.__load(self._meta_filepath).items():
+4 -4
View File
@@ -5,10 +5,10 @@ from torrentool.torrent import Torrent
from transmission_rpc import File from transmission_rpc import File
from app import schemas from app import schemas
from app.core.cache import get_file_cache_backend from app.core.cache import FileCache
from app.core.config import settings from app.core.config import settings
from app.core.metainfo import MetaInfo
from app.core.event import eventmanager, Event from app.core.event import eventmanager, Event
from app.core.metainfo import MetaInfo
from app.log import logger from app.log import logger
from app.modules import _ModuleBase, _DownloaderBase from app.modules import _ModuleBase, _DownloaderBase
from app.modules.transmission.transmission import Transmission from app.modules.transmission.transmission import Transmission
@@ -120,7 +120,7 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
torrent_content = content.read_bytes() torrent_content = content.read_bytes()
else: else:
# 缓存处理器 # 缓存处理器
cache_backend = get_file_cache_backend() cache_backend = FileCache()
# 读取缓存的种子文件 # 读取缓存的种子文件
torrent_content = cache_backend.get(content.as_posix(), region="torrents") torrent_content = cache_backend.get(content.as_posix(), region="torrents")
else: else:
@@ -325,7 +325,7 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
del torrents del torrents
else: else:
return None return None
return ret_torrents # noqa return ret_torrents # noqa
def transfer_completed(self, hashs: str, downloader: Optional[str] = None) -> None: def transfer_completed(self, hashs: str, downloader: Optional[str] = None) -> None:
""" """