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
- #include <stdio.h>
- #include "Hiredis.h"
- int main ()
- {
- Rediscontext *conn = Redisconnect ("127.0.0.1", 6379);
- if (conn! = NULL && conn->err)
- {
- printf ("Connection error:%s\n", CONN->ERRSTR);
- return 0;
- }
- Redisreply *reply = (redisreply*) rediscommand (conn,"set foo 1234");
- Freereplyobject (reply);
- Reply = Rediscommand (conn,"get foo");
- printf ("%s\n", reply->str);
- Freereplyobject (reply);
- Redisfree (conn);
- return 0;
- }
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
- #include <stdio.h>
- #include <sys/time.h>
- #include "Hiredis.h"
- int main ()
- {
- struct timeval begin;
- Rediscontext *conn = Redisconnect ("127.0.0.1", 6379);
- if (conn! = NULL && conn->err)
- {
- printf ("Connection error:%s\n", CONN->ERRSTR);
- return 0;
- }
- Gettimeofday (&begin, NULL);
- Redisreply *reply = (redisreply*) rediscommand (conn,"publish foo 1234");
- int sec, usec;
- SEC = begin.tv_sec;
- USEC = begin.tv_usec;
- printf ("%d\t%d\n", sec, USEC);
- Freereplyobject (reply);
- Reply = Rediscommand (conn,"get foo");
- printf ("%s\n", reply->str);
- Freereplyobject (reply);
- Redisfree (conn);
- return 0;
- }
Subscribe to Sub Features:
[CPP]View PlainCopy
- #include <sys/time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include "Hiredis.h"
- #include "async.h"
- #include "Adapters/libevent.h"
- void Subcallback (Redisasynccontext *c, void *r, void *priv) {
- struct Timeval end;
- int sec, usec;
- Redisreply *reply = r;
- if (reply = = NULL) return;
- if (Reply->type = = Redis_reply_array && reply->elements = = 3) {
- if (strcmp (REPLY->ELEMENT[0]->STR, "subscribe")! = 0) {
- Gettimeofday (&end,null);
- SEC = end.tv_sec;
- USEC = end.tv_usec;
- printf ("%d\t%d\n", sec, USEC);
- printf ( "received[%s] Channel%s:%s\n",
- (char*) Priv,
- REPLY->ELEMENT[1]->STR,
- REPLY->ELEMENT[2]->STR);
- }
- }
- }
- void Connectcallback (const redisasynccontext *c, int status) {
- if (Status! = REDIS_OK) {
- printf ("Error:%s\n", C->ERRSTR);
- return;
- }
- printf ("connected...\n");
- }
- void Disconnectcallback (const redisasynccontext *c, int status) {
- if (Status! = REDIS_OK) {
- printf ("Error:%s\n", C->ERRSTR);
- return;
- }
- printf ("disconnected...\n");
- }
- int main (int argc, char **argv) {
- Signal (Sigpipe, sig_ign);
- struct Event_base *base = event_base_new ();
- Redisasynccontext *c = Redisasyncconnect ("127.0.0.1", 6379);
- if (c->err) {
- / * Let *c leak to now ... * /
- printf ("Error:%s\n", C->ERRSTR);
- return 1;
- }
- Redislibeventattach (c,base);
- Redisasyncsetconnectcallback (C,connectcallback);
- Redisasyncsetdisconnectcallback (C,disconnectcallback);
- Redisasynccommand (c, Subcallback, (char*) "Sub", "SUBSCRIBE foo");
- Event_base_dispatch (base);
- return 0;
- }
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