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

23 lines
725 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
async def get_with_ttl(self, key: str) -> Tuple[int, Optional[bytes]]:
return 3600, await self.get(key)
2020-08-26 18:04:57 +08:00
async def get(self, key: str) -> Optional[bytes]:
return await self.mcache.get(key.encode())
2020-08-26 18:04:57 +08:00
async def set(self, key: str, value: bytes, expire: Optional[int] = None) -> None:
await self.mcache.set(key.encode(), value, 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