@@ -968,25 +968,25 @@ instance can be created:
968
968
- Using 'host' and 'port' arguments:
969
969
970
970
``` pycon
971
- >>> from redis.cluster import RedisCluster as Redis
972
- >>> rc = Redis(host='localhost', port=6379)
973
- >>> print(rc.get_nodes())
971
+ >>> from redis.cluster import RedisCluster as Redis
972
+ >>> rc = Redis(host = ' localhost' , port = 6379 )
973
+ >>> print (rc.get_nodes())
974
974
[[host=127.0.0.1,port=6379,name=127.0.0.1:6379,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>], [host=127.0.0.1,port=6378,name=127.0.0.1:6378,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6378,db=0>>>], [host=127.0.0.1,port=6377,name=127.0.0.1:6377,server_type=replica,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6377,db=0>>>]]
975
975
```
976
976
- Using the Redis URL specification:
977
977
978
978
``` pycon
979
- >>> from redis.cluster import RedisCluster as Redis
980
- >>> rc = Redis.from_url("redis://localhost:6379/0")
979
+ >>> from redis.cluster import RedisCluster as Redis
980
+ >>> rc = Redis.from_url(" redis://localhost:6379/0" )
981
981
```
982
982
983
983
- Directly, via the ClusterNode class:
984
984
985
985
``` pycon
986
- >>> from redis.cluster import RedisCluster as Redis
987
- >>> from redis.cluster import ClusterNode
988
- >>> nodes = [ClusterNode('localhost', 6379), ClusterNode('localhost', 6378)]
989
- >>> rc = Redis(startup_nodes=nodes)
986
+ >>> from redis.cluster import RedisCluster as Redis
987
+ >>> from redis.cluster import ClusterNode
988
+ >>> nodes = [ClusterNode(' localhost' , 6379 ), ClusterNode(' localhost' , 6378 )]
989
+ >>> rc = Redis(startup_nodes = nodes)
990
990
```
991
991
992
992
When a RedisCluster instance is being created it first attempts to establish a
@@ -1016,18 +1016,18 @@ The 'target_nodes' parameter is explained in the following section,
1016
1016
'Specifying Target Nodes'.
1017
1017
1018
1018
``` pycon
1019
- >>> # target-nodes: the node that holds 'foo1's key slot
1020
- >>> rc.set('foo1', 'bar1')
1021
- >>> # target-nodes: the node that holds 'foo2's key slot
1022
- >>> rc.set('foo2', 'bar2')
1023
- >>> # target-nodes: the node that holds 'foo1's key slot
1024
- >>> print(rc.get('foo1'))
1025
- b'bar'
1026
- >>> # target-node: default-node
1027
- >>> print(rc.keys())
1028
- [b'foo1']
1029
- >>> # target-node: default-node
1030
- >>> rc.ping()
1019
+ >>> # target-nodes: the node that holds 'foo1's key slot
1020
+ >>> rc.set(' foo1' , ' bar1' )
1021
+ >>> # target-nodes: the node that holds 'foo2's key slot
1022
+ >>> rc.set(' foo2' , ' bar2' )
1023
+ >>> # target-nodes: the node that holds 'foo1's key slot
1024
+ >>> print (rc.get(' foo1' ))
1025
+ b'bar'
1026
+ >>> # target-node: default-node
1027
+ >>> print (rc.keys())
1028
+ [b'foo1']
1029
+ >>> # target-node: default-node
1030
+ >>> rc.ping()
1031
1031
```
1032
1032
1033
1033
** Specifying Target Nodes:**
@@ -1043,18 +1043,18 @@ the client will be able to resolve the nodes flag again with the new topology
1043
1043
and attempt to retry executing the command.
1044
1044
1045
1045
``` pycon
1046
- >>> from redis.cluster import RedisCluster as Redis
1047
- >>> # run cluster-meet command on all of the cluster's nodes
1048
- >>> rc.cluster_meet('127.0.0.1', 6379, target_nodes=Redis.ALL_NODES)
1049
- >>> # ping all replicas
1050
- >>> rc.ping(target_nodes=Redis.REPLICAS)
1051
- >>> # ping a random node
1052
- >>> rc.ping(target_nodes=Redis.RANDOM)
1053
- >>> # get the keys from all cluster nodes
1054
- >>> rc.keys(target_nodes=Redis.ALL_NODES)
1055
- [b'foo1', b'foo2']
1056
- >>> # execute bgsave in all primaries
1057
- >>> rc.bgsave(Redis.PRIMARIES)
1046
+ >>> from redis.cluster import RedisCluster as Redis
1047
+ >>> # run cluster-meet command on all of the cluster's nodes
1048
+ >>> rc.cluster_meet(' 127.0.0.1' , 6379 , target_nodes = Redis.ALL_NODES )
1049
+ >>> # ping all replicas
1050
+ >>> rc.ping(target_nodes = Redis.REPLICAS )
1051
+ >>> # ping a random node
1052
+ >>> rc.ping(target_nodes = Redis.RANDOM )
1053
+ >>> # get the keys from all cluster nodes
1054
+ >>> rc.keys(target_nodes = Redis.ALL_NODES )
1055
+ [b'foo1', b'foo2']
1056
+ >>> # execute bgsave in all primaries
1057
+ >>> rc.bgsave(Redis.PRIMARIES )
1058
1058
```
1059
1059
1060
1060
You could also pass ClusterNodes directly if you want to execute a command on a
@@ -1064,28 +1064,28 @@ will not be made, since the passed target node/s may no longer be valid, and
1064
1064
the relevant cluster or connection error will be returned.
1065
1065
1066
1066
``` pycon
1067
- >>> node = rc.get_node('localhost', 6379)
1068
- >>> # Get the keys only for that specific node
1069
- >>> rc.keys(target_nodes=node)
1070
- >>> # get Redis info from a subset of primaries
1071
- >>> subset_primaries = [node for node in rc.get_primaries() if node.port > 6378]
1072
- >>> rc.info(target_nodes=subset_primaries)
1067
+ >>> node = rc.get_node(' localhost' , 6379 )
1068
+ >>> # Get the keys only for that specific node
1069
+ >>> rc.keys(target_nodes = node)
1070
+ >>> # get Redis info from a subset of primaries
1071
+ >>> subset_primaries = [node for node in rc.get_primaries() if node.port > 6378 ]
1072
+ >>> rc.info(target_nodes = subset_primaries)
1073
1073
```
1074
1074
1075
1075
In addition, the RedisCluster instance can query the Redis instance of a
1076
1076
specific node and execute commands on that node directly. The Redis client,
1077
1077
however, does not handle cluster failures and retries.
1078
1078
1079
1079
``` pycon
1080
- >>> cluster_node = rc.get_node(host='localhost', port=6379)
1081
- >>> print(cluster_node)
1082
- [host=127.0.0.1,port=6379,name=127.0.0.1:6379,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>]
1083
- >>> r = cluster_node.redis_connection
1084
- >>> r.client_list()
1085
- [{'id': '276', 'addr': '127.0.0.1:64108', 'fd': '16', 'name': '', 'age': '0', 'idle': '0', 'flags': 'N', 'db': '0', 'sub': '0', 'psub': '0', 'multi': '-1', 'qbuf': '26', 'qbuf-free': '32742', 'argv-mem': '10', 'obl': '0', 'oll': '0', 'omem': '0', 'tot-mem': '54298', 'events': 'r', 'cmd': 'client', 'user': 'default'}]
1086
- >>> # Get the keys only for that specific node
1087
- >>> r.keys()
1088
- [b'foo1']
1080
+ >>> cluster_node = rc.get_node(host = ' localhost' , port = 6379 )
1081
+ >>> print (cluster_node)
1082
+ [host=127.0.0.1,port=6379,name=127.0.0.1:6379,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>]
1083
+ >>> r = cluster_node.redis_connection
1084
+ >>> r.client_list()
1085
+ [{'id': '276', 'addr': '127.0.0.1:64108', 'fd': '16', 'name': '', 'age': '0', 'idle': '0', 'flags': 'N', 'db': '0', 'sub': '0', 'psub': '0', 'multi': '-1', 'qbuf': '26', 'qbuf-free': '32742', 'argv-mem': '10', 'obl': '0', 'oll': '0', 'omem': '0', 'tot-mem': '54298', 'events': 'r', 'cmd': 'client', 'user': 'default'}]
1086
+ >>> # Get the keys only for that specific node
1087
+ >>> r.keys()
1088
+ [b'foo1']
1089
1089
```
1090
1090
1091
1091
** Multi-key commands:**
@@ -1105,14 +1105,14 @@ operations batch the keys according to their hash value, and then each batch is
1105
1105
sent separately to the slot's owner.
1106
1106
1107
1107
``` pycon
1108
- # Atomic operations can be used when all keys are mapped to the same slot
1109
- >>> rc.mset({'{foo}1': 'bar1', '{foo}2': 'bar2'})
1110
- >>> rc.mget('{foo}1', '{foo}2')
1111
- [b'bar1', b'bar2']
1112
- # Non-atomic multi-key operations splits the keys into different slots
1113
- >>> rc.mset_nonatomic({'foo': 'value1', 'bar': 'value2', 'zzz': 'value3')
1114
- >>> rc.mget_nonatomic('foo', 'bar', 'zzz')
1115
- [b'value1', b'value2', b'value3']
1108
+ # Atomic operations can be used when all keys are mapped to the same slot
1109
+ >>> rc.mset({' {foo} 1' : ' bar1' , ' {foo} 2' : ' bar2' })
1110
+ >>> rc.mget(' {foo} 1' , ' {foo} 2' )
1111
+ [b'bar1', b'bar2']
1112
+ # Non-atomic multi-key operations splits the keys into different slots
1113
+ >>> rc.mset_nonatomic({' foo' : ' value1' , ' bar' : ' value2' , ' zzz' : ' value3' )
1114
+ >>> rc.mget_nonatomic(' foo' , ' bar' , ' zzz' )
1115
+ [b'value1', b'value2', b'value3']
1116
1116
```
1117
1117
1118
1118
** Cluster PubSub:**
@@ -1135,11 +1135,11 @@ See [redis-py-cluster documentation](https://redis-py-cluster.readthedocs.io/en/
1135
1135
for more.
1136
1136
1137
1137
``` pycon
1138
- >>> p1 = rc.pubsub()
1139
- # p1 connection will be set to the node that holds 'foo' keyslot
1140
- >>> p1.subscribe('foo')
1141
- # p2 connection will be set to node 'localhost:6379'
1142
- >>> p2 = rc.pubsub(rc.get_node('localhost', 6379))
1138
+ >>> p1 = rc.pubsub()
1139
+ # p1 connection will be set to the node that holds 'foo' keyslot
1140
+ >>> p1.subscribe(' foo' )
1141
+ # p2 connection will be set to node 'localhost:6379'
1142
+ >>> p2 = rc.pubsub(rc.get_node(' localhost' , 6379 ))
1143
1143
```
1144
1144
1145
1145
** Read Only Mode**
@@ -1157,20 +1157,20 @@ target_nodes='replicas', and read-write access can be restored by calling the
1157
1157
readwrite() method.
1158
1158
1159
1159
``` pycon
1160
- >>> from cluster import RedisCluster as Redis
1161
- # Use 'debug' log level to print the node that the command is executed on
1162
- >>> rc_readonly = Redis(startup_nodes=startup_nodes,
1163
- read_from_replicas=True)
1164
- >>> rc_readonly.set('{foo}1', 'bar1')
1165
- >>> for i in range(0, 4):
1166
- # Assigns read command to the slot's hosts in a Round-Robin manner
1167
- >>> rc_readonly.get('{foo}1')
1168
- # set command would be directed only to the slot's primary node
1169
- >>> rc_readonly.set('{foo}2', 'bar2')
1170
- # reset READONLY flag
1171
- >>> rc_readonly.readwrite(target_nodes='replicas')
1172
- # now the get command would be directed only to the slot's primary node
1173
- >>> rc_readonly.get('{foo}1')
1160
+ >>> from cluster import RedisCluster as Redis
1161
+ # Use 'debug' log level to print the node that the command is executed on
1162
+ >>> rc_readonly = Redis(startup_nodes = startup_nodes,
1163
+ ... read_from_replicas= True )
1164
+ >>> rc_readonly.set(' {foo} 1' , ' bar1' )
1165
+ >>> for i in range (0 , 4 ):
1166
+ ... # Assigns read command to the slot's hosts in a Round-Robin manner
1167
+ ... rc_readonly.get(' {foo} 1' )
1168
+ # set command would be directed only to the slot's primary node
1169
+ >>> rc_readonly.set(' {foo} 2' , ' bar2' )
1170
+ # reset READONLY flag
1171
+ >>> rc_readonly.readwrite(target_nodes = ' replicas' )
1172
+ # now the get command would be directed only to the slot's primary node
1173
+ >>> rc_readonly.get(' {foo} 1' )
1174
1174
```
1175
1175
1176
1176
** Cluster Pipeline**
@@ -1187,16 +1187,17 @@ by significantly reducing the the number of network round trips between the
1187
1187
client and the server.
1188
1188
1189
1189
``` pycon
1190
- >>> with rc.pipeline() as pipe:
1191
- >>> pipe.set('foo', 'value1')
1192
- >>> pipe.set('bar', 'value2')
1193
- >>> pipe.get('foo')
1194
- >>> pipe.get('bar')
1195
- >>> print(pipe.execute())
1196
- [True, True, b'value1', b'value2']
1197
- >>> pipe.set('foo1', 'bar1').get('foo1').execute()
1198
- [True, b'bar1']
1190
+ >>> with rc.pipeline() as pipe:
1191
+ ... pipe.set(' foo' , ' value1' )
1192
+ ... pipe.set(' bar' , ' value2' )
1193
+ ... pipe.get(' foo' )
1194
+ ... pipe.get(' bar' )
1195
+ ... print (pipe.execute())
1196
+ [True, True, b'value1', b'value2']
1197
+ ... pipe.set(' foo1' , ' bar1' ).get(' foo1' ).execute()
1198
+ [True, b'bar1']
1199
1199
```
1200
+
1200
1201
Please note:
1201
1202
- RedisCluster pipelines currently only support key-based commands.
1202
1203
- The pipeline gets its 'read_from_replicas' value from the cluster's parameter.
0 commit comments