Files
fastapi-cache/fastapi_cache/backends/memcached.py

23 lines
749 B
Python
Raw Normal View History

2022-11-07 16:39:17 +08:00
from typing import Optional, Tuple
2020-08-26 18:04:57 +08:00
from aiomcache import Client
from fastapi_cache.backends import Backend
2020-11-09 20:32:23 +08:00
class MemcachedBackend(Backend):
2020-08-26 18:04:57 +08:00
def __init__(self, mcache: Client):
self.mcache = mcache
2022-10-22 20:59:37 +04:00
async def get_with_ttl(self, key: str) -> Tuple[int, Optional[str]]:
2020-11-10 10:34:52 +08:00
return 3600, await self.mcache.get(key.encode())
2020-08-26 18:04:57 +08:00
2022-10-22 20:59:37 +04:00
async def get(self, key: str) -> Optional[str]:
2020-08-26 18:04:57 +08:00
return await self.mcache.get(key, key.encode())
2022-10-22 20:59:37 +04:00
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)
2020-11-03 18:08:06 +08:00
2022-10-22 20:59:37 +04:00
async def clear(self, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
2020-11-03 18:08:06 +08:00
raise NotImplementedError