example-libev.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include "hiredis.h"
  6. #include "async.h"
  7. #include "adapters/libev.h"
  8. void getCallback(redisAsyncContext *c, void *r, void *privdata) {
  9. redisReply *reply = r;
  10. if (reply == NULL) return;
  11. printf("argv[%s]: %s\n", (char*)privdata, reply->str);
  12. /* Disconnect after receiving the reply to GET */
  13. redisAsyncDisconnect(c);
  14. }
  15. void connectCallback(const redisAsyncContext *c) {
  16. ((void)c);
  17. printf("connected...\n");
  18. }
  19. void disconnectCallback(const redisAsyncContext *c, int status) {
  20. if (status != REDIS_OK) {
  21. printf("Error: %s\n", c->errstr);
  22. }
  23. printf("disconnected...\n");
  24. }
  25. int main (int argc, char **argv) {
  26. signal(SIGPIPE, SIG_IGN);
  27. redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
  28. if (c->err) {
  29. /* Let *c leak for now... */
  30. printf("Error: %s\n", c->errstr);
  31. return 1;
  32. }
  33. redisLibevAttach(EV_DEFAULT_ c);
  34. redisAsyncSetConnectCallback(c,connectCallback);
  35. redisAsyncSetDisconnectCallback(c,disconnectCallback);
  36. redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
  37. redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
  38. ev_loop(EV_DEFAULT_ 0);
  39. return 0;
  40. }