Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use redis connection string for cluster #550

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions src/flask_caching/backends/rediscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(
db=db,
default_timeout=default_timeout,
key_prefix=key_prefix,
**kwargs
**kwargs,
)

@classmethod
Expand Down Expand Up @@ -176,7 +176,7 @@ def __init__(
password=password,
db=db,
sentinel_kwargs=sentinel_kwargs,
**kwargs
**kwargs,
)

self._write_client = sentinel.master_for(master)
Expand Down Expand Up @@ -235,27 +235,30 @@ def __init__(
except ImportError as e:
raise RuntimeError("no redis.cluster module found") from e

try:
nodes = [(node.split(":")) for node in cluster.split(",")]
startup_nodes = [
ClusterNode(node[0].strip(), node[1].strip()) for node in nodes
]
except IndexError as e:
raise ValueError(
"Please give the correct cluster argument "
"e.g. host1:port1,host2:port2,host3:port3"
) from e

# Skips the check of cluster-require-full-coverage config,
# useful for clusters without the CONFIG command (like aws)
skip_full_coverage_check = kwargs.pop("skip_full_coverage_check", True)

cluster = RedisCluster(
startup_nodes=startup_nodes,
password=password,
skip_full_coverage_check=skip_full_coverage_check,
**kwargs
)
if kwargs.get("redis_url", None):
cluster = RedisCluster.from_url(kwargs["redis_url"])
else:
try:
nodes = [(node.split(":")) for node in cluster.split(",")]
startup_nodes = [
ClusterNode(node[0].strip(), node[1].strip()) for node in nodes
]
except IndexError as e:
raise ValueError(
"Please give the correct cluster argument "
"e.g. host1:port1,host2:port2,host3:port3"
) from e

# Skips the check of cluster-require-full-coverage config,
# useful for clusters without the CONFIG command (like aws)
skip_full_coverage_check = kwargs.pop("skip_full_coverage_check", True)

cluster = RedisCluster(
startup_nodes=startup_nodes,
password=password,
skip_full_coverage_check=skip_full_coverage_check,
**kwargs,
)

self._write_client = cluster
self._read_client = cluster
Expand All @@ -268,6 +271,7 @@ def factory(cls, app, config, args, kwargs):
password=config.get("CACHE_REDIS_PASSWORD", ""),
default_timeout=config.get("CACHE_DEFAULT_TIMEOUT", 300),
key_prefix=config.get("CACHE_KEY_PREFIX", ""),
redis_url=config.get("CACHE_REDIS_URL", ""),
)
)
return cls(*args, **kwargs)
Loading