hiredis安装和使用介绍

hiredis安装和使用介绍

1.         redis安装和启动

在使用hiredis前需要安装并启动redis

安装:

1.make

2.make  PREFIX=/usr/local/redis install

前台:redis-server 该方式启动后关闭窗口后就会将server关闭

后台启动:

将redis安装目录下的redis.conf内的daemonize no改成daemonize yes

启动命令:redis-server redis.conf

2.         hiredis安装

在github中https://github.com/redis/hiredis下载代码

执行:

make

make install

 

3.         结构体介绍

Redis的链接信息保存:

typedef struct redisContext {

    int err; /* Error flags, 0 when there is no error */

    char errstr[128]; /* String representation of error when applicable */

    int fd;

    int flags;

    char *obuf; /* Write buffer */

    redisReader *reader; /* Protocol reader */

 

    enum redisConnectionType connection_type;

    struct timeval *timeout;

 

    struct {

        char *host;

        char *source_addr;

        int port;

    } tcp;

 

    struct {

        char *path;

    } unix_sock;

 

} redisContext;

 

/* redisCommand() 返回结构*/

typedef struct redisReply {

    int type; /* REDIS_REPLY_* */

    long long integer; /* 整型值当type= REDIS_REPLY_INTEGER */

    size_t len; /* 字符串的长度 */

    char *str; /* 当type为 REDIS_REPLY_ERROR 和REDIS_REPLY_STRING使用 */

    size_t elements; /* 元素个数,当type为 REDIS_REPLY_ARRAY */

    struct redisReply **element; /* 元素vector当type为REDIS_REPLY_ARRAY */

} redisReply;

 

4.         常用API介绍

redisContext *redisConnect(const char *ip,int port);

该函数用于创建redis的链接。redisContext保存链接的状态,在redisContext结构体中包含了一个整形变量err,当err不为0是,errstr变量中保存错误描述。在链接之后需要对链接状态进行检查。如下代码为官方提供:

redisContext *c = redisConnect("127.0.0.1", 6379);
if (c == NULL || c->err) {
    if (c) {
        printf("Error: %s\n", c->errstr);
        // handle error
    } else {
        printf("Can't allocate redis context\n");
    }
}

注:redisContext不是线程安全


void *redisCommand(redisContext *c, constchar *format, ...);

使用方法:

用于向redis发送指令,该函数接受的格式类似于printf

最简单的用法如下:

reply = redisCommand(context, "SET foo bar");

可以像使用sprintf一样使用它:

reply = redisCommand(context, "SET foo %s", value);

当需要传递二进制字符串时:

reply = redisCommand(context, "SET foo %b", value, (size_t) valuelen);

当需要传递多个参数时:

reply = redisCommand(context, "SET key:%s %s", myid, value);

响应类型介绍:

redisCommand当成功执行时会返回reply,当发生错误值返回值为NULL以及在context中的err域将被设置。一旦一个错误返回且context无法被重用,那么就需要重新建立一个链接。

redisCommand标准的返回类型是redisReply。其中type域用于表示哪种响应被接收:

* **`REDIS_REPLY_STATUS`**:

redisCommand状态响应,状态信息保存在reply->str,同时str的长度保存在reply->len

* **`REDIS_REPLY_ERROR`**:

响应错误,错误信息保存在str中

* **`REDIS_REPLY_INTEGER`**:

响应整形值,数据保存在reply->integer类型为long long

* **`REDIS_REPLY_NIL`**:

nil响应,没有数据可以访问。

* **`REDIS_REPLY_STRING`**:

响应字符串,信息保存在reply->str,同时str的长度保存在reply->len

* **`REDIS_REPLY_ARRAY`**:

多个块信息响应,元素个数保存在reply->elements,具体信息保存在reply->element[…index…]

void freeReplyObject(void *reply);

释放reply

void redisFree(redisContext *c);

立即断开链接、关闭socket以及释放context

其他API,包括非阻塞的接口请查看github中的README.md


5.测试代码

#include 
#include 
#include 
#include 
using namespace std;

int main(int argc,char *argv[])
{
    redisContext *context=NULL;
    redisReply *reply=NULL;
    //create connect
    context=redisConnect("127.0.0.1",6379);
    if (context == NULL || context->err) 
    {
        if (context) 
        {
            printf("Error: %s\n", context->errstr);
            redisFree(context);
        } 
        else 
        {
            printf("Can't allocate redis context\n");
        }
        return -1;
    }
    printf(" connect redis success\n!");
    reply=(redisReply *)redisCommand(context,"set test hello");
    if(reply==NULL)
    {
        printf("command execute error,errmsg=[%s]",context->errstr);
        redisFree(context);
        return -1;
    }
    //get return status
    if(reply->type!=REDIS_REPLY_STATUS||strcmp(reply->str,"OK")!=0)
    {
        printf("command execute happen some error,msg=[%s]",
            reply->str);
        freeReplyObject(reply);
        redisFree(context);
        return -1;
    }
    printf("execute[set test hello] success!\n");
    freeReplyObject(reply);
    //get key[test]
    reply=(redisReply *)redisCommand(context,"get test");
    if(reply==NULL)
    {
        printf("command execute error,errmsg=[%s]",context->errstr);
        redisFree(context);
        return -1;
    }
    //get return msg
    if(reply->type!=REDIS_REPLY_STRING)
    {
        printf("command execute happen some error,msg=[%s]",
            reply->str);
        freeReplyObject(reply);
        redisFree(context);
        return -1;
    }
    printf("[get test] return[%s]\n",reply->str);
    freeReplyObject(reply);
    redisFree(context);
    return 0;
}

编译指令:

g++ test.cpp -o test -lhiredis

执行:./test

结果:


 connect redis success

!execute[set test hello] success!

[get test] return[hello]





你可能感兴趣的:(hiredis安装和使用介绍)