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

Cluster schema support #3152

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ class Redis(RedisModuleCommands, CoreCommands, SentinelCommands):
"""

@classmethod
def from_url(cls, url: str, **kwargs) -> "Redis":
def from_url(cls, url: str, **kwargs) -> Union["Redis", "RedisCluster"]:
"""
Return a Redis client object configured from the given URL
Return a Redis or RedisCluster client object configured from the given URL

For example::

redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[username@]/path/to/socket.sock?db=0[&password=password]
rediscluster://[[username]:[password]]@localhost:6379/0

Three URL schemes are supported:

Expand Down Expand Up @@ -146,6 +147,9 @@ class initializer. In the case of conflicting arguments, querystring
arguments always win.

"""
if url.startswith("rediscluster"):
from .cluster import RedisCluster
return RedisCluster.from_url(url, **kwargs)
single_connection_client = kwargs.pop("single_connection_client", False)
connection_pool = ConnectionPool.from_url(url, **kwargs)
client = cls(
Expand Down
4 changes: 3 additions & 1 deletion redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,8 @@ def parse_url(url):
if not (
url.startswith("redis://")
or url.startswith("rediss://")
or url.startswith("rediscluster://")
or url.startswith("redisclusters://")
or url.startswith("unix://")
):
raise ValueError(
Expand Down Expand Up @@ -1004,7 +1006,7 @@ def parse_url(url):
except (AttributeError, ValueError):
pass

if url.scheme == "rediss":
if url.scheme in ["rediss", "redisclusters"]:
kwargs["connection_class"] = SSLConnection

return kwargs
Expand Down
16 changes: 16 additions & 0 deletions tests/test_rediscluster_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Xiang Wang <ramwin@qq.com>


import unittest

from redis import Redis
from redis.cluster import RedisCluster


class Test(unittest.TestCase):

def test(self):
res = Redis.from_url("rediscluster://localhost:7000")
self.assertIsInstance(res, RedisCluster)