mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Import supported backends
This ensures that any syntax issues are caught early (by type checkers and tests). Backends that are missing dependencies are skipped. By importing, this exposed an issue where the redis type annotations raised an exception, which has been fixed by using forward annotations. To help avoid import dependency hell, the Backend ABC has been moved to `fastapi_cache.types`. In the process, it has been made an actual ABC.
This commit is contained in:
committed by
Martijn Pieters
parent
9638d70dfe
commit
d10f4af6d6
@@ -1,20 +1,29 @@
|
||||
import abc
|
||||
from typing import Optional, Tuple
|
||||
from fastapi_cache.types import Backend
|
||||
from fastapi_cache.backends import inmemory
|
||||
|
||||
|
||||
class Backend:
|
||||
@abc.abstractmethod
|
||||
async def get_with_ttl(self, key: str) -> Tuple[int, Optional[bytes]]:
|
||||
raise NotImplementedError
|
||||
__all__ = ["Backend", "inmemory"]
|
||||
|
||||
@abc.abstractmethod
|
||||
async def get(self, key: str) -> Optional[bytes]:
|
||||
raise NotImplementedError
|
||||
# import each backend in turn and add to __all__. This syntax
|
||||
# is explicitly supported by type checkers, while more dynamic
|
||||
# syntax would not be recognised.
|
||||
try:
|
||||
from fastapi_cache.backends import dynamodb
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
__all__ += ["dynamodb"]
|
||||
|
||||
@abc.abstractmethod
|
||||
async def set(self, key: str, value: bytes, expire: Optional[int] = None) -> None:
|
||||
raise NotImplementedError
|
||||
try:
|
||||
from fastapi_cache.backends import memcached
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
__all__ += ["memcached"]
|
||||
|
||||
@abc.abstractmethod
|
||||
async def clear(self, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
|
||||
raise NotImplementedError
|
||||
try:
|
||||
from fastapi_cache.backends import redis
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
__all__ += ["redis"]
|
||||
|
||||
Reference in New Issue
Block a user