mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
enabled redis in cluster mode
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
from typing import Optional, Tuple
|
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
|
from fastapi_cache.backends import Backend
|
||||||
|
|
||||||
|
|
||||||
class RedisBackend(Backend):
|
class RedisBackend(Backend):
|
||||||
def __init__(self, redis: Redis):
|
def __init__(self, redis: AbstractRedis):
|
||||||
self.redis = redis
|
self.redis = redis
|
||||||
|
self.is_cluster = isinstance(redis, AbstractRedisCluster)
|
||||||
|
|
||||||
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=True) as pipe:
|
async with self.redis.pipeline(transaction=not self.is_cluster) as pipe:
|
||||||
return await (pipe.ttl(key).get(key).execute())
|
return await (pipe.ttl(key).get(key).execute())
|
||||||
|
|
||||||
async def get(self, key: str) -> Optional[str]:
|
async def get(self, key: str) -> Optional[str]:
|
||||||
|
|||||||
Reference in New Issue
Block a user