hiredis

hiredis是redis开源库对外发布的客户端API包。

当redis-server配置启动后,可以通过hiredis操作redis资源。

主要分为:

  strings、hash、lists、sets、sort sets

hiredis使用较为简单,下面是几个主要的函数和对象:

 /*
作用:用于连接redis服务器
 ip : 为redis的ip地址;
 port: 端口地址;
 tv:连接超时的参数;
*/
 redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv);

 

/*
作用:执行命令
c:redisConnectWitTimeout返回的对象;
format:命令参数;
*/

void *redisCommand(redisContext *c, const char *format, ...)
 /*
 说明:redisCommand返回的对象指针,也就是已经命令返回的结果数据
*/
 
 typedef struct redisReply {
      int type; /* REDIS_REPLY_* */
      long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
     int len; /* Length of string */
      char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
      size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
      struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
  } redisReply;

 

 

来一个具体的实例

 1 /*需要配置,才能编译通过*/
 2 #include <iostream>
 3 #include "hiredis.h"
 4 
 5 #define MAX_LEN  64
 6 
 7 int main()
 8 {
 9     timeval timeout = {1,500000};
10     char ip[MAX_LEN],passwd[MAX_LEN];
11     memset(ip,0,MAX_LEN);
12     memset(passwd,0,MAX_LEN);
13     sprintf(ip,"*****");
14     sprintf(passwd,"******");
15     uint32_t port = 6379;
16     redisContext  *m_pRedisContext = redisConnectWithTimeout(ip,port,timeout);
17     if(m_pRedisContext->err){
18         std::cout << "log, redis connect error\n";
19         return 0;
20     }
21     
22     redisReply *reply = static_cast<redisReply*>(redisCommand(m_pRedisContext,\
23                 "AUTH %s",passwd));
24     if(!reply){
25         std::cout << "log, redis command error, " << m_pRedisContext->errstr << \
26             std::endl;
27         return 0;
28     }
29 
30     std::cout << "AUTH " << passwd << reply->str << std::endl;
31     freeReplyObject(reply);
32    
33     //create datadase id = 1;
34     uint32_t index  = 1;
35     reply = static_cast<redisReply*>(redisCommand(m_pRedisContext,"SELECT %d",1));
36     if(!reply) {
37         std::cout << "log, redis command error," << m_pRedisContext->errstr << \
38             std::endl;
39         freeReplyObject(reply);
40         return 0;
41     }
42 
43     std::cout << "SELECT " << index << reply->str << std::endl;
44     freeReplyObject(reply);
45     
46     uint32_t id = 1;
47     reply = static_cast<redisReply*>(redisCommand(m_pRedisContext, \
48                 "HMSET user:%u %s %s %s %s",id,"name","xuxu","age","24"));
49     if(!reply){
50         std::cout << "log, redis command error," << m_pRedisContext->errstr << \
51             std::endl;
52         freeReplyObject(reply);
53         return 0;
54     }
55     
56     reply = static_cast<redisReply*>(redisCommand(m_pRedisContext,\
57                 "SET name:%s %s","1","liushun"));
58     if(!reply){
59         std::cout << "log, redis command error, " << m_pRedisContext->errstr << \
60             std::endl;
61         freeReplyObject(reply);
62         return 0;
63   }
64 
65     reply = static_cast<redisReply*>(redisCommand(m_pRedisContext,"GET name:%s","1"));
66     if(!reply){
67         std::cout << "log, redis command error," << m_pRedisContext->errstr << \
68             std::endl;
69         freeReplyObject(reply);
70         return 0;
71     }
72 
73     std::cout << reply->str << std::endl;
74     freeReplyObject(reply);
75   
76     return 0;
77 }

 

 

 

 

posted on 2013-08-06 17:18  笔记吧... 可能只有自己看得懂  阅读(8345)  评论(0编辑  收藏  举报