From 9ed2132af28652e964b6a4a20d387580d1081003 Mon Sep 17 00:00:00 2001 From: Theo Despoudis Date: Mon, 7 Jan 2019 14:21:55 +0000 Subject: Add READONLY and READWRITE commands --- CHANGES | 1 + redis/client.py | 12 ++++++++++-- tests/test_commands.py | 13 +++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 9f15d44..5256faf 100644 --- a/CHANGES +++ b/CHANGES @@ -16,6 +16,7 @@ AuthenticationError is now a subclass of ConnectionError, which will cause the connection to be disconnected and cleaned up appropriately. #923 + * Add READONLY and READWRITE commands. Thanks @theodesp. #1114 * 3.2.1 * Fix SentinelConnectionPool to work in multiprocess/forked environments. * 3.2.0 diff --git a/redis/client.py b/redis/client.py index b87fcf6..aa1ca7e 100755 --- a/redis/client.py +++ b/redis/client.py @@ -499,8 +499,8 @@ class Redis(object): string_keys_to_dict('SORT', sort_return_tuples), string_keys_to_dict('ZSCORE ZINCRBY GEODIST', float_or_none), string_keys_to_dict( - 'FLUSHALL FLUSHDB LSET LTRIM MSET PFMERGE RENAME ' - 'SAVE SELECT SHUTDOWN SLAVEOF SWAPDB WATCH UNWATCH', + 'FLUSHALL FLUSHDB LSET LTRIM MSET PFMERGE READONLY READWRITE ' + 'RENAME SAVE SELECT SHUTDOWN SLAVEOF SWAPDB WATCH UNWATCH ', bool_ok ), string_keys_to_dict('BLPOP BRPOP', lambda r: r and tuple(r) or None), @@ -928,6 +928,14 @@ class Redis(object): raise DataError("CLIENT PAUSE timeout must be an integer") return self.execute_command('CLIENT PAUSE', str(timeout)) + def readwrite(self): + "Disables read queries for a connection to a Redis Cluster slave node" + return self.execute_command('READWRITE') + + def readonly(self): + "Enables read queries for a connection to a Redis Cluster replica node" + return self.execute_command('READONLY') + def config_get(self, pattern="*"): "Return a dictionary of configuration based on the ``pattern``" return self.execute_command('CONFIG GET', pattern) diff --git a/tests/test_commands.py b/tests/test_commands.py index 0815427..64cabdf 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1643,6 +1643,19 @@ class TestRedisCommands(object): assert isinstance(mock_cluster_resp_slaves.cluster( 'slaves', 'nodeid'), dict) + @skip_if_server_version_lt('3.0.0') + def test_readwrite(self, r): + assert r.readwrite() + + @skip_if_server_version_lt('3.0.0') + def test_readonly_invalid_cluster_state(self, r): + with pytest.raises(exceptions.RedisError): + r.readonly() + + @skip_if_server_version_lt('3.0.0') + def test_readonly(self, mock_cluster_resp_ok): + assert mock_cluster_resp_ok.readonly() is True + # GEO COMMANDS @skip_if_server_version_lt('3.2.0') def test_geoadd(self, r): -- cgit v1.2.1