mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-26 06:19:07 +00:00
* Add multi level caching and optimize caching * optimize current caching * improved decorators * improved local * add pub/sub * fix serialize * Improve pub and sub * Add comment * Fix lru_cache maxsize * Fix lint * Fix config warmup
57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
from typing import Any
|
|
|
|
import cachebox
|
|
|
|
from backend.core.conf import settings
|
|
|
|
|
|
class LocalCacheManager:
|
|
"""本地缓存管理器"""
|
|
|
|
def __init__(self) -> None:
|
|
self.hot_cache: cachebox.TTLCache = cachebox.TTLCache(
|
|
settings.CACHE_LOCAL_MAXSIZE, ttl=settings.CACHE_LOCAL_TTL
|
|
)
|
|
|
|
def get(self, key: str) -> Any:
|
|
"""获取缓存"""
|
|
try:
|
|
return self.hot_cache[key]
|
|
except KeyError:
|
|
return None
|
|
|
|
def set(self, key: str, value: Any) -> None:
|
|
"""设置缓存"""
|
|
self.hot_cache[key] = value
|
|
|
|
def delete(self, key: str) -> bool:
|
|
"""删除缓存"""
|
|
try:
|
|
del self.hot_cache[key]
|
|
except KeyError:
|
|
return False
|
|
return True
|
|
|
|
def clear(self) -> None:
|
|
"""清空缓存"""
|
|
self.hot_cache.clear()
|
|
|
|
def delete_prefix(self, prefix: str, exclude: str | list[str] | None = None) -> None:
|
|
"""
|
|
删除指定前缀的缓存
|
|
|
|
:param prefix: 要删除的键前缀
|
|
:param exclude: 要排除的键或键列表
|
|
:return:
|
|
"""
|
|
exclude_set = set(exclude) if isinstance(exclude, list) else {exclude} if isinstance(exclude, str) else set()
|
|
for key in list(self.hot_cache.keys()):
|
|
if key.startswith(prefix) and key not in exclude_set:
|
|
try:
|
|
del self.hot_cache[key]
|
|
except KeyError:
|
|
pass
|
|
|
|
|
|
local_cache_manager = LocalCacheManager()
|