一例C语言凭借hiredis连接redis

#include <stdio.h>
#include <hiredis/hiredis.h>
int main()
{
    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");
        }
    }
    redisReply *reply=NULL;
    // set
    reply = redisCommand(c, "set %s %s", "name", "程劲");
    printf("set is %s\n", reply->str);
    freeReplyObject(reply);

        // get
    reply = redisCommand(c, "get name");
    printf("name :%s\n", reply->str);
    freeReplyObject(reply);
    printf("Hello World!\n");
    return 0;
}

运行结果:

 

 

前提:在github上找到 hiredis库,下载下来。在解压的文件中运行 make  make install 完成libhireids.so的生成。

作为85后,习惯了在windows系统中,下个exe文件,双击安装,然后就可以在visual studio名正言顺的按照,某一款数据库或平台系统官方文档提供的示范代码,根据业务需求开发,现如今移到linux上,在终端上运行命令安装扩展库,还真是大开眼界

时下更流行的做法是某些平台或数据库的连接代码和API全都放在github上,既可以方便维护升级,也方便分享。不过总给人感觉很野路子,可能是我太保守了。这里使用了hiredis这一连接库,当然别忘了在Qt项目中,添加库libhiredis.so

posted @ 2020-11-19 03:43  saintdingtheGreat  阅读(291)  评论(0编辑  收藏  举报