Exemple #1
0
void *Redis_create(char *host, int port) {
	redisContext *con;
	con = redisConnect(host, port);

	if (con == NULL || con->err) {
		if (con) {
			printf("Connection error: %s\n", con->errstr);
			redisFree(con);
		} else {
			printf("Connection error: can't allocate redis context\n");
		}
		exit(1);
	}

	Redis proto = { .host = host, .port = port, .con = con, .init = Redis_init,
			.readReply = Redis_readReply, .close = Redis_close, .command =
					Redis_command };

	Redis *m = calloc(1, sizeof(Redis));
	*m = proto;

	if (!m->init(m)) {
		// looks like it didn't initialize properly
		m->close(m);
		return NULL;
	} else {
		// all done, we made an object of any type
		return m;
	}

}