Skip to content

Instantly share code, notes, and snippets.

@dspezia
Created May 26, 2011 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dspezia/993738 to your computer and use it in GitHub Desktop.
Save dspezia/993738 to your computer and use it in GitHub Desktop.
Example of pipelining with async hiredis
/* Code borrowed from hiredis examples */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "hiredis.h"
#include "async.h"
#include "adapters/ae.h"
/* Put event loop in the global scope, so it can be explicitly stopped */
static aeEventLoop *loop;
void getCallback2(redisAsyncContext *c, void *r, void *privdata) {
redisReply *reply = r;
if (reply == NULL) return;
printf("argv[%s]: %s\n", (char*)privdata, reply->str);
/* Disconnect after receiving the reply to GET */
redisAsyncDisconnect(c);
}
void getCallback1(redisAsyncContext *c, void *r, void *privdata) {
redisReply *reply = r;
if (reply == NULL) return;
printf("argv[%s]: %s\n", (char*)privdata, reply->str);
redisAsyncCommand(c, NULL, NULL, "HINCRBY key field %d", 5 );
redisAsyncCommand(c, NULL, NULL, "HINCRBY key field %d", 5 );
redisAsyncCommand(c, NULL, NULL, "HINCRBY key field %d", 5 );
redisAsyncCommand(c, getCallback2, (char *)"end", "HGET key field");
}
void connectCallback(const redisAsyncContext *c) {
((void)c);
printf("connected...\n");
}
void disconnectCallback(const redisAsyncContext *c, int status) {
if (status != REDIS_OK) {
printf("Error: %s\n", c->errstr);
}
printf("disconnected...\n");
aeStop(loop);
}
int main ( int argc, char *argv[] ) {
signal(SIGPIPE, SIG_IGN);
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
if (c->err) {
/* Let *c leak for now... */
printf("Error: %s\n", c->errstr);
return 1;
}
loop = aeCreateEventLoop();
redisAeAttach(loop, c);
redisAsyncSetConnectCallback(c,connectCallback);
redisAsyncSetDisconnectCallback(c,disconnectCallback);
redisAsyncCommand(c, NULL, NULL, "HSET key field %d", 0 );
redisAsyncCommand(c, NULL, NULL, "HINCRBY key field %d", 5 );
redisAsyncCommand(c, NULL, NULL, "HINCRBY key field %d", 5 );
redisAsyncCommand(c, getCallback1, (char *)"end", "HGET key field");
aeMain(loop);
return 0;
}
@zhentanfeng
Copy link

Is this a real async for pipline? I only find the pipline instruction for the blocking mode in the offical document.
If I want to issue multiple commands like: "hget key field", "hget key1 field", "hget key2 field".
how could I get the reply for once reply?
thanks.

@AccuMistry
Copy link

Hi,
I have a bit problem using "adapters/ae.h" in netbeans IDE ,
Its giving me fatal error inside ae.h file..
Should I use redis/src/ae.h or deps/hiredis/adapter/ae.h???

@naveenkak
Copy link

Is the pipelining possible in asynchronous mode also?
I only see redisAppendCommand for synchronous mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment