Beispiel #1
0
PHP_REDIS_API redis_pool_member *
redis_pool_get_sock(redis_pool *pool, const char *key) {
    unsigned int pos, i;
	redis_pool_member *rpm = pool->head;
    memcpy(&pos, key, sizeof(pos));
    pos %= pool->totalWeight;
    for(i = 0; i < pool->totalWeight;) {
        if(pos >= i && pos < i + rpm->weight) {
            int needs_auth = 0;
            if(rpm->auth && rpm->auth_len && rpm->redis_sock->status != REDIS_SOCK_STATUS_CONNECTED) {
                needs_auth = 1;
            }
            redis_sock_server_open(rpm->redis_sock, 0);
            if(needs_auth) {
                redis_pool_member_auth(rpm);
            }
            if(rpm->database >= 0) { /* default is -1 which leaves the choice to redis. */
                redis_pool_member_select(rpm);
            }

            return rpm;
        }
        i += rpm->weight;
        rpm = rpm->next;
    }

    return NULL;
}
Beispiel #2
0
RedisArray*
ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b_lazy_connect)
{
    int i = 0, host_len;
    zval *id;
    char *host, *p;
    short port;
    zval *zpData, z_cons, z_ret;
    RedisSock *redis_sock  = NULL;

    /* function calls on the Redis object */
    ZVAL_STRING(&z_cons, "__construct");

    /* init connections */
    ZEND_HASH_FOREACH_VAL(hosts, zpData) {
        if (Z_TYPE_P(zpData) != IS_STRING) {
            efree(ra);
            return NULL;
        }

        ra->hosts[i] = estrdup(Z_STRVAL_P(zpData));

        /* default values */
        host = Z_STRVAL_P(zpData);
        host_len = Z_STRLEN_P(zpData);
        port = 6379;

        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 */
        object_init_ex(&ra->redis[i], redis_ce);
        call_user_function(&redis_ce->function_table, &ra->redis[i], &z_cons, &z_ret, 0, NULL);

        /* create socket */
        redis_sock = redis_sock_create(host, host_len, port, ra->connect_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect);

        if (!b_lazy_connect)
        {
            /* connect */
            redis_sock_server_open(redis_sock, 1);
        }

        /* attach */
        id = zend_list_insert(redis_sock, le_redis_sock);
        add_property_resource(&ra->redis[i], "socket", Z_RES_P(id));

        i++;
    } ZEND_HASH_FOREACH_END();

    return ra;
}
Beispiel #3
0
RedisArray*
ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval TSRMLS_DC)
{
	int i, host_len, id;
	int count = zend_hash_num_elements(hosts);
	char *host, *p;
	short port;
	zval **zpData, z_cons, z_ret;
	RedisSock *redis_sock  = NULL;

	/* function calls on the Redis object */
	ZVAL_STRING(&z_cons, "__construct", 0);

	/* init connections */
	for(i = 0; i < count; ++i) {
		if(FAILURE == zend_hash_quick_find(hosts, NULL, 0, i, (void**)&zpData)) {
			efree(ra);
			return NULL;
		}

		ra->hosts[i] = estrdup(Z_STRVAL_PP(zpData));

		/* default values */
		host = Z_STRVAL_PP(zpData);
		host_len = Z_STRLEN_PP(zpData);
		port = 6379;

		if((p = strchr(host, ':'))) { /* found port */
			host_len = p - host;
			port = (short)atoi(p+1);
		}

		/* create Redis object */
		MAKE_STD_ZVAL(ra->redis[i]);
		object_init_ex(ra->redis[i], redis_ce);
		INIT_PZVAL(ra->redis[i]);
		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? */

		/* connect */
		redis_sock_server_open(redis_sock, 1 TSRMLS_CC);

		/* attach */
#if PHP_VERSION_ID >= 50400
		id = zend_list_insert(redis_sock, le_redis_sock TSRMLS_CC);
#else
		id = zend_list_insert(redis_sock, le_redis_sock);
#endif
		add_property_resource(ra->redis[i], "socket", id);
	}

	return ra;
}
RedisArray*
ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC)
{
    int i = 0, host_len;
    zval *id;
    char *host, *p;
    short port;
    zval *zpData, z_cons, z_ret;
    RedisSock *redis_sock  = NULL;

    /* function calls on the Redis object */
    ZVAL_STRING(&z_cons, "__construct");

    /* init connections */
    for (zend_hash_internal_pointer_reset(hosts);
         zend_hash_has_more_elements(hosts) == SUCCESS;
         zend_hash_move_forward(hosts))
    {
        if ((zpData = zend_hash_get_current_data(hosts)) == NULL || (Z_TYPE_P(zpData) != IS_STRING))
        {
            zval_dtor(&z_cons);
            efree(ra);
            return NULL;
        }

        ra->hosts[i] = estrdup(Z_STRVAL_P(zpData));

        /* default values */
        host = Z_STRVAL_P(zpData);
        host_len = Z_STRLEN_P(zpData);
        port = 6379;

		if(((p = strrchr(host, ':')))) { /* found port */
			host_len = p - host;
			port = (short)atoi(p+1);
		} else if(strchr(host,'/') != NULL) { /* unix socket */
		    port = -1;
		}

        /* create Redis object */
        object_init_ex(&ra->redis[i], redis_ce);
        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, ra->connect_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect);

        if (!b_lazy_connect) {
            /* connect */
            redis_sock_server_open(redis_sock, 1 TSRMLS_CC);
        }

        /* attach */
        id = zend_list_insert(redis_sock, le_redis_sock TSRMLS_CC);
        add_property_resource(&ra->redis[i], "socket", Z_RES_P(id));
        i++;
    }

    /* Cleanup constructor zval */
    zval_dtor(&z_cons);

    return ra;
}