diff options
Diffstat (limited to 'redis_array_impl.c')
-rw-r--r-- | redis_array_impl.c | 107 |
1 files changed, 69 insertions, 38 deletions
diff --git a/redis_array_impl.c b/redis_array_impl.c index bafcbd49..c3a1f365 100644 --- a/redis_array_impl.c +++ b/redis_array_impl.c @@ -30,7 +30,7 @@ extern int le_redis_sock; extern zend_class_entry *redis_ce; RedisArray* -ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval TSRMLS_DC) +ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC) { int i, host_len, id; int count = zend_hash_num_elements(hosts); @@ -59,7 +59,9 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval TSRMLS_DC) if((p = strchr(host, ':'))) { /* found port */ host_len = p - host; port = (short)atoi(p+1); - } + } else if(strchr(host,'/') != NULL) { /* unix socket */ + port = -1; + } /* create Redis object */ MAKE_STD_ZVAL(ra->redis[i]); @@ -68,10 +70,13 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval TSRMLS_DC) call_user_function(&redis_ce->function_table, &ra->redis[i], &z_cons, &z_ret, 0, NULL TSRMLS_CC); /* create socket */ - redis_sock = redis_sock_create(host, host_len, port, 0, 0, NULL, retry_interval); /* TODO: persistence? */ + redis_sock = redis_sock_create(host, host_len, port, 0, ra->pconnect, NULL, retry_interval, b_lazy_connect); - /* connect */ - redis_sock_server_open(redis_sock, 1 TSRMLS_CC); + if (!b_lazy_connect) + { + /* connect */ + redis_sock_server_open(redis_sock, 1 TSRMLS_CC); + } /* attach */ #if PHP_VERSION_ID >= 50400 @@ -160,10 +165,13 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) { zval *z_params_index; zval *z_params_autorehash; zval *z_params_retry_interval; + zval *z_params_pconnect; + zval *z_params_lazy_connect; RedisArray *ra = NULL; - zend_bool b_index = 0, b_autorehash = 0; + zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0; long l_retry_interval = 0; + zend_bool b_lazy_connect = 0; HashTable *hHosts = NULL, *hPrev = NULL; /* find entry */ @@ -241,8 +249,27 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) { } } + /* find pconnect option */ + MAKE_STD_ZVAL(z_params_pconnect); + array_init(z_params_pconnect); + sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.pconnect")), z_params_pconnect TSRMLS_CC); + if (zend_hash_find(Z_ARRVAL_P(z_params_pconnect), name, strlen(name) + 1, (void**) &z_data_pp) != FAILURE) { + if(Z_TYPE_PP(z_data_pp) == IS_STRING && strncmp(Z_STRVAL_PP(z_data_pp), "1", 1) == 0) { + b_pconnect = 1; + } + } + /* find retry interval option */ + MAKE_STD_ZVAL(z_params_lazy_connect); + array_init(z_params_lazy_connect); + sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.lazyconnect")), z_params_lazy_connect TSRMLS_CC); + if (zend_hash_find(Z_ARRVAL_P(z_params_lazy_connect), name, strlen(name) + 1, (void **) &z_data_pp) != FAILURE) { + if(Z_TYPE_PP(z_data_pp) == IS_STRING && strncmp(Z_STRVAL_PP(z_data_pp), "1", 1) == 0) { + b_lazy_connect = 1; + } + } + /* create RedisArray object */ - ra = ra_make_array(hHosts, z_fun, z_dist, hPrev, b_index, l_retry_interval TSRMLS_CC); + ra = ra_make_array(hHosts, z_fun, z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect TSRMLS_CC); ra->auto_rehash = b_autorehash; /* cleanup */ @@ -258,12 +285,16 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) { efree(z_params_autorehash); zval_dtor(z_params_retry_interval); efree(z_params_retry_interval); + zval_dtor(z_params_pconnect); + efree(z_params_pconnect); + zval_dtor(z_params_lazy_connect); + efree(z_params_lazy_connect); return ra; } RedisArray * -ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev, zend_bool b_index, long retry_interval TSRMLS_DC) { +ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC) { int count = zend_hash_num_elements(hosts); @@ -281,10 +312,10 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev /* init array data structures */ ra_init_function_table(ra); - if(NULL == ra_load_hosts(ra, hosts, retry_interval TSRMLS_CC)) { + if(NULL == ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC)) { return NULL; } - ra->prev = hosts_prev ? ra_make_array(hosts_prev, z_fun, z_dist, NULL, b_index, retry_interval TSRMLS_CC) : NULL; + ra->prev = hosts_prev ? ra_make_array(hosts_prev, z_fun, z_dist, NULL, b_index, b_pconnect, retry_interval, b_lazy_connect TSRMLS_CC) : NULL; /* copy function if provided */ if(z_fun) { @@ -398,34 +429,34 @@ ra_call_distributor(RedisArray *ra, const char *key, int key_len, int *pos TSRML zval * ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_DC) { - uint32_t hash; - char *out; - int pos, out_len; - - /* extract relevant part of the key */ - out = ra_extract_key(ra, key, key_len, &out_len TSRMLS_CC); - if(!out) - return NULL; - - if(ra->z_dist) { - if (!ra_call_distributor(ra, key, key_len, &pos TSRMLS_CC)) { - return NULL; - } - } - else { - /* hash */ - hash = rcrc32(out, out_len); - efree(out); - - /* get position on ring */ - uint64_t h64 = hash; - h64 *= ra->count; - h64 /= 0xffffffff; - pos = (int)h64; - } - if(out_pos) *out_pos = pos; - - return ra->redis[pos]; + uint32_t hash; + char *out; + int pos, out_len; + + /* extract relevant part of the key */ + out = ra_extract_key(ra, key, key_len, &out_len TSRMLS_CC); + if(!out) + return NULL; + + if(ra->z_dist) { + if (!ra_call_distributor(ra, key, key_len, &pos TSRMLS_CC)) { + return NULL; + } + } + else { + /* hash */ + hash = rcrc32(out, out_len); + efree(out); + + /* get position on ring */ + uint64_t h64 = hash; + h64 *= ra->count; + h64 /= 0xffffffff; + pos = (int)h64; + } + if(out_pos) *out_pos = pos; + + return ra->redis[pos]; } zval * |