Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' of https://github.com/Zakay/phpredis into Zakay…
…-master
  • Loading branch information
nicolasff committed Jun 12, 2011
2 parents 43bc590 + 9e4eeee commit 265feef
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
8 changes: 5 additions & 3 deletions README.markdown
Expand Up @@ -109,7 +109,7 @@ So be patient on to many open FD's (specially on redis server side) when using p
connections on many servers connecting to one redis server.

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

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

##### *Return Value*

Expand All @@ -129,8 +130,9 @@ persistent equivalents.
<pre>
$redis->pconnect('127.0.0.1', 6379);
$redis->pconnect('127.0.0.1'); // port 6379 by default - same connection like before.
$redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection then the two before.
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection then the three before.
$redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection than the two before.
$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.
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection than the four before.
</pre>

## close
Expand Down
1 change: 1 addition & 0 deletions common.h
Expand Up @@ -147,6 +147,7 @@ typedef struct {
int failed;
int status;
int persistent;
char *persistent_id;

int serializer;

Expand Down
19 changes: 17 additions & 2 deletions library.c
Expand Up @@ -650,7 +650,7 @@ PHPAPI void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
* redis_sock_create
*/
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port,
double timeout, int persistent)
double timeout, int persistent, char *persistent_id)
{
RedisSock *redis_sock;

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

redis_sock->persistent = persistent;

if(persistent_id) {
size_t persistent_id_len = strlen(persistent_id);
redis_sock->persistent_id = ecalloc(persistent_id_len + 1, 1);
memcpy(redis_sock->persistent_id, persistent_id, persistent_id_len);
} else {
redis_sock->persistent_id = NULL;
}

memcpy(redis_sock->host, host, host_len);
redis_sock->host[host_len] = '\0';

redis_sock->port = port;
redis_sock->timeout = timeout;

Expand Down Expand Up @@ -700,7 +711,11 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
}

if (redis_sock->persistent) {
spprintf(&persistent_id, 0, "%s:%f", host, redis_sock->timeout);
if (redis_sock->persistent_id) {
spprintf(&persistent_id, 0, "phpredis:%s:%s", host, redis_sock->persistent_id);
} else {
spprintf(&persistent_id, 0, "phpredis:%s:%f", host, redis_sock->timeout);
}
}

redis_sock->stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE,
Expand Down
2 changes: 1 addition & 1 deletion library.h
Expand Up @@ -13,7 +13,7 @@ PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis
PHPAPI void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent);
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent, char *persistent_id);
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
PHPAPI int redis_sock_server_open(RedisSock *redis_sock, int force_connect TSRMLS_DC);
PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);
Expand Down
9 changes: 6 additions & 3 deletions redis.c
Expand Up @@ -414,6 +414,9 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
char *host = NULL;
long port = -1;

char *persistent_id = NULL;
int persistent_id_len = -1;

#ifdef ZTS
/* not sure how in threaded mode this works so disabled persistents at first */
persistent = 0;
Expand All @@ -422,9 +425,9 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
double timeout = 0.0;
RedisSock *redis_sock = NULL;

if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|ld",
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|lds",
&object, redis_ce, &host, &host_len, &port,
&timeout) == FAILURE) {
&timeout, &persistent_id, &persistent_id_len) == FAILURE) {
return FAILURE;
}

Expand All @@ -447,7 +450,7 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
}
}

redis_sock = redis_sock_create(host, host_len, port, timeout, persistent);
redis_sock = redis_sock_create(host, host_len, port, timeout, persistent, persistent_id, persistent_id_len);

if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {
redis_free_socket(redis_sock);
Expand Down
9 changes: 6 additions & 3 deletions redis_session.c
Expand Up @@ -180,7 +180,7 @@ PS_OPEN_FUNC(redis)
int weight = 1;
double timeout = 86400.0;
int persistent = 0;
char *prefix = NULL, *auth = NULL;
char *prefix = NULL, *auth = NULL, *persistent_id = NULL;

/* translate unix: into file: */
if (!strncmp(save_path+i, "unix:", sizeof("unix:")-1)) {
Expand Down Expand Up @@ -222,6 +222,9 @@ PS_OPEN_FUNC(redis)
if (zend_hash_find(Z_ARRVAL_P(params), "persistent", sizeof("persistent"), (void **) &param) != FAILURE) {
persistent = (atol(Z_STRVAL_PP(param)) == 1 ? 1 : 0);
}
if (zend_hash_find(Z_ARRVAL_P(params), "persistent_id", sizeof("persistent_id"), (void **) &param) != FAILURE) {
persistent_id = estrndup(Z_STRVAL_PP(param), Z_STRLEN_PP(param));
}
if (zend_hash_find(Z_ARRVAL_P(params), "prefix", sizeof("prefix"), (void **) &param) != FAILURE) {
prefix = estrndup(Z_STRVAL_PP(param), Z_STRLEN_PP(param));
}
Expand All @@ -248,9 +251,9 @@ PS_OPEN_FUNC(redis)

RedisSock *redis_sock;
if(url->path) { /* unix */
redis_sock = redis_sock_create(url->path, strlen(url->path), 0, timeout, persistent);
redis_sock = redis_sock_create(url->path, strlen(url->path), 0, timeout, persistent, persistent_id);
} else {
redis_sock = redis_sock_create(url->host, strlen(url->host), url->port, timeout, persistent);
redis_sock = redis_sock_create(url->host, strlen(url->host), url->port, timeout, persistent, persistent_id);
}
redis_pool_add(pool, redis_sock, weight, prefix, auth TSRMLS_CC);

Expand Down

0 comments on commit 265feef

Please sign in to comment.