feat: add more type hints

This commit is contained in:
Ivan Moiseev
2022-10-22 20:59:37 +04:00
parent 1ef80ff457
commit 4c6abcf786
9 changed files with 71 additions and 67 deletions

View File

@@ -1,20 +1,20 @@
import abc
from typing import Tuple
from typing import Tuple, Optional
class Backend:
@abc.abstractmethod
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
async def get_with_ttl(self, key: str) -> Tuple[int, Optional[str]]:
raise NotImplementedError
@abc.abstractmethod
async def get(self, key: str) -> str:
async def get(self, key: str) -> Optional[str]:
raise NotImplementedError
@abc.abstractmethod
async def set(self, key: str, value: str, expire: int = None):
async def set(self, key: str, value: str, expire: Optional[int] = None) -> None:
raise NotImplementedError
@abc.abstractmethod
async def clear(self, namespace: str = None, key: str = None) -> int:
async def clear(self, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
raise NotImplementedError

View File

@@ -1,6 +1,7 @@
import datetime
from typing import Tuple
from typing import Tuple, Optional
from aiobotocore.client import AioBaseClient
from aiobotocore.session import get_session
from fastapi_cache.backends import Backend
@@ -24,18 +25,18 @@ class DynamoBackend(Backend):
>> FastAPICache.init(dynamodb)
"""
def __init__(self, table_name, region=None):
def __init__(self, table_name: str, region: Optional[str] = None) -> None:
self.session = get_session()
self.client = None # Needs async init
self.client: Optional[AioBaseClient] = None # Needs async init
self.table_name = table_name
self.region = region
async def init(self):
async def init(self) -> None:
self.client = await self.session.create_client(
"dynamodb", region_name=self.region
).__aenter__()
async def close(self):
async def close(self) -> None:
self.client = await self.client.__aexit__(None, None, None)
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
@@ -55,12 +56,12 @@ class DynamoBackend(Backend):
return 0, None
async def get(self, key) -> str:
async def get(self, key: str) -> str:
response = await self.client.get_item(TableName=self.table_name, Key={"key": {"S": key}})
if "Item" in response:
return response["Item"].get("value", {}).get("S")
async def set(self, key: str, value: str, expire: int = None):
async def set(self, key: str, value: str, expire: Optional[int] = None) -> None:
ttl = (
{
"ttl": {
@@ -88,5 +89,5 @@ class DynamoBackend(Backend):
},
)
async def clear(self, namespace: str = None, key: str = None) -> int:
async def clear(self, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
raise NotImplementedError

View File

@@ -20,13 +20,14 @@ class InMemoryBackend(Backend):
def _now(self) -> int:
return int(time.time())
def _get(self, key: str):
def _get(self, key: str) -> Value | None:
v = self._store.get(key)
if v:
if v.ttl_ts < self._now:
del self._store[key]
else:
return v
return None
async def get_with_ttl(self, key: str) -> Tuple[int, Optional[str]]:
async with self._lock:
@@ -35,17 +36,18 @@ class InMemoryBackend(Backend):
return v.ttl_ts - self._now, v.data
return 0, None
async def get(self, key: str) -> str:
async def get(self, key: str) -> Optional[str]:
async with self._lock:
v = self._get(key)
if v:
return v.data
return None
async def set(self, key: str, value: str, expire: int = None):
async def set(self, key: str, value: str, expire: Optional[int] = None) -> None:
async with self._lock:
self._store[key] = Value(value, self._now + (expire or 0))
async def clear(self, namespace: str = None, key: str = None) -> int:
async def clear(self, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
count = 0
if namespace:
keys = list(self._store.keys())

View File

@@ -1,4 +1,4 @@
from typing import Tuple
from typing import Tuple, Optional
from aiomcache import Client
@@ -9,14 +9,14 @@ class MemcachedBackend(Backend):
def __init__(self, mcache: Client):
self.mcache = mcache
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
async def get_with_ttl(self, key: str) -> Tuple[int, Optional[str]]:
return 3600, await self.mcache.get(key.encode())
async def get(self, key: str):
async def get(self, key: str) -> Optional[str]:
return await self.mcache.get(key, key.encode())
async def set(self, key: str, value: str, expire: int = None):
return await self.mcache.set(key.encode(), value.encode(), exptime=expire or 0)
async def set(self, key: str, value: str, expire: Optional[int] = None) -> None:
await self.mcache.set(key.encode(), value.encode(), exptime=expire or 0)
async def clear(self, namespace: str = None, key: str = None):
async def clear(self, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
raise NotImplementedError

View File

@@ -1,4 +1,4 @@
from typing import Tuple
from typing import Tuple, Optional
from redis.asyncio.client import Redis
@@ -13,15 +13,16 @@ class RedisBackend(Backend):
async with self.redis.pipeline(transaction=True) as pipe:
return await (pipe.ttl(key).get(key).execute())
async def get(self, key) -> str:
async def get(self, key: str) -> Optional[str]:
return await self.redis.get(key)
async def set(self, key: str, value: str, expire: int = None):
async def set(self, key: str, value: str, expire: Optional[int] = None) -> None:
return await self.redis.set(key, value, ex=expire)
async def clear(self, namespace: str = None, key: str = None) -> int:
async def clear(self, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
if namespace:
lua = f"for i, name in ipairs(redis.call('KEYS', '{namespace}:*')) do redis.call('DEL', name); end"
return await self.redis.eval(lua, numkeys=0)
elif key:
return await self.redis.delete(key)
return 0