mirror of
https://github.com/fastapi-practices/fastapi_best_architecture.git
synced 2026-03-13 09:31:31 +08: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
36 lines
692 B
Python
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()
|