Skip to content

Feature Request: Allow injecting a redis.Redis or ConnectionPool into RedisCache #629

@icarocd

Description

@icarocd

🚀 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.Redis instance, 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.Redis instance is often created (with a custom ConnectionPool, timeouts, or TLS).
  • Other libraries such as limits support 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.Redis instance via a client parameter, or
  • a redis.ConnectionPool via a connection_pool parameter.

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_caching
  • flask_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions