mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-05-11 18:10:15 +08:00
fix TTLCache
This commit is contained in:
@@ -810,13 +810,16 @@ class TTLCache:
|
||||
- 支持Redis和cachetools的切换
|
||||
"""
|
||||
|
||||
def __init__(self, maxsize: int = 128, ttl: int = 1800):
|
||||
def __init__(self, region: Optional[str] = DEFAULT_CACHE_REGION,
|
||||
maxsize: int = 128, ttl: int = 1800):
|
||||
"""
|
||||
初始化TTL缓存
|
||||
|
||||
:param region: 缓存的区,默认为 DEFAULT_CACHE_REGION
|
||||
:param maxsize: 缓存的最大条目数
|
||||
:param ttl: 缓存的存活时间,单位秒
|
||||
"""
|
||||
self.region = region
|
||||
self.maxsize = maxsize
|
||||
self.ttl = ttl
|
||||
self._backend = get_cache_backend(maxsize=maxsize, ttl=ttl)
|
||||
@@ -826,7 +829,7 @@ class TTLCache:
|
||||
获取缓存项
|
||||
"""
|
||||
try:
|
||||
value = self._backend.get(key)
|
||||
value = self._backend.get(key, region=self.region)
|
||||
if value is not None:
|
||||
return value
|
||||
except Exception as e:
|
||||
@@ -839,7 +842,7 @@ class TTLCache:
|
||||
设置缓存项
|
||||
"""
|
||||
try:
|
||||
self._backend.set(key, value, ttl=self.ttl)
|
||||
self._backend.set(key, value, ttl=self.ttl, region=self.region)
|
||||
except Exception as e:
|
||||
logger.warning(f"缓存设置失败: {e}")
|
||||
|
||||
@@ -848,7 +851,7 @@ class TTLCache:
|
||||
删除缓存项
|
||||
"""
|
||||
try:
|
||||
self._backend.delete(key)
|
||||
self._backend.delete(key, region=self.region)
|
||||
except Exception as e:
|
||||
logger.warning(f"缓存删除失败: {e}")
|
||||
|
||||
@@ -857,7 +860,7 @@ class TTLCache:
|
||||
检查键是否存在
|
||||
"""
|
||||
try:
|
||||
return self._backend.exists(key)
|
||||
return self._backend.exists(key, region=self.region)
|
||||
except Exception as e:
|
||||
logger.warning(f"缓存检查失败: {e}")
|
||||
return False
|
||||
@@ -866,15 +869,25 @@ class TTLCache:
|
||||
"""
|
||||
返回缓存的迭代器
|
||||
"""
|
||||
for key, _ in self._backend.items():
|
||||
for key, _ in self._backend.items(region=self.region):
|
||||
yield key
|
||||
|
||||
def set(self, key: str, value: Any, ttl: Optional[int] = None):
|
||||
"""
|
||||
设置缓存项,支持自定义 TTL
|
||||
"""
|
||||
try:
|
||||
ttl = ttl or self.ttl
|
||||
self._backend.set(key, value, ttl=ttl, region=self.region)
|
||||
except Exception as e:
|
||||
logger.warning(f"缓存设置失败: {e}")
|
||||
|
||||
def get(self, key: str, default: Any = None):
|
||||
"""
|
||||
获取缓存项,如果不存在返回默认值
|
||||
"""
|
||||
try:
|
||||
value = self._backend.get(key)
|
||||
value = self._backend.get(key, region=self.region)
|
||||
if value is not None:
|
||||
return value
|
||||
except Exception as e:
|
||||
@@ -887,7 +900,7 @@ class TTLCache:
|
||||
清空缓存
|
||||
"""
|
||||
try:
|
||||
self._backend.clear()
|
||||
self._backend.clear(region=self.region)
|
||||
except Exception as e:
|
||||
logger.warning(f"缓存清空失败: {e}")
|
||||
|
||||
|
||||
@@ -307,7 +307,7 @@ class TemplateHelper(metaclass=SingletonClass):
|
||||
|
||||
def __init__(self):
|
||||
self.builder = TemplateContextBuilder()
|
||||
self.cache = TTLCache(maxsize=100, ttl=600)
|
||||
self.cache = TTLCache(region="notification", maxsize=100, ttl=600)
|
||||
|
||||
@staticmethod
|
||||
def _generate_cache_key(cuntent: Union[str, dict]) -> str:
|
||||
|
||||
@@ -72,7 +72,7 @@ class Monitor(metaclass=Singleton):
|
||||
# 存储过照间隔(分钟)
|
||||
self._snapshot_interval = 5
|
||||
# TTL缓存,10秒钟有效
|
||||
self._cache = TTLCache(maxsize=1024, ttl=10)
|
||||
self._cache = TTLCache(region="monitor", maxsize=1024, ttl=10)
|
||||
# 监控的文件扩展名
|
||||
self.all_exts = settings.RMT_MEDIAEXT
|
||||
# 初始化快照缓存目录
|
||||
|
||||
Reference in New Issue
Block a user