Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/phpredis/phpredis.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Favre-Felix <n.favrefelix@gmail.com>2010-12-08 01:10:29 +0300
committerNicolas Favre-Felix <n.favrefelix@gmail.com>2010-12-08 01:10:29 +0300
commit44f048c523ea798198b71e9c3f845aed66b78dfe (patch)
tree7f55bb656385731549afec21cc80cebfb108a8ac
parent126812026deced3787c201a33795bae608f24575 (diff)
Added support for UNIX Domain Sockets.2.0.12
-rw-r--r--README.markdown6
-rw-r--r--common.h2
-rw-r--r--library.c19
-rwxr-xr-xredis.c8
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*
diff --git a/common.h b/common.h
index aae8e1c0..48827dcc 100644
--- a/common.h
+++ b/common.h
@@ -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;
diff --git a/library.c b/library.c
index b1591ff0..408dcf80 100644
--- a/library.c
+++ b/library.c
@@ -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);
diff --git a/redis.c b/redis.c
index c1cc51c3..38e20750 100755
--- a/redis.c
+++ b/redis.c
@@ -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) {