example-push.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020, Michael Grunder <michael dot grunder at gmail dot com>
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <hiredis.h>
  34. #include <win32.h>
  35. #define KEY_COUNT 5
  36. #define panicAbort(fmt, ...) \
  37. do { \
  38. fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__); \
  39. exit(-1); \
  40. } while (0)
  41. static void assertReplyAndFree(redisContext *context, redisReply *reply, int type) {
  42. if (reply == NULL)
  43. panicAbort("NULL reply from server (error: %s)", context->errstr);
  44. if (reply->type != type) {
  45. if (reply->type == REDIS_REPLY_ERROR)
  46. fprintf(stderr, "Redis Error: %s\n", reply->str);
  47. panicAbort("Expected reply type %d but got type %d", type, reply->type);
  48. }
  49. freeReplyObject(reply);
  50. }
  51. /* Switch to the RESP3 protocol and enable client tracking */
  52. static void enableClientTracking(redisContext *c) {
  53. redisReply *reply = redisCommand(c, "HELLO 3");
  54. if (reply == NULL || c->err) {
  55. panicAbort("NULL reply or server error (error: %s)", c->errstr);
  56. }
  57. if (reply->type != REDIS_REPLY_MAP) {
  58. fprintf(stderr, "Error: Can't send HELLO 3 command. Are you sure you're ");
  59. fprintf(stderr, "connected to redis-server >= 6.0.0?\nRedis error: %s\n",
  60. reply->type == REDIS_REPLY_ERROR ? reply->str : "(unknown)");
  61. exit(-1);
  62. }
  63. freeReplyObject(reply);
  64. /* Enable client tracking */
  65. reply = redisCommand(c, "CLIENT TRACKING ON");
  66. assertReplyAndFree(c, reply, REDIS_REPLY_STATUS);
  67. }
  68. void pushReplyHandler(void *privdata, void *r) {
  69. redisReply *reply = r;
  70. int *invalidations = privdata;
  71. /* Sanity check on the invalidation reply */
  72. if (reply->type != REDIS_REPLY_PUSH || reply->elements != 2 ||
  73. reply->element[1]->type != REDIS_REPLY_ARRAY ||
  74. reply->element[1]->element[0]->type != REDIS_REPLY_STRING)
  75. {
  76. panicAbort("%s", "Can't parse PUSH message!");
  77. }
  78. /* Increment our invalidation count */
  79. *invalidations += 1;
  80. printf("pushReplyHandler(): INVALIDATE '%s' (invalidation count: %d)\n",
  81. reply->element[1]->element[0]->str, *invalidations);
  82. freeReplyObject(reply);
  83. }
  84. /* We aren't actually freeing anything here, but it is included to show that we can
  85. * have hiredis call our data destructor when freeing the context */
  86. void privdata_dtor(void *privdata) {
  87. unsigned int *icount = privdata;
  88. printf("privdata_dtor(): In context privdata dtor (invalidations: %u)\n", *icount);
  89. }
  90. int main(int argc, char **argv) {
  91. unsigned int j, invalidations = 0;
  92. redisContext *c;
  93. redisReply *reply;
  94. const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
  95. int port = (argc > 2) ? atoi(argv[2]) : 6379;
  96. redisOptions o = {0};
  97. REDIS_OPTIONS_SET_TCP(&o, hostname, port);
  98. /* Set our context privdata to the address of our invalidation counter. Each
  99. * time our PUSH handler is called, hiredis will pass the privdata for context.
  100. *
  101. * This could also be done after we create the context like so:
  102. *
  103. * c->privdata = &invalidations;
  104. * c->free_privdata = privdata_dtor;
  105. */
  106. REDIS_OPTIONS_SET_PRIVDATA(&o, &invalidations, privdata_dtor);
  107. /* Set our custom PUSH message handler */
  108. o.push_cb = pushReplyHandler;
  109. c = redisConnectWithOptions(&o);
  110. if (c == NULL || c->err)
  111. panicAbort("Connection error: %s", c ? c->errstr : "OOM");
  112. /* Enable RESP3 and turn on client tracking */
  113. enableClientTracking(c);
  114. /* Set some keys and then read them back. Once we do that, Redis will deliver
  115. * invalidation push messages whenever the key is modified */
  116. for (j = 0; j < KEY_COUNT; j++) {
  117. reply = redisCommand(c, "SET key:%d initial:%d", j, j);
  118. assertReplyAndFree(c, reply, REDIS_REPLY_STATUS);
  119. reply = redisCommand(c, "GET key:%d", j);
  120. assertReplyAndFree(c, reply, REDIS_REPLY_STRING);
  121. }
  122. /* Trigger invalidation messages by updating keys we just read */
  123. for (j = 0; j < KEY_COUNT; j++) {
  124. printf(" main(): SET key:%d update:%d\n", j, j);
  125. reply = redisCommand(c, "SET key:%d update:%d", j, j);
  126. assertReplyAndFree(c, reply, REDIS_REPLY_STATUS);
  127. printf(" main(): SET REPLY OK\n");
  128. }
  129. printf("\nTotal detected invalidations: %d, expected: %d\n", invalidations, KEY_COUNT);
  130. /* PING server */
  131. redisFree(c);
  132. }