long2ice / fastapi-cache

fastapi-cache is a tool to cache fastapi response and function result, with backends support redis and memcached.
https://github.com/long2ice/fastapi-cache
Apache License 2.0
1.3k stars 160 forks source link

How to connect to a redis cluster? #428

Closed dicolasi closed 4 months ago

dicolasi commented 4 months ago

As per title, I am struggling to make the cache working with a redis cluster ( whereas it works perfectly fine with master/slave configuration). I created a simple test file to check the connection and I either got MOVED, or timeout. Surely I got something wrong here:

import asyncio
from redis.asyncio.cluster import RedisCluster

async def test_redis_connection():
    try:
        # Connect to the Redis cluster
        redis = RedisCluster.from_url('redis://localhost:6379', encoding='utf-8', decode_responses=True)

        # Test the connection by setting and getting a value
        await redis.set('key1', 'bar')
        value = await redis.get('key1')

        # Print the result
        print(f"Connection successful, value: {value}")

        # Close the connection
        await redis.close()
    except Exception as e:
        print(f"Connection failed: {e}")

# Run the test
asyncio.run(test_redis_connection())

In my application I got this bit:

def startup() -> FastAPI:
    configure_routers(app)  # Modify the globally defined app
    app.mount("/static", StaticFiles(directory=settings.STATIC_DIR), name="static")
    redis_client = RedisCluster.from_url(url=Config.REDIS_CONNECTION_STRING, decode_responses=True)
    FastAPICache.init(RedisBackend(redis_client), prefix="fastapi-cache")
    return app

Which hangs, without doing anything.

For context, the cluster is running in my k8s cluster, I have simply port-forwarded the cluster svc to my localhost. There is no password, no ssl.

dicolasi commented 4 months ago

Silly, mistake found with the redis-cli -c command. The node pool is not reachable unless you portforward all of them. Everything works within a pod in k8s environment.