Permalink
Browse files

Added support for UNIX Domain Sockets.

  • Loading branch information...
1 parent 1268120 commit 44f048c523ea798198b71e9c3f845aed66b78dfe @nicolasff nicolasff committed Dec 7, 2010
Showing with 22 additions and 13 deletions.
  1. +4 −2 README.markdown
  2. +1 −1 common.h
  3. +11 −8 library.c
  4. +6 −2 redis.c
View
@@ -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*
View
@@ -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;
View
@@ -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);
View
@@ -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) {

0 comments on commit 44f048c

Please sign in to comment.