Simulation Core
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

example_redis.c 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "../include/hiredis.h"
  5. #ifdef _MSC_VER
  6. #include <winsock2.h> /* For struct timeval */
  7. #endif
  8. static void example_argv_command(redisContext *c, size_t n) {
  9. char **argv, tmp[42];
  10. size_t *argvlen;
  11. redisReply *reply;
  12. /* We're allocating two additional elements for command and key */
  13. argv = malloc(sizeof(*argv) * (2 + n));
  14. argvlen = malloc(sizeof(*argvlen) * (2 + n));
  15. /* First the command */
  16. argv[0] = (char*)"RPUSH";
  17. argvlen[0] = sizeof("RPUSH") - 1;
  18. /* Now our key */
  19. argv[1] = (char*)"argvlist";
  20. argvlen[1] = sizeof("argvlist") - 1;
  21. /* Now add the entries we wish to add to the list */
  22. for (size_t i = 2; i < (n + 2); i++) {
  23. argvlen[i] = snprintf(tmp, sizeof(tmp), "argv-element-%zu", i - 2);
  24. argv[i] = strdup(tmp);
  25. }
  26. /* Execute the command using redisCommandArgv. We're sending the arguments with
  27. * two explicit arrays. One for each argument's string, and the other for its
  28. * length. */
  29. reply = redisCommandArgv(c, n + 2, (const char **)argv, (const size_t*)argvlen);
  30. if (reply == NULL || c->err) {
  31. fprintf(stderr, "Error: Couldn't execute redisCommandArgv\n");
  32. exit(1);
  33. }
  34. if (reply->type == REDIS_REPLY_INTEGER) {
  35. printf("%s reply: %lld\n", argv[0], reply->integer);
  36. }
  37. freeReplyObject(reply);
  38. /* Clean up */
  39. for (size_t i = 2; i < (n + 2); i++) {
  40. free(argv[i]);
  41. }
  42. free(argv);
  43. free(argvlen);
  44. }
  45. int main(int argc, char **argv) {
  46. unsigned int j, isunix = 0;
  47. redisContext *c;
  48. redisReply *reply;
  49. const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
  50. if (argc > 2) {
  51. if (*argv[2] == 'u' || *argv[2] == 'U') {
  52. isunix = 1;
  53. /* in this case, host is the path to the unix socket */
  54. printf("Will connect to unix socket @%s\n", hostname);
  55. }
  56. }
  57. int port = (argc > 2) ? atoi(argv[2]) : 6379;
  58. struct timeval timeout = { 1, 500000 }; // 1.5 seconds
  59. if (isunix) {
  60. c = redisConnectUnixWithTimeout(hostname, timeout);
  61. } else {
  62. c = redisConnectWithTimeout(hostname, port, timeout);
  63. }
  64. if (c == NULL || c->err) {
  65. if (c) {
  66. printf("Connection error: %s\n", c->errstr);
  67. redisFree(c);
  68. } else {
  69. printf("Connection error: can't allocate redis context\n");
  70. }
  71. exit(1);
  72. }
  73. /* PING server */
  74. reply = redisCommand(c,"PING");
  75. printf("PING: %s\n", reply->str);
  76. freeReplyObject(reply);
  77. /* Set a key */
  78. reply = redisCommand(c,"SET %s %s", "foo", "hello world");
  79. printf("SET: %s\n", reply->str);
  80. freeReplyObject(reply);
  81. /* Set a key using binary safe API */
  82. reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
  83. printf("SET (binary API): %s\n", reply->str);
  84. freeReplyObject(reply);
  85. /* Try a GET and two INCR */
  86. reply = redisCommand(c,"GET foo");
  87. printf("GET foo: %s\n", reply->str);
  88. freeReplyObject(reply);
  89. reply = redisCommand(c,"INCR counter");
  90. printf("INCR counter: %lld\n", reply->integer);
  91. freeReplyObject(reply);
  92. /* again ... */
  93. reply = redisCommand(c,"INCR counter");
  94. printf("INCR counter: %lld\n", reply->integer);
  95. freeReplyObject(reply);
  96. /* Create a list of numbers, from 0 to 9 */
  97. reply = redisCommand(c,"DEL mylist");
  98. freeReplyObject(reply);
  99. for (j = 0; j < 10; j++) {
  100. char buf[64];
  101. snprintf(buf,64,"%u",j);
  102. reply = redisCommand(c,"LPUSH mylist element-%s", buf);
  103. freeReplyObject(reply);
  104. }
  105. /* Let's check what we have inside the list */
  106. reply = redisCommand(c,"LRANGE mylist 0 -1");
  107. if (reply->type == REDIS_REPLY_ARRAY) {
  108. for (j = 0; j < reply->elements; j++) {
  109. printf("%u) %s\n", j, reply->element[j]->str);
  110. }
  111. }
  112. freeReplyObject(reply);
  113. /* See function for an example of redisCommandArgv */
  114. example_argv_command(c, 10);
  115. /* Disconnects and frees the context */
  116. redisFree(c);
  117. return 0;
  118. }