mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
first commit
This commit is contained in:
16
fastapi_cache/backends/__init__.py
Normal file
16
fastapi_cache/backends/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import abc
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class Backend:
|
||||
@abc.abstractmethod
|
||||
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def get(self, key: str) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def set(self, key: str, value: str, expire: int = None):
|
||||
raise NotImplementedError
|
||||
19
fastapi_cache/backends/mencache.py
Normal file
19
fastapi_cache/backends/mencache.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from typing import Tuple
|
||||
|
||||
from aiomcache import Client
|
||||
|
||||
from fastapi_cache.backends import Backend
|
||||
|
||||
|
||||
class MemcacheBackend(Backend):
|
||||
def __init__(self, mcache: Client):
|
||||
self.mcache = mcache
|
||||
|
||||
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
|
||||
return 0, await self.mcache.get(key.encode())
|
||||
|
||||
async def get(self, key: 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)
|
||||
22
fastapi_cache/backends/redis.py
Normal file
22
fastapi_cache/backends/redis.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from typing import Tuple
|
||||
|
||||
from aioredis import Redis
|
||||
|
||||
from fastapi_cache.backends import Backend
|
||||
|
||||
|
||||
class RedisBackend(Backend):
|
||||
def __init__(self, redis: Redis):
|
||||
self.redis = redis
|
||||
|
||||
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
|
||||
p = self.redis.pipeline()
|
||||
p.ttl(key)
|
||||
p.get(key)
|
||||
return await p.execute()
|
||||
|
||||
async def get(self, key) -> str:
|
||||
return await self.redis.get(key)
|
||||
|
||||
async def set(self, key: str, value: str, expire: int = None):
|
||||
return await self.redis.set(key, value, expire=expire)
|
||||
Reference in New Issue
Block a user