You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: cluster.markdown
+16-18Lines changed: 16 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Redis introduces cluster support as of version 3.0.0, and to communicate with a
8
8
To maintain consistency with the RedisArray class, one can create and connect to a cluster either by passing it one or more 'seed' nodes, or by defining these in redis.ini as a 'named' cluster.
9
9
10
10
#### Declaring a cluster with an array of seeds
11
-
<pre>
11
+
~~~php
12
12
// Create a cluster setting two nodes as seeds
13
13
$obj_cluster = new RedisCluster(NULL, Array('host:7000', 'host:7001', 'host:7003'));
$obj_cluster = new RedisCluster(NULL, Array("host:7000", "host:7001"), 1.5, 1.5, true, "password");
24
-
25
-
</pre>
24
+
~~~
26
25
27
26
#### Loading a cluster configuration by name
28
27
In order to load a named array, one must first define the seed nodes in redis.ini. The following lines would define the cluster 'mycluster', and be loaded automatically by phpredis.
With the exception of commands that are directed to a specific node, each command executed via RedisCluster is processed through a command loop, where we make the request, handle any MOVED or ASK redirection, and repeat if necessary. This continues until one of the following conditions is met:
84
83
85
-
1. We fail to communicate with *any* node that we are aware of, in which case a ```RedisClusterException``` is raised.
84
+
1. We fail to communicate with *any* node that we are aware of, in which case a ~~~RedisClusterException~~~ is raised.
86
85
2. We have been bounced around longer than the timeout which was set on construction.
87
-
3. Redis cluster returns to us a ```CLUSTERDOWN``` error, in which case a ```RedisClusterException``` is raised.
86
+
3. Redis cluster returns to us a ~~~CLUSTERDOWN~~~ error, in which case a ~~~RedisClusterException~~~ is raised.
88
87
4. We receive a valid response, in which case the data is returned to the caller.
89
88
90
89
## Transactions
91
90
The RedisCluster class fully supports MULTI ... EXEC transactions, including commands such as MGET and MSET which operate on multiple keys. There are considerations that must be taken into account here however.
92
91
93
-
When you call ```RedisCluster->multi()```, the cluster is put into a MULTI state, but the MULTI command is not delivered to any nodes until a key is requested on that node. In addition, calls to EXEC will always return an array (even in the event that a transaction to a given node failed), as the commands can be going to any number of nodes depending on what is called.
92
+
When you call ~~~RedisCluster->multi()~~~, the cluster is put into a MULTI state, but the MULTI command is not delivered to any nodes until a key is requested on that node. In addition, calls to EXEC will always return an array (even in the event that a transaction to a given node failed), as the commands can be going to any number of nodes depending on what is called.
// This will always return an array, even in the event of a failed transaction
110
109
// on one of the nodes, in which case that element will be FALSE
111
110
print_r($obj_cluster->exec());
112
-
</pre>
111
+
~~~
113
112
114
113
## Pipelining
115
114
The RedisCluster class does not support pipelining as there is no way to detect whether the keys still live where our map indicates that they do and would therefore be inherently unsafe. It would be possible to implement this support as an option if there is demand for such a feature.
@@ -122,11 +121,11 @@ For all of these multiple key commands (with the exception of MGET and MSET), th
122
121
### MGET and MSET
123
122
RedisCluster has specialized processing for MGET and MSET which allows you to send any number of keys (hashing to whichever slots) without having to consider where they live. The way this works, is that the RedisCluster class will split the command as it iterates through keys, delivering a subset of commands per each key's slot.
124
123
125
-
<pre>
124
+
~~~php
126
125
// This will be delivered in two commands. First for all of the {hash1} keys,
In the case of all commands which need to be directed at a node, the calling convention is identical to the Redis call, except that they require an additional (first) argument in order to deliver the command. Following is a list of each of these commands:
@@ -168,7 +166,7 @@ You can use the cluster functionality of phpredis to store PHP session informati
168
166
169
167
To do this, you must configure your `session.save_handler` and `session.save_path` INI variables to give phpredis enough information to communicate with the cluster.
0 commit comments