hiredis读写(同步式)

本文记录一下hiredis同步读写的操作方式。

简单的实例

#include <iostream>
#include <stdio.h>

extern "C" {
#include <hiredis.h> 
}

int test_1()
{
    redisContext *c = redisConnect( "127.0.0.1", 6379 );
    if ( c != NULL && c->err) {
        std::cout << "Error: " << c->errstr << std::endl;
        return -1;
    }
    std::cout << "hello world." << std::endl;

    redisReply *reply = (redisReply *)redisCommand( c, "GET foo");

    if ( reply == nullptr ) { 
        std::cout << "get redis failed" << std::endl;
        return -2;
    }

    if ( reply->type == REDIS_REPLY_ERROR ) {
        std::cout << "type == REDIS_REPLY_ERROR" << std::endl;
        freeReplyObject( reply ); 
        return -3;
    }

    std::cout << "set redis success. " << "SET foo bar" << std::endl;
    freeReplyObject( reply ); 



    return 0;
}

函数好像也只有非常简单的command。在使用的时候需要注意其中的内存用完是需要释放掉的。

你可能感兴趣的:(C语言,实例,hiredis,同步方式)