Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7165364

Browse files
Additional cluster based session additions
* Initial documentation for cluster based session handler * Updated how we handle persistent option (so we can use 1/yes/true)
1 parent c5811b1 commit 7165364

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

cluster.markdown

+31-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ $obj_cluster = new RedisCluster(NULL, Array('host:7000', 'host:7001', 'host:7003
1414

1515
// Connect and specify timeout and read_timeout
1616
$obj_cluster = new RedisCluster(
17-
NULL, Array("host:7000", "host:7001", 1.5, 1.5);
17+
NULL, Array("host:7000", "host:7001"), 1.5, 1.5
1818
);
19+
20+
// Connect with read/write timeout as well as specify that phpredis should use
21+
// persistent connections to each node.
22+
$obj_cluster = new RedisCluster(
23+
NULL, Array("host:7000", "host:7001"), 1.5, 1.5, true
24+
);
25+
1926
</pre>
2027

2128
#### Loading a cluster configuration by name
@@ -146,3 +153,26 @@ In the case of all commands which need to be directed at a node, the calling con
146153
14. RANDOMKEY
147154
15. PING
148155

156+
## Session Handler
157+
You can use the cluster functionality of phpredis to store PHP session information in a Redis cluster as you can with a non cluster-enabled Redis instance.
158+
159+
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.
160+
161+
~~~
162+
session.save_handler = rediscluster
163+
session.save_path = "seed[]=host1:port1&seed[]=host2:port2&seed[]=hostN:portN&timeout=2&read_timeout=2&failover=error&persistent=1"
164+
~~~
165+
166+
### session.session_handler
167+
Set this variable to "rediscluster" to inform phpredis that this is a cluster instance.
168+
169+
### session.save_path
170+
The save path for cluster based session storage takes the form of a PHP GET request, and requires that you specify at least on `seed` node. Other options you can specify are as follows:
171+
172+
* _timeout (double)_: The amount of time phpredis will wait when connecting or writing to the cluster.
173+
* _read_timeout (double)_: The amount of time phpredis will wait for a result from the cluster.
174+
* _persistent_: Tells phpredis whether persistent connections should be used.
175+
* _distribute_: phpredis will randomly distribute session reads between masters and any attached slaves (load balancing).
176+
* _failover (string)_: How phpredis should distribute session reads between master and slave nodes.
177+
* * _none_ : phpredis will only communicate with master nodes
178+
* * _error_: phpredis will communicate with master nodes unless one failes, in which case an attempt will be made to read session information from a slave.

redis_session.c

+26-7
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,29 @@ static void session_conf_timeout(HashTable *ht_conf, const char *key, int key_le
477477
}
478478
}
479479

480+
/* Simple helper to retreive a boolean (0 or 1) value from a string stored in our
481+
* session.save_path variable. This is so the user can use 0, 1, or 'true',
482+
* 'false' */
483+
static void session_conf_bool(HashTable *ht_conf, char *key, int keylen,
484+
int *retval) {
485+
zval **z_val;
486+
char *str;
487+
int strlen;
488+
489+
/* See if we have the option, and it's a string */
490+
if (zend_hash_find(ht_conf, key, keylen, (void**)&z_val) == SUCCESS &&
491+
Z_TYPE_PP(z_val) == IS_STRING)
492+
{
493+
str = Z_STRVAL_PP(z_val);
494+
strlen = Z_STRLEN_PP(z_val);
495+
496+
/* true/yes/1 are treated as true. Everything else is false */
497+
*retval = (strlen == 4 && !strncasecmp(str, "true", 4)) ||
498+
(strlen == 3 && !strncasecmp(str, "yes", 3)) ||
499+
(strlen == 1 && !strncasecmp(str, "1", 1));
500+
}
501+
}
502+
480503
/* Prefix a session key */
481504
static char *cluster_session_key(redisCluster *c, const char *key, int keylen,
482505
int *skeylen, short *slot) {
@@ -524,6 +547,9 @@ PS_OPEN_FUNC(rediscluster) {
524547
session_conf_timeout(ht_conf, "timeout", sizeof("timeout"), &timeout);
525548
session_conf_timeout(ht_conf, "read_timeout", sizeof("read_timeout"), &read_timeout);
526549

550+
/* Grab persistent option */
551+
session_conf_bool(ht_conf, "persistent", sizeof("persistent"), &persistent);
552+
527553
/* Sanity check on our timeouts */
528554
if (timeout < 0 || read_timeout < 0) {
529555
php_error_docref(NULL TSRMLS_CC, E_WARNING,
@@ -555,13 +581,6 @@ PS_OPEN_FUNC(rediscluster) {
555581
}
556582
}
557583

558-
/* Check for persistent option */
559-
if (zend_hash_find(ht_conf, "persistent", sizeof("persistent"), (void **)&z_val) == SUCCESS &&
560-
Z_TYPE_PP(z_val) == IS_STRING)
561-
{
562-
persistent = atoi(z_val);
563-
}
564-
565584
c = cluster_create(timeout, read_timeout, failover, persistent);
566585
if (!cluster_init_seeds(c, ht_seeds) && !cluster_map_keyspace(c TSRMLS_CC)) {
567586
/* Set up our prefix */

0 commit comments

Comments
 (0)