ubuntu中使用QT、C++使用redis、hiredis记录

ubuntu中使用QT、C++使用redis、hiredis记录

参考:

https://blog.csdn.net/qq78442761/article/details/103084824
https://blog.csdn.net/MoriingRain/article/details/115742181

本机环境
ubuntu20.04

一、安装redis

这一部分参考:菜鸟教程-redis安装

二、安装hiredis

C++调用redis需要配合hiredis一起使用,因此需要事先安装hiredis。
1.下载hiredis包

git clone https://github.com/redis/hiredis

2.进入hiredis文件夹

在hiredis文件夹下开启终端,并输入

make
sudo make install

在这里插入图片描述

三、在QT中配置hiredis

1.新建项目
在这里插入图片描述
2.Pro文件添加hiredis

INCLUDEPATH += /home/cj/redis/hiredis
LIBS += -L /usr/local/lib -lhiredis
HEADERS += /home/cj/redis/hiredis/adapters/qt.h

1).INCLUDEPATH += 中这个地址写入你前面下载hiredis的安装包位置
在这里插入图片描述
在这里插入图片描述
2)LIBS +=这个地方写入hiredis安装位置
在这里插入图片描述
3)HEADERS += 这个将qt.h加入

3.测试代码
在main.cpp文件中写入

#include <QCoreApplication>
#include <adapters/qt.h>
#include <QDebug>

void getCallback(redisAsyncContext *ctx, void *r, void *privdata){

    qDebug() << "getCallback called";

    redisReply *reply = static_cast<redisReply*>(r);
    if(reply == nullptr){

        qDebug() << "The reply is nullptr";
        return;
    }

    qDebug() << "result: ";

    for(int i = 0; i < reply->elements; i++){

        qDebug() << "key: " << reply->element[i]->str;
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    redisAsyncContext *ctx = redisAsyncConnect("127.0.0.1", 6379);

    RedisQtAdapter adapter;

    if(ctx->err){

        qDebug()<< "error: " << ctx->errstr;
        redisAsyncFree(ctx);
        return 0;
    }
    adapter.setContext(ctx);

    redisAsyncCommand(ctx, nullptr, nullptr, "auth xxxxxxxxx");
    redisAsyncCommand(ctx, getCallback, nullptr, "KEYS *");
    qDebug() << "over";

    return a.exec();
}

运行结果
在这里插入图片描述

四、C++直接使用hiredis

编辑器依旧使用QT
1.建立纯C++项目
在这里插入图片描述
2.修改pro文件
加入:

LIBS += -L /usr/local/lib -lhiredis

在这里插入图片描述
3.main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <hiredis/hiredis.h>

int main()
{
    redisContext * rc = redisConnect("127.0.0.1",6379);
    assert(rc != NULL);

    char* com = "hmset user name jack age 18 sex male height 180";
    redisReply* res =(redisReply*)redisCommand(rc,com);

    if(res->type == REDIS_REPLY_STATUS)
    {
        printf("Success %s\n",res->str);
    }
    else
        printf("fail\n");

    com = "hgetall user";
    res = (redisReply*)redisCommand(rc,com);
    if(res->type == REDIS_REPLY_ARRAY)
    {
        for(int i = 0; i < res->elements; i++)
        {
            if(i%2 != 0)
                printf("%s\n",res->element[i]->str);
            else
                printf("%s",res->element[i]->str);
        }
    }
    else if(res->type == REDIS_REPLY_STRING)
    {
        printf("%s",res->str);
    }

    freeReplyObject(res);
    redisFree(rc);
    return 1;
}


效果
在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
QT使用异步Redis,可以使用hiredis库。hiredis是一个C语言库,提供了异步和同步两种方式访问Redis。可以通过在QT项目添加hiredis库文件和头文件,然后在代码使用异步API来实现异步Redis的访问。 以下是使用hiredis来进行异步Redis访问的基本步骤: 1. 在QT项目添加hiredis库文件和头文件。 2. 创建一个异步Redis上下文对象,并设置连接信息(IP地址、端口号、密码等)。 ```c redisAsyncContext *context = redisAsyncConnect("127.0.0.1", 6379); if (context == NULL || context->err) { if (context) { qDebug() << "Error: " << context->errstr; redisAsyncFree(context); } else { qDebug() << "Can't allocate redis context"; } return; } ``` 3. 设置连接回调函数和命令回调函数。 ```c redisAsyncSetConnectCallback(context, connectCallback); redisAsyncSetDisconnectCallback(context, disconnectCallback); ``` 4. 发送Redis命令。 ```c redisAsyncCommand(context, commandCallback, (void *)reply, "SET key value"); ``` 5. 在命令回调函数处理结果。 ```c void commandCallback(redisAsyncContext *context, void *reply, void *privdata) { redisReply *r = (redisReply *)reply; if (reply == NULL) { qDebug() << "Error: no reply received"; return; } if (r->type == REDIS_REPLY_ERROR) { qDebug() << "Error: " << r->str; } else { qDebug() << "Success: " << r->str; } } ``` 6. 在需要关闭连接时,调用redisAsyncDisconnect函数。 ```c redisAsyncDisconnect(context); ``` 注意,以上步骤只是一个简单的示例。具体的实现方式需要根据实际需求进行调整。另外,需要注意的是,hiredis的异步API是基于事件驱动的,因此需要在QT项目添加事件驱动库(如libevent)来支持异步Redis访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小俊俊的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值