Skip to content

Commit 4765298

Browse files
author
Soumendra kumar sahoo
authored
README code snippets formatted to highlight properly (#1888)
1 parent 168467e commit 4765298

File tree

1 file changed

+85
-84
lines changed

1 file changed

+85
-84
lines changed

README.md

Lines changed: 85 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -968,25 +968,25 @@ instance can be created:
968968
- Using 'host' and 'port' arguments:
969969

970970
``` 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())
974974
[[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>>>]]
975975
```
976976
- Using the Redis URL specification:
977977

978978
``` 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")
981981
```
982982

983983
- Directly, via the ClusterNode class:
984984

985985
``` 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)
990990
```
991991

992992
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,
10161016
'Specifying Target Nodes'.
10171017

10181018
``` 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()
10311031
```
10321032

10331033
**Specifying Target Nodes:**
@@ -1043,18 +1043,18 @@ the client will be able to resolve the nodes flag again with the new topology
10431043
and attempt to retry executing the command.
10441044

10451045
``` 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)
10581058
```
10591059

10601060
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
10641064
the relevant cluster or connection error will be returned.
10651065

10661066
``` 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)
10731073
```
10741074

10751075
In addition, the RedisCluster instance can query the Redis instance of a
10761076
specific node and execute commands on that node directly. The Redis client,
10771077
however, does not handle cluster failures and retries.
10781078

10791079
``` 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']
10891089
```
10901090

10911091
**Multi-key commands:**
@@ -1105,14 +1105,14 @@ operations batch the keys according to their hash value, and then each batch is
11051105
sent separately to the slot's owner.
11061106

11071107
``` 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']
11161116
```
11171117

11181118
**Cluster PubSub:**
@@ -1135,11 +1135,11 @@ See [redis-py-cluster documentation](https://redis-py-cluster.readthedocs.io/en/
11351135
for more.
11361136

11371137
``` 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))
11431143
```
11441144

11451145
**Read Only Mode**
@@ -1157,20 +1157,20 @@ target_nodes='replicas', and read-write access can be restored by calling the
11571157
readwrite() method.
11581158

11591159
``` 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')
11741174
```
11751175

11761176
**Cluster Pipeline**
@@ -1187,16 +1187,17 @@ by significantly reducing the the number of network round trips between the
11871187
client and the server.
11881188

11891189
``` 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']
11991199
```
1200+
12001201
Please note:
12011202
- RedisCluster pipelines currently only support key-based commands.
12021203
- The pipeline gets its 'read_from_replicas' value from the cluster's parameter.

0 commit comments

Comments
 (0)