mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-06 16:07:01 +08:00
fix TTLCache
This commit is contained in:
@@ -895,6 +895,25 @@ class TTLCache:
|
|||||||
|
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
def delete(self, key: str):
|
||||||
|
"""
|
||||||
|
删除缓存项
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
self._backend.delete(key, region=self.region)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"缓存删除失败: {e}")
|
||||||
|
|
||||||
|
def items(self):
|
||||||
|
"""
|
||||||
|
获取缓存的所有键值对
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return self._backend.items(region=self.region)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"缓存获取失败: {e}")
|
||||||
|
return []
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
"""
|
"""
|
||||||
清空缓存
|
清空缓存
|
||||||
@@ -904,6 +923,12 @@ class TTLCache:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"缓存清空失败: {e}")
|
logger.warning(f"缓存清空失败: {e}")
|
||||||
|
|
||||||
|
def is_redis(self) -> bool:
|
||||||
|
"""
|
||||||
|
判断当前缓存后端是否为 Redis
|
||||||
|
"""
|
||||||
|
return self._backend.is_redis()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""
|
"""
|
||||||
关闭缓存连接
|
关闭缓存连接
|
||||||
|
|||||||
@@ -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 Cache
|
from app.core.cache import TTLCache
|
||||||
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 = Cache(maxsize=self.maxsize, ttl=self.ttl)
|
self._cache = TTLCache(region=self.region, 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():
|
||||||
@@ -61,7 +61,7 @@ class DoubanCache(metaclass=WeakSingleton):
|
|||||||
"""
|
"""
|
||||||
key = self.__get_key(meta)
|
key = self.__get_key(meta)
|
||||||
with lock:
|
with lock:
|
||||||
return self._cache.get(key, region=self.region) or {}
|
return self._cache.get(key) or {}
|
||||||
|
|
||||||
def delete(self, key: str) -> dict:
|
def delete(self, key: str) -> dict:
|
||||||
"""
|
"""
|
||||||
@@ -70,9 +70,9 @@ class DoubanCache(metaclass=WeakSingleton):
|
|||||||
@return: 被删除的缓存内容
|
@return: 被删除的缓存内容
|
||||||
"""
|
"""
|
||||||
with lock:
|
with lock:
|
||||||
redis_data = self._cache.get(key, region=self.region)
|
redis_data = self._cache.get(key)
|
||||||
if redis_data:
|
if redis_data:
|
||||||
self._cache.delete(key, region=self.region)
|
self._cache.delete(key)
|
||||||
return redis_data
|
return redis_data
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -84,10 +84,10 @@ class DoubanCache(metaclass=WeakSingleton):
|
|||||||
@return: 被修改后缓存内容
|
@return: 被修改后缓存内容
|
||||||
"""
|
"""
|
||||||
with lock:
|
with lock:
|
||||||
redis_data = self._cache.get(key, region=self.region)
|
redis_data = self._cache.get(key)
|
||||||
if redis_data:
|
if redis_data:
|
||||||
redis_data["title"] = title
|
redis_data["title"] = title
|
||||||
self._cache.set(key, redis_data, region=self.region)
|
self._cache.set(key, redis_data)
|
||||||
return redis_data
|
return redis_data
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -139,14 +139,14 @@ class DoubanCache(metaclass=WeakSingleton):
|
|||||||
"year": cache_year,
|
"year": cache_year,
|
||||||
"title": cache_title,
|
"title": cache_title,
|
||||||
"poster_path": poster_path
|
"poster_path": poster_path
|
||||||
}, region=self.region)
|
})
|
||||||
|
|
||||||
elif info is not None:
|
elif info is not None:
|
||||||
# None时不缓存,此时代表网络错误,允许重复请求
|
# None时不缓存,此时代表网络错误,允许重复请求
|
||||||
with lock:
|
with lock:
|
||||||
self._cache.set(self.__get_key(meta), {
|
self._cache.set(self.__get_key(meta), {
|
||||||
"id": 0
|
"id": 0
|
||||||
}, region=self.region)
|
})
|
||||||
|
|
||||||
def save(self, force: Optional[bool] = False) -> None:
|
def save(self, force: Optional[bool] = False) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -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 Cache
|
from app.core.cache import TTLCache
|
||||||
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 = Cache(maxsize=self.maxsize, ttl=self.ttl)
|
self._cache = TTLCache(region=self.region, 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():
|
||||||
@@ -59,7 +59,7 @@ class TmdbCache(metaclass=WeakSingleton):
|
|||||||
key = self.__get_key(meta)
|
key = self.__get_key(meta)
|
||||||
|
|
||||||
with lock:
|
with lock:
|
||||||
return self._cache.get(key, region=self.region) or {}
|
return self._cache.get(key) or {}
|
||||||
|
|
||||||
def delete(self, key: str) -> dict:
|
def delete(self, key: str) -> dict:
|
||||||
"""
|
"""
|
||||||
@@ -68,9 +68,9 @@ class TmdbCache(metaclass=WeakSingleton):
|
|||||||
@return: 被删除的缓存内容
|
@return: 被删除的缓存内容
|
||||||
"""
|
"""
|
||||||
with lock:
|
with lock:
|
||||||
redis_data = self._cache.get(key, region=self.region)
|
redis_data = self._cache.get(key)
|
||||||
if redis_data:
|
if redis_data:
|
||||||
self._cache.delete(key, region=self.region)
|
self._cache.delete(key)
|
||||||
return redis_data
|
return redis_data
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -82,10 +82,10 @@ class TmdbCache(metaclass=WeakSingleton):
|
|||||||
@return: 被修改后缓存内容
|
@return: 被修改后缓存内容
|
||||||
"""
|
"""
|
||||||
with lock:
|
with lock:
|
||||||
redis_data = self._cache.get(key, region=self.region)
|
redis_data = self._cache.get(key)
|
||||||
if redis_data:
|
if redis_data:
|
||||||
redis_data['title'] = title
|
redis_data['title'] = title
|
||||||
self._cache.set(key, redis_data, region=self.region)
|
self._cache.set(key, redis_data)
|
||||||
return redis_data
|
return redis_data
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -128,12 +128,12 @@ class TmdbCache(metaclass=WeakSingleton):
|
|||||||
"poster_path": info.get("poster_path"),
|
"poster_path": info.get("poster_path"),
|
||||||
"backdrop_path": info.get("backdrop_path")
|
"backdrop_path": info.get("backdrop_path")
|
||||||
}
|
}
|
||||||
self._cache.set(key, cache_data, region=self.region)
|
self._cache.set(key, cache_data)
|
||||||
|
|
||||||
elif info is not None:
|
elif info is not None:
|
||||||
# None时不缓存,此时代表网络错误,允许重复请求
|
# None时不缓存,此时代表网络错误,允许重复请求
|
||||||
with lock:
|
with lock:
|
||||||
self._cache.set(key, {"id": 0}, region=self.region)
|
self._cache.set(key, {"id": 0})
|
||||||
|
|
||||||
def save(self, force: bool = False) -> None:
|
def save(self, force: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user