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 }

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM