xtc-backend / app /cache.py
a3216's picture
deploy: initial release to Hugging Face Spaces
36fae79
Raw
History Blame Contribute Delete
717 Bytes
"""缓存层:模型列表等热数据 TTL 缓存。"""
from __future__ import annotations
import asyncio
import time
from typing import Any, Optional
from cachetools import TTLCache
# 模型列表缓存:默认 30s TTL,最多 32 个厂商
_models_cache: "TTLCache[str, list[Any]]" = TTLCache(maxsize=32, ttl=30)
def get_cached_models(provider_id: str) -> Optional[list[Any]]:
return _models_cache.get(provider_id)
def set_cached_models(provider_id: str, models: list[Any]) -> None:
_models_cache[provider_id] = models
def invalidate_models_cache(provider_id: Optional[str] = None) -> None:
if provider_id:
_models_cache.pop(provider_id, None)
else:
_models_cache.clear()