diff options
author | Nicolas Favre-Felix <n.favrefelix@gmail.com> | 2010-12-08 01:10:29 +0300 |
---|---|---|
committer | Nicolas Favre-Felix <n.favrefelix@gmail.com> | 2010-12-08 01:10:29 +0300 |
commit | 44f048c523ea798198b71e9c3f845aed66b78dfe (patch) | |
tree | 7f55bb656385731549afec21cc80cebfb108a8ac | |
parent | 126812026deced3787c201a33795bae608f24575 (diff) |
Added support for UNIX Domain Sockets.2.0.12
-rw-r--r-- | README.markdown | 6 | ||||
-rw-r--r-- | common.h | 2 | ||||
-rw-r--r-- | library.c | 19 | ||||
-rwxr-xr-x | redis.c | 8 |
4 files changed, 22 insertions, 13 deletions
diff --git a/README.markdown b/README.markdown index d79d1dab..e717cc70 100644 --- a/README.markdown +++ b/README.markdown @@ -72,8 +72,8 @@ Connects to a Redis instance. ##### *Parameters* -*host*: string -*port*: int +*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) ##### *Return Value* @@ -83,7 +83,9 @@ Connects to a Redis instance. ##### *Example* $redis->connect('127.0.0.1', 6379); +$redis->connect('127.0.0.1'); // port 6379 by default $redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout. +$redis->connect('/tmp/redis.sock'); // unix domain socket. ## ping ##### *Description* @@ -138,7 +138,7 @@ typedef struct request_item { typedef struct { php_stream *stream; char *host; - unsigned short port; + short port; double timeout; int failed; int status; @@ -722,17 +722,20 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC) tv.tv_sec = (time_t)redis_sock->timeout; tv.tv_usec = (int)((redis_sock->timeout - tv.tv_sec) * 1000000); - - host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port); - if(tv.tv_sec != 0 || tv.tv_usec != 0) { - tv_ptr = &tv; + tv_ptr = &tv; + } + + if(redis_sock->host[0] == '/' && redis_sock->port < 1) { + host_len = spprintf(&host, 0, "unix://%s", redis_sock->host); + } else { + host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port); } redis_sock->stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE, - STREAM_XPORT_CLIENT - | STREAM_XPORT_CONNECT, - hash_key, tv_ptr, NULL, &errstr, &err - ); + STREAM_XPORT_CLIENT + | STREAM_XPORT_CONNECT, + hash_key, tv_ptr, NULL, &errstr, &err + ); efree(host); @@ -357,12 +357,12 @@ PHP_METHOD(Redis, connect) zval *object; int host_len, id; char *host = NULL; - long port; + long port = -1; double timeout = 0.0; RedisSock *redis_sock = NULL; - if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl|d", + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|ld", &object, redis_ce, &host, &host_len, &port, &timeout) == FAILURE) { RETURN_FALSE; @@ -373,6 +373,10 @@ PHP_METHOD(Redis, connect) RETURN_FALSE; } + if(port == -1 && host_len && host[0] != '/') { /* not unix socket, set to default value */ + port = 6379; + } + redis_sock = redis_sock_create(host, host_len, port, timeout); if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) { |