hiredis读写(同步式)

来源:互联网 发布:西门子828d编程视频 编辑:程序博客网 时间:2024/05/16 14:53

本文记录一下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。在使用的时候需要注意其中的内存用完是需要释放掉的。

0 0
原创粉丝点击