Files
fastapi-cache/fastapi_cache/__init__.py

98 lines
2.9 KiB
Python
Raw Normal View History

from typing import ClassVar, Optional, Type
from fastapi_cache.coder import Coder, JsonCoder
from fastapi_cache.key_builder import default_key_builder
from fastapi_cache.types import Backend, KeyBuilder
__all__ = [
"Backend",
"Coder",
"FastAPICache",
"JsonCoder",
"KeyBuilder",
"default_key_builder",
]
2020-08-26 18:04:57 +08:00
class FastAPICache:
_backend: ClassVar[Optional[Backend]] = None
_prefix: ClassVar[Optional[str]] = None
_expire: ClassVar[Optional[int]] = None
_init: ClassVar[bool] = False
_coder: ClassVar[Optional[Type[Coder]]] = None
_key_builder: ClassVar[Optional[KeyBuilder]] = None
_cache_status_header: ClassVar[Optional[str]] = None
_enable: ClassVar[bool] = True
2020-08-26 18:04:57 +08:00
@classmethod
def init(
cls,
2022-10-22 20:59:37 +04:00
backend: Backend,
prefix: str = "",
2022-10-22 20:59:37 +04:00
expire: Optional[int] = None,
coder: Type[Coder] = JsonCoder,
key_builder: KeyBuilder = default_key_builder,
cache_status_header: str = "X-FastAPI-Cache",
2021-10-28 15:52:21 +08:00
enable: bool = True,
2022-10-22 20:59:37 +04:00
) -> None:
2020-10-08 15:10:34 +08:00
if cls._init:
return
cls._init = True
2020-08-26 18:04:57 +08:00
cls._backend = backend
cls._prefix = prefix
2020-10-08 15:10:34 +08:00
cls._expire = expire
cls._coder = coder
cls._key_builder = key_builder
cls._cache_status_header = cache_status_header
2021-10-28 15:52:21 +08:00
cls._enable = enable
2020-08-26 18:04:57 +08:00
@classmethod
def reset(cls) -> None:
cls._init = False
cls._backend = None
cls._prefix = None
cls._expire = None
cls._coder = None
cls._key_builder = None
cls._cache_status_header = None
cls._enable = True
2020-08-26 18:04:57 +08:00
@classmethod
2022-10-22 20:59:37 +04:00
def get_backend(cls) -> Backend:
assert cls._backend, "You must call init first!" # noqa: S101
2020-08-26 18:04:57 +08:00
return cls._backend
@classmethod
def get_prefix(cls) -> str:
assert cls._prefix is not None, "You must call init first!" # noqa: S101
2020-08-26 18:04:57 +08:00
return cls._prefix
2020-10-08 15:10:34 +08:00
@classmethod
2022-10-22 20:59:37 +04:00
def get_expire(cls) -> Optional[int]:
2020-10-08 15:10:34 +08:00
return cls._expire
@classmethod
def get_coder(cls) -> Type[Coder]:
assert cls._coder, "You must call init first!" # noqa: S101
return cls._coder
@classmethod
def get_key_builder(cls) -> KeyBuilder:
assert cls._key_builder, "You must call init first!" # noqa: S101
return cls._key_builder
2020-11-03 18:08:06 +08:00
@classmethod
def get_cache_status_header(cls) -> str:
assert cls._cache_status_header, "You must call init first!" # noqa: S101
return cls._cache_status_header
2021-10-28 15:52:21 +08:00
@classmethod
2022-10-22 20:59:37 +04:00
def get_enable(cls) -> bool:
2021-10-28 15:52:21 +08:00
return cls._enable
2020-11-03 18:08:06 +08:00
@classmethod
2022-10-22 20:59:37 +04:00
async def clear(cls, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
assert cls._backend and cls._prefix is not None, "You must call init first!" # noqa: S101
namespace = cls._prefix + (":" + namespace if namespace else "")
2020-11-03 18:08:06 +08:00
return await cls._backend.clear(namespace, key)