-
-
Notifications
You must be signed in to change notification settings - Fork 199
Description
🚀 Feature Request: Allow injecting a redis.Redis or ConnectionPool into RedisCache
Summary
It would be very useful if the RedisCache backend supported injecting either:
- an already-initialized
redis.Redisinstance, or - a
redis.ConnectionPool,
instead of always creating its own internal Redis client based on host, port, or url.
Motivation
In real-world applications, Redis is commonly used by multiple libraries:
- A centralized
redis.Redisinstance is often created (with a customConnectionPool, timeouts, or TLS). - Other libraries such as
limitssupport injecting Redis clients directly. - When using
flask_caching,flask_limiter,flask_session, and direct Redis access in the same app, each one creates its own Redis client and its own connection pool.
This results in:
- Redundant connection pools.
- Excessive total connections to Redis (especially painful with AWS ElastiCache connection limits).
- Less visibility and control over pool configuration (timeouts, limits, etc.).
- Difficult-to-debug connection bottlenecks in production environments.
Proposal
Allow RedisCache to accept either:
- a
redis.Redisinstance via aclientparameter, or - a
redis.ConnectionPoolvia aconnection_poolparameter.
Suggested implementation logic:
if client is not None:
self._client = client
elif connection_pool is not None:
self._client = redis.Redis(connection_pool=connection_pool)
else:
self._client = redis.Redis(host=..., port=..., ...)This would follow the conventions already supported in the redis-py library and mirror how libraries like limits allow Redis injection.
The change would be fully backward-compatible.
Example usage
import redis
from flask_caching import Cache
from flask_caching.backends.rediscache import RedisCache
# Shared connection pool
pool = redis.ConnectionPool(host="...", max_connections=20)
# Shared Redis instance
redis_client = redis.Redis(connection_pool=pool)
# Option 1: Inject Redis instance
cache = Cache(cache=RedisCache(client=redis_client))
# Option 2: Inject pool and let RedisCache create the Redis instance
cache = Cache(cache=RedisCache(connection_pool=pool))This makes it easy to reuse a single Redis connection setup across:
flask_cachingflask_limiter- other libraries
- direct application usage
All without duplicating pools or reconnecting unnecessarily.
Current limitation
Currently, the RedisCache backend does not support this. It always instantiates its own redis.Redis(...), based on host, port, or url.
Relevant line:
https://github.com/pallets-eco/flask-caching/blob/master/src/flask_caching/backends/rediscache.py#L55
There is no supported way to inject a shared client or pool.
Impact
This enhancement would:
- Allow efficient reuse of Redis resources across a Flask app.
- Reduce connection pressure on Redis backends like ElastiCache.
- Improve performance and control over Redis connection handling.
- Avoid hidden connection duplication across multiple libraries.