2
0

example-libev.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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, int status) {
  16. if (status != REDIS_OK) {
  17. printf("Error: %s\n", c->errstr);
  18. return;
  19. }
  20. printf("Connected...\n");
  21. }
  22. void disconnectCallback(const redisAsyncContext *c, int status) {
  23. if (status != REDIS_OK) {
  24. printf("Error: %s\n", c->errstr);
  25. return;
  26. }
  27. printf("Disconnected...\n");
  28. }
  29. int main (int argc, char **argv) {
  30. signal(SIGPIPE, SIG_IGN);
  31. redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
  32. if (c->err) {
  33. /* Let *c leak for now... */
  34. printf("Error: %s\n", c->errstr);
  35. return 1;
  36. }
  37. redisLibevAttach(EV_DEFAULT_ c);
  38. redisAsyncSetConnectCallback(c,connectCallback);
  39. redisAsyncSetDisconnectCallback(c,disconnectCallback);
  40. redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
  41. redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
  42. ev_loop(EV_DEFAULT_ 0);
  43. return 0;
  44. }