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

Skip to content

Commit 265feef

Browse files
committed
Merge branch 'master' of https://github.com/Zakay/phpredis into Zakay-master
2 parents 43bc590 + 9e4eeee commit 265feef

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

README.markdown

+5-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ So be patient on to many open FD's (specially on redis server side) when using p
109109
connections on many servers connecting to one redis server.
110110

111111
Also more than one persistent connection can be made identified by either host + port + timeout
112-
or unix socket + timeout.
112+
or host + persistent_id or unix socket + timeout.
113113

114114
This feature is not available in threaded versions. `pconnect` and `popen` then working like their non
115115
persistent equivalents.
@@ -119,6 +119,7 @@ persistent equivalents.
119119
*host*: string. can be a host, or the path to a unix domain socket
120120
*port*: int, optional
121121
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
122+
*persistent_id*: string. identity for the requested persistent connection
122123

123124
##### *Return Value*
124125

@@ -129,8 +130,9 @@ persistent equivalents.
129130
<pre>
130131
$redis->pconnect('127.0.0.1', 6379);
131132
$redis->pconnect('127.0.0.1'); // port 6379 by default - same connection like before.
132-
$redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection then the two before.
133-
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection then the three before.
133+
$redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection than the two before.
134+
$redis->pconnect('127.0.0.1', 6379, 2.5, 'x'); // x is sent as persistent_id and would be another connection the the three before.
135+
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection than the four before.
134136
</pre>
135137

136138
## close

common.h

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ typedef struct {
147147
int failed;
148148
int status;
149149
int persistent;
150+
char *persistent_id;
150151

151152
int serializer;
152153

library.c

+17-2
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ PHPAPI void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
650650
* redis_sock_create
651651
*/
652652
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port,
653-
double timeout, int persistent)
653+
double timeout, int persistent, char *persistent_id)
654654
{
655655
RedisSock *redis_sock;
656656

@@ -661,6 +661,17 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
661661

662662
redis_sock->persistent = persistent;
663663

664+
if(persistent_id) {
665+
size_t persistent_id_len = strlen(persistent_id);
666+
redis_sock->persistent_id = ecalloc(persistent_id_len + 1, 1);
667+
memcpy(redis_sock->persistent_id, persistent_id, persistent_id_len);
668+
} else {
669+
redis_sock->persistent_id = NULL;
670+
}
671+
672+
memcpy(redis_sock->host, host, host_len);
673+
redis_sock->host[host_len] = '\0';
674+
664675
redis_sock->port = port;
665676
redis_sock->timeout = timeout;
666677

@@ -700,7 +711,11 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
700711
}
701712

702713
if (redis_sock->persistent) {
703-
spprintf(&persistent_id, 0, "%s:%f", host, redis_sock->timeout);
714+
if (redis_sock->persistent_id) {
715+
spprintf(&persistent_id, 0, "phpredis:%s:%s", host, redis_sock->persistent_id);
716+
} else {
717+
spprintf(&persistent_id, 0, "phpredis:%s:%f", host, redis_sock->timeout);
718+
}
704719
}
705720

706721
redis_sock->stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE,

library.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis
1313
PHPAPI void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
1414
PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
1515
PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
16-
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent);
16+
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent, char *persistent_id);
1717
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
1818
PHPAPI int redis_sock_server_open(RedisSock *redis_sock, int force_connect TSRMLS_DC);
1919
PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);

redis.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
414414
char *host = NULL;
415415
long port = -1;
416416

417+
char *persistent_id = NULL;
418+
int persistent_id_len = -1;
419+
417420
#ifdef ZTS
418421
/* not sure how in threaded mode this works so disabled persistents at first */
419422
persistent = 0;
@@ -422,9 +425,9 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
422425
double timeout = 0.0;
423426
RedisSock *redis_sock = NULL;
424427

425-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|ld",
428+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|lds",
426429
&object, redis_ce, &host, &host_len, &port,
427-
&timeout) == FAILURE) {
430+
&timeout, &persistent_id, &persistent_id_len) == FAILURE) {
428431
return FAILURE;
429432
}
430433

@@ -447,7 +450,7 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
447450
}
448451
}
449452

450-
redis_sock = redis_sock_create(host, host_len, port, timeout, persistent);
453+
redis_sock = redis_sock_create(host, host_len, port, timeout, persistent, persistent_id, persistent_id_len);
451454

452455
if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {
453456
redis_free_socket(redis_sock);

redis_session.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ PS_OPEN_FUNC(redis)
180180
int weight = 1;
181181
double timeout = 86400.0;
182182
int persistent = 0;
183-
char *prefix = NULL, *auth = NULL;
183+
char *prefix = NULL, *auth = NULL, *persistent_id = NULL;
184184

185185
/* translate unix: into file: */
186186
if (!strncmp(save_path+i, "unix:", sizeof("unix:")-1)) {
@@ -222,6 +222,9 @@ PS_OPEN_FUNC(redis)
222222
if (zend_hash_find(Z_ARRVAL_P(params), "persistent", sizeof("persistent"), (void **) &param) != FAILURE) {
223223
persistent = (atol(Z_STRVAL_PP(param)) == 1 ? 1 : 0);
224224
}
225+
if (zend_hash_find(Z_ARRVAL_P(params), "persistent_id", sizeof("persistent_id"), (void **) &param) != FAILURE) {
226+
persistent_id = estrndup(Z_STRVAL_PP(param), Z_STRLEN_PP(param));
227+
}
225228
if (zend_hash_find(Z_ARRVAL_P(params), "prefix", sizeof("prefix"), (void **) &param) != FAILURE) {
226229
prefix = estrndup(Z_STRVAL_PP(param), Z_STRLEN_PP(param));
227230
}
@@ -248,9 +251,9 @@ PS_OPEN_FUNC(redis)
248251

249252
RedisSock *redis_sock;
250253
if(url->path) { /* unix */
251-
redis_sock = redis_sock_create(url->path, strlen(url->path), 0, timeout, persistent);
254+
redis_sock = redis_sock_create(url->path, strlen(url->path), 0, timeout, persistent, persistent_id);
252255
} else {
253-
redis_sock = redis_sock_create(url->host, strlen(url->host), url->port, timeout, persistent);
256+
redis_sock = redis_sock_create(url->host, strlen(url->host), url->port, timeout, persistent, persistent_id);
254257
}
255258
redis_pool_add(pool, redis_sock, weight, prefix, auth TSRMLS_CC);
256259

0 commit comments

Comments
 (0)