mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 15:38:19 +08:00
Replace file-based snapshot caching with FileCache implementation (#4809)
Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: jxxghp <jxxghp@live.cn>
This commit is contained in:
co-authored by
Cursor Agent
jxxghp
parent
f37540f4e5
commit
1734d53b3c
+22
-17
@@ -10,7 +10,7 @@ from threading import Lock
|
|||||||
from typing import Any, Optional, Dict, List
|
from typing import Any, Optional, Dict, List
|
||||||
|
|
||||||
from apscheduler.schedulers.background import BackgroundScheduler
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
from app.core.cache import TTLCache
|
from app.core.cache import TTLCache, FileCache
|
||||||
from watchdog.events import FileSystemEventHandler, FileSystemMovedEvent, FileSystemEvent
|
from watchdog.events import FileSystemEventHandler, FileSystemMovedEvent, FileSystemEvent
|
||||||
from watchdog.observers.polling import PollingObserver
|
from watchdog.observers.polling import PollingObserver
|
||||||
|
|
||||||
@@ -73,6 +73,8 @@ class Monitor(metaclass=Singleton):
|
|||||||
self._snapshot_interval = 5
|
self._snapshot_interval = 5
|
||||||
# TTL缓存,10秒钟有效
|
# TTL缓存,10秒钟有效
|
||||||
self._cache = TTLCache(region="monitor", maxsize=1024, ttl=10)
|
self._cache = TTLCache(region="monitor", maxsize=1024, ttl=10)
|
||||||
|
# 快照文件缓存
|
||||||
|
self._snapshot_cache = FileCache(base=settings.TEMP_PATH / "snapshots", ttl=24*3600) # 24小时TTL
|
||||||
# 监控的文件扩展名
|
# 监控的文件扩展名
|
||||||
self.all_exts = settings.RMT_MEDIAEXT
|
self.all_exts = settings.RMT_MEDIAEXT
|
||||||
# 初始化快照缓存目录
|
# 初始化快照缓存目录
|
||||||
@@ -98,14 +100,13 @@ class Monitor(metaclass=Singleton):
|
|||||||
def save_snapshot(self, storage: str, snapshot: Dict, file_count: int = 0,
|
def save_snapshot(self, storage: str, snapshot: Dict, file_count: int = 0,
|
||||||
last_snapshot_time: Optional[float] = None):
|
last_snapshot_time: Optional[float] = None):
|
||||||
"""
|
"""
|
||||||
保存快照到文件
|
保存快照到文件缓存
|
||||||
:param storage: 存储名称
|
:param storage: 存储名称
|
||||||
:param snapshot: 快照数据
|
:param snapshot: 快照数据
|
||||||
:param last_snapshot_time: 上次快照时间戳
|
:param last_snapshot_time: 上次快照时间戳
|
||||||
:param file_count: 文件数量,用于调整监控间隔
|
:param file_count: 文件数量,用于调整监控间隔
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
cache_file = self._snapshot_cache_dir / f"{storage}_snapshot.json"
|
|
||||||
snapshot_time = max((item.get('modify_time', 0) for item in snapshot.values()), default=None)
|
snapshot_time = max((item.get('modify_time', 0) for item in snapshot.values()), default=None)
|
||||||
if snapshot_time is None:
|
if snapshot_time is None:
|
||||||
snapshot_time = last_snapshot_time or time.time()
|
snapshot_time = last_snapshot_time or time.time()
|
||||||
@@ -114,9 +115,11 @@ class Monitor(metaclass=Singleton):
|
|||||||
'file_count': file_count,
|
'file_count': file_count,
|
||||||
'snapshot': snapshot
|
'snapshot': snapshot
|
||||||
}
|
}
|
||||||
with open(cache_file, 'w', encoding='utf-8') as f:
|
# 使用FileCache保存快照数据
|
||||||
json.dump(snapshot_data, f, ensure_ascii=False, indent=2) # noqa
|
cache_key = f"{storage}_snapshot"
|
||||||
logger.debug(f"快照已保存到 {cache_file}")
|
snapshot_json = json.dumps(snapshot_data, ensure_ascii=False, indent=2)
|
||||||
|
self._snapshot_cache.set(cache_key, snapshot_json.encode('utf-8'), region="snapshots")
|
||||||
|
logger.debug(f"快照已保存到缓存: {storage}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"保存快照失败: {e}")
|
logger.error(f"保存快照失败: {e}")
|
||||||
|
|
||||||
@@ -127,9 +130,9 @@ class Monitor(metaclass=Singleton):
|
|||||||
:return: 是否成功
|
:return: 是否成功
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
cache_file = self._snapshot_cache_dir / f"{storage}_snapshot.json"
|
cache_key = f"{storage}_snapshot"
|
||||||
if cache_file.exists():
|
if self._snapshot_cache.exists(cache_key, region="snapshots"):
|
||||||
cache_file.unlink()
|
self._snapshot_cache.delete(cache_key, region="snapshots")
|
||||||
logger.info(f"快照已重置: {storage}")
|
logger.info(f"快照已重置: {storage}")
|
||||||
return True
|
return True
|
||||||
logger.debug(f"快照文件不存在,无需重置: {storage}")
|
logger.debug(f"快照文件不存在,无需重置: {storage}")
|
||||||
@@ -187,18 +190,18 @@ class Monitor(metaclass=Singleton):
|
|||||||
|
|
||||||
def load_snapshot(self, storage: str) -> Optional[Dict]:
|
def load_snapshot(self, storage: str) -> Optional[Dict]:
|
||||||
"""
|
"""
|
||||||
从文件加载快照
|
从文件缓存加载快照
|
||||||
:param storage: 存储名称
|
:param storage: 存储名称
|
||||||
:return: 快照数据或None
|
:return: 快照数据或None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
cache_file = self._snapshot_cache_dir / f"{storage}_snapshot.json"
|
cache_key = f"{storage}_snapshot"
|
||||||
if cache_file.exists():
|
snapshot_data = self._snapshot_cache.get(cache_key, region="snapshots")
|
||||||
with open(cache_file, 'r', encoding='utf-8') as f:
|
if snapshot_data:
|
||||||
data = json.load(f)
|
data = json.loads(snapshot_data.decode('utf-8'))
|
||||||
logger.debug(f"成功加载快照: {cache_file}, 包含 {len(data.get('snapshot', {}))} 个文件")
|
logger.debug(f"成功加载快照: {storage}, 包含 {len(data.get('snapshot', {}))} 个文件")
|
||||||
return data
|
return data
|
||||||
logger.debug(f"快照文件不存在: {cache_file}")
|
logger.debug(f"快照文件不存在: {storage}")
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"加载快照失败: {e}")
|
logger.error(f"加载快照失败: {e}")
|
||||||
@@ -793,4 +796,6 @@ class Monitor(metaclass=Singleton):
|
|||||||
self._scheduler = None
|
self._scheduler = None
|
||||||
if self._cache:
|
if self._cache:
|
||||||
self._cache.close()
|
self._cache.close()
|
||||||
|
if self._snapshot_cache:
|
||||||
|
self._snapshot_cache.close()
|
||||||
self._event.clear()
|
self._event.clear()
|
||||||
|
|||||||
Reference in New Issue
Block a user