Simulation Core
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

example2.c 2.2 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "hiredis.h"
  2. void dumpReply(redisReply *r, int indent) {
  3. sds buffer = sdsempty();
  4. switch (r->type) {
  5. case REDIS_REPLY_STRING:
  6. buffer = sdscatfmt(buffer, "(string) %s\n", r->str);
  7. break;
  8. case REDIS_REPLY_STATUS:
  9. buffer = sdscatfmt(buffer, "(status) %s\n", r->str);
  10. break;
  11. case REDIS_REPLY_INTEGER:
  12. buffer = sdscatlen(buffer, "(integer) %lld\n", r->integer);
  13. break;
  14. case REDIS_REPLY_NIL:
  15. buffer = sdscatlen(buffer, "(nill)\n", 7);
  16. break;
  17. case REDIS_REPLY_ERROR:
  18. buffer = sdscatfmt(buffer, " (error) %s", r->str);
  19. break;
  20. case REDIS_REPLY_ARRAY:
  21. for (size_t i = 0; i < r->elements; i++) {
  22. dumpReply(r->element[i], indent + 2);
  23. }
  24. break;
  25. default:
  26. return;
  27. // dbg_abort("Don't know how to deal with reply type %d", r->type);
  28. }
  29. if (sdslen(buffer) > 0) {
  30. for (int i = 0; i < indent; i++)
  31. printf(" ");
  32. printf("%s", buffer);
  33. }
  34. sdsfree(buffer);
  35. }
  36. int main() {
  37. redisContext *c;
  38. redisReply *reply;
  39. // Connect to Redis server
  40. c = redisConnect("127.0.0.1", 6379);
  41. if (c->err) {
  42. printf("Error 0: %s\n", c->errstr);
  43. return 1;
  44. }
  45. // Authenticate if necessary
  46. reply = redisCommand(c, "AUTH %s", "1qazxsw2$$");
  47. freeReplyObject(reply);
  48. // Read from the stream
  49. char commandStr[100] = "XREADGROUP GROUP sampleGroup NOACK STREAMS sampleStream >";
  50. printf("command: %s\n",commandStr);
  51. reply = redisCommand(c, commandStr);
  52. printf("reply->type =>%d\n",reply->type);
  53. dumpReply(reply,0);
  54. // if (reply->type == REDIS_REPLY_ARRAY) {
  55. // printf("reply->elements: %ld\n",reply->elements);
  56. // for(int i = 0; i < reply->elements; i++) {
  57. // redisReply *element = reply->element[i];
  58. // printf("read from redis: %s\n", reply->str);
  59. // }
  60. // }
  61. // Don't forget to free the reply object when you're done with it
  62. freeReplyObject(reply);
  63. // Disconnect from the Redis server
  64. redisFree(c);
  65. return 0;
  66. }