Redis C + + Programming example

Source: Internet
Author: User
Tags install redis

Basic function steps:

1, download and install redis,:http://www.redis.cn/download.html.

2, download and install Hiredis,:https://github.com/redis/hiredis.

Place the libhiredis.so in the/usr/lib/directory.

3, write the client program under the Hiredis-master directory.

[CPP]View PlainCopy
  1. #include <stdio.h>
  2. #include "Hiredis.h"
  3. int main ()
  4. {
  5. Rediscontext *conn = Redisconnect ("127.0.0.1", 6379);
  6. if (conn! = NULL && conn->err)
  7. {
  8. printf ("Connection error:%s\n", CONN->ERRSTR);
  9. return 0;
  10. }
  11. Redisreply *reply = (redisreply*) rediscommand (conn,"set foo 1234");
  12. Freereplyobject (reply);
  13. Reply = Rediscommand (conn,"get foo");
  14. printf ("%s\n", reply->str);
  15. Freereplyobject (reply);
  16. Redisfree (conn);
  17. return 0;
  18. }

Compile command: Gcc-o redistest Redistest.c-lhiredis

4. Start the server and run the client program.

Redis pub/sub feature implementation.

Publish pub Feature:

[CPP]View PlainCopy
  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. #include "Hiredis.h"
  4. int main ()
  5. {
  6. struct timeval begin;
  7. Rediscontext *conn = Redisconnect ("127.0.0.1", 6379);
  8. if (conn! = NULL && conn->err)
  9. {
  10. printf ("Connection error:%s\n", CONN->ERRSTR);
  11. return 0;
  12. }
  13. Gettimeofday (&begin, NULL);
  14. Redisreply *reply = (redisreply*) rediscommand (conn,"publish foo 1234");
  15. int sec, usec;
  16. SEC = begin.tv_sec;
  17. USEC = begin.tv_usec;
  18. printf ("%d\t%d\n", sec, USEC);
  19. Freereplyobject (reply);
  20. Reply = Rediscommand (conn,"get foo");
  21. printf ("%s\n", reply->str);
  22. Freereplyobject (reply);
  23. Redisfree (conn);
  24. return 0;
  25. }

Subscribe to Sub Features:

[CPP]View PlainCopy
  1. #include <sys/time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include "Hiredis.h"
  7. #include "async.h"
  8. #include "Adapters/libevent.h"
  9. void Subcallback (Redisasynccontext *c, void *r, void *priv) {
  10. struct Timeval end;
  11. int sec, usec;
  12. Redisreply *reply = r;
  13. if (reply = = NULL) return;
  14. if (Reply->type = = Redis_reply_array && reply->elements = = 3) {
  15. if (strcmp (REPLY->ELEMENT[0]->STR, "subscribe")! = 0) {
  16. Gettimeofday (&end,null);
  17. SEC = end.tv_sec;
  18. USEC = end.tv_usec;
  19. printf ("%d\t%d\n", sec, USEC);
  20. printf ( "received[%s] Channel%s:%s\n",
  21. (char*) Priv,
  22. REPLY->ELEMENT[1]->STR,
  23. REPLY->ELEMENT[2]->STR);
  24. }
  25. }
  26. }
  27. void Connectcallback (const redisasynccontext *c, int status) {
  28. if (Status! = REDIS_OK) {
  29. printf ("Error:%s\n", C->ERRSTR);
  30. return;
  31. }
  32. printf ("connected...\n");
  33. }
  34. void Disconnectcallback (const redisasynccontext *c, int status) {
  35. if (Status! = REDIS_OK) {
  36. printf ("Error:%s\n", C->ERRSTR);
  37. return;
  38. }
  39. printf ("disconnected...\n");
  40. }
  41. int main (int argc, char **argv) {
  42. Signal (Sigpipe, sig_ign);
  43. struct Event_base *base = event_base_new ();
  44. Redisasynccontext *c = Redisasyncconnect ("127.0.0.1", 6379);
  45. if (c->err) {
  46. / * Let *c leak to now ... * /
  47. printf ("Error:%s\n", C->ERRSTR);
  48. return 1;
  49. }
  50. Redislibeventattach (c,base);
  51. Redisasyncsetconnectcallback (C,connectcallback);
  52. Redisasyncsetdisconnectcallback (C,disconnectcallback);
  53. Redisasynccommand (c, Subcallback, (char*) "Sub", "SUBSCRIBE foo");
  54. Event_base_dispatch (base);
  55. return 0;
  56. }

Pub compile command ibid., Sub Compile command: Gcc-o Sub sub.c-lhiredis-levent

Attention:

1, you may need to build a link when compiling the Basic program: Ln-s libhiredis.so libhiredis.so.0.12

2, 1, subscription sub requires installation of Libevent-dev.

Redis C + + Programming example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.