c++ - 关于返回类型的 Redis 错误

标签 c++ redis return-type

我最近在我的一个 C++ 程序中包含了一个 Redis 连接。我决定使用 redox库,因为它看起来很容易使用并且不依赖于 boost 库。

我使用连接将值插入到 redis 中的列表中。该命令大部分时间都有效,但有时我会收到一条错误消息,指出 Received reply of type 3, expected type 1 or 5. 经过大量搜索,我在 hiredis.h 中找到了这些返回类型。头文件。该库似乎需要 StringStatus 回复类型,但收到的是 Integer 类型。

不幸的是,我还没有找到任何关于这意味着什么以及如何解决这个问题的信息。尤其是这些代码有时有效,有时却不让我感到困惑。

在我的用例中,我在 celery 中插入了一个包含 json 字典的字符串值。格式(但本质上只是一个字符串)到列表中。我很确定它与字符串的组成方式无关,因为通过 redis-cli 客户端手动插入相同的字符串工作正常。

我插入消息的代码是:

redox::Redox rdx;
try {
    if(!rdx.connect("localhost", 6379)){
        cerr << "Could not connect to redis" << endl;
    }

    redox::Command<string>& c = rdx.commandSync<string>({"lpush", "queue_name", message});
    if(!c.ok()) {
        cerr << "Error while communicating with redis" << c.status() << endl;
    }
} catch (runtime_error& e) {
    cerr << "send_message: Exception in redox: " << e.what() << endl;
}

打印的错误是 !c.ok() 检查之后的错误。

感谢您的帮助。

最佳答案

您遇到的问题是由于您使用字符串作为响应的参数。

documentation of redox 中所述:

This statement tells redox to run the command GET hello. The <string> template parameter means that we want the reply to be put into a string and that we expect the server to respond with something that can be put into a string

但这行得通,因为示例使用的是预期返回字符串的“GET”命令。在您使用的“LPUSH”命令的情况下,返回结果是一个整数,在使用 redis-cli 发出命令时可以看到

127.0.0.1:6379> lpush "test" test
(integer) 1

因此您必须使用带有整数参数的响应,可能的响应的完整列表如所列here是:

<redisReply*>: All reply types, returns the hiredis struct directly
<char*>: Simple Strings, Bulk Strings
<std::string>: Simple Strings, Bulk Strings
<long long int>: Integers
<int>: Integers (careful about overflow, long long int recommended)
<std::nullptr_t>: Null Bulk Strings, any other receiving a nil reply will get a NIL_REPLY status
<std::vector<std::string>>: Arrays of Simple Strings or Bulk Strings (in received order)
<std::set<std::string>>: Arrays of Simple Strings or Bulk Strings (in sorted order)
<std::unordered_set<std::string>>: Arrays of Simple Strings or Bulk Strings (in no order)

所以像这样的事情会做:

redox::Redox rdx;
try {
  if(!rdx.connect("localhost", 6379)){
    cerr << "Could not connect to redis" << endl;
  }

  redox::Command<int>& c = rdx.commandSync<int>({"lpush", "queue_name", message});

  if(!c.ok()) {
    cerr << "Error while communicating with redis" << c.status() << endl;
}}catch (runtime_error& e) {
    cerr << "send_message: Exception in redox: " << e.what() << endl;
}

或者如果使用 lamda:

redox::Redox rdx;
try {
  if(!rdx.connect("localhost", 6379)){
    cerr << "Could not connect to redis" << endl;
  }

  rdx.commandSync<int>(
    {"lpush", "queue_name", message},
    [](redox::Command<int>& response){
      if(!response.ok()){
        cerr << "Error while communicating with redis" << c.status() << endl;
  }});  
}catch (runtime_error& e) {
    cerr << "send_message: Exception in redox: " << e.what() << endl;
}

关于c++ - 关于返回类型的 Redis 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34634421/

相关文章:

c++ - 访问说明符和虚函数

c++ - 有符号到无符号转换,并返回,整数的定义行为?

database - Redis:我可以为不同的键保存超过 1 个值吗?

Redis 使用查询更新

redis - 我们可以在redis中存储多维数组吗

c++ - 如何使用前一个 block 的哈希值链接两个 block ?

c++ - C++11 预处理器有哪些新功能?

c - 如何通过 C 中的地址来访问 char 数组中的数据?

c++ - Operator=声明问题

c++ - 对不同类型使用具有不同返回值的模板函数不起作用