Files
fastapi_best_architecture/backend/common/context.py
Wu Clan 6f1c27786d Add multi level caching and optimize caching (#1054)
* 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
2026-02-02 17:18:49 +08:00

36 lines
692 B
Python

from datetime import datetime
from typing import Any, Protocol
from starlette_context.ctx import _Context, context
class TypedContextProtocol(Protocol):
perf_time: float
start_time: datetime
ip: str
country: str | None
region: str | None
city: str | None
user_agent: str | None
os: str | None
browser: str | None
device: str | None
permission: str | None
language: str
user_id: int | None
class TypedContext(TypedContextProtocol, _Context):
def __getattr__(self, name: str) -> Any:
return context.get(name)
def __setattr__(self, name: str, value: Any) -> None:
context[name] = value
ctx = TypedContext()