The backend needs an async redis client with a pipeline method

The Abstract* classes lack the pipeline method so are not sufficient.
This commit is contained in:
Martijn Pieters
2023-04-27 16:33:43 +01:00
parent 6af14be049
commit 72c42325ab

View File

@@ -1,15 +1,15 @@
from typing import Optional, Tuple from typing import Optional, Tuple
from redis.asyncio.client import AbstractRedis from redis.asyncio.client import Redis
from redis.asyncio.cluster import AbstractRedisCluster from redis.asyncio.cluster import RedisCluster
from fastapi_cache.backends import Backend from fastapi_cache.backends import Backend
class RedisBackend(Backend): class RedisBackend(Backend):
def __init__(self, redis: AbstractRedis): def __init__(self, redis: Redis[str] | RedisCluster[str]):
self.redis = redis self.redis = redis
self.is_cluster = isinstance(redis, AbstractRedisCluster) self.is_cluster: bool = isinstance(redis, RedisCluster)
async def get_with_ttl(self, key: str) -> Tuple[int, str]: async def get_with_ttl(self, key: str) -> Tuple[int, str]:
async with self.redis.pipeline(transaction=not self.is_cluster) as pipe: async with self.redis.pipeline(transaction=not self.is_cluster) as pipe: