基本功能步驟:
1,下載安裝Redis,下載地址:http://www.redis.cn/download.html。
2,下載安裝hiredis,下載地址:https://github.com/redis/hiredis。
將libhiredis.so放到/usr/lib/目錄下。
3,在hiredis-master目錄下編寫客戶端程序。
- #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;
- }
編譯命令:gcc -o redistest redistest.c -lhiredis
4,啟動服務器,運行客戶端程序。
Redis pub/sub功能實現。
發布pub功能:
- #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;
- }
訂閱sub功能:
- #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 for 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編譯命令同上,sub編譯命令:gcc -o sub sub.c -lhiredis -levent
注意:
1,編譯基本程序時可能需要建立鏈接:ln -s libhiredis.so libhiredis.so.0.12
2,1,訂閱sub需要安裝libevent-dev。