mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
enabled redis in cluster mode
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from redis.asyncio.client import Redis
|
||||
from redis.asyncio.client import AbstractRedis
|
||||
from redis.asyncio.cluster import AbstractRedisCluster
|
||||
|
||||
from fastapi_cache.backends import Backend
|
||||
|
||||
|
||||
class RedisBackend(Backend):
|
||||
def __init__(self, redis: Redis):
|
||||
def __init__(self, redis: AbstractRedis):
|
||||
self.redis = redis
|
||||
self.is_cluster = isinstance(redis, AbstractRedisCluster)
|
||||
|
||||
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
|
||||
async with self.redis.pipeline(transaction=True) as pipe:
|
||||
async with self.redis.pipeline(transaction=not self.is_cluster) as pipe:
|
||||
return await (pipe.ttl(key).get(key).execute())
|
||||
|
||||
async def get(self, key: str) -> Optional[str]:
|
||||
@@ -25,4 +27,4 @@ class RedisBackend(Backend):
|
||||
return await self.redis.eval(lua, numkeys=0)
|
||||
elif key:
|
||||
return await self.redis.delete(key)
|
||||
return 0
|
||||
return 0
|
||||
Reference in New Issue
Block a user