vipshop / hiredis-vip

Support redis cluster. Maintained and used at vipshop.
BSD 3-Clause "New" or "Revised" License
322 stars 173 forks source link

Passing a string to redisClusterCommand() as an argument #109

Closed LazyHippogriff closed 5 years ago

LazyHippogriff commented 5 years ago

The below code works fine. I can get the values back from the server: redisReply reply; reply = (redisReply)(redisClusterCommand(cc,"hmget test_hash bin1_name bin3_name"));

However, the below code doesn't work: redisReply reply; string set_name="test_hash"; string bin_names="bin1_name bin3_name"; reply = (redisReply)(redisClusterCommand(cc,"hmget %s %s",set_name.c_str(),bin_names.c_str()));

How can I make the above code work?

thakurajayL commented 5 years ago

after expansion your API will look like this, reply = (redisReply)(redisClusterCommand(cc,"hmget test_hash bin1_name bin3_name"); When you say it does not work. Means you are getting local error from the client library ? or you are getting rejection from the server ?

LazyHippogriff commented 5 years ago

I meant that I'm not getting the values. Here's the code in bit more detail:

redisReply *reply;
reply = (redisReply*)(redisClusterCommand(cc,"hmget test_hash bin1_name bin3_name"));
if (cc != NULL && cc->err)
        printf("Error: %s\n", cc->errstr);
        else
{
        cout<<endl<<"reply type: "<<reply->type<<endl;
        if (reply->type == REDIS_REPLY_ARRAY) {
                for (int j = 0; j < reply->elements; j++) {
                        printf("%u) %s\n", j, reply->element[j]->str);
                }
        }
}
freeReplyObject(reply);

string set_name="test_hash";
string bin_names="bin1_name bin3_name";
reply = (redisReply*)(redisClusterCommand(cc,"hmget %s %s",set_name.c_str(),bin_names.c_str()));
if (cc != NULL && cc->err)
        printf("Error: %s\n", cc->errstr);
        else
{
        cout<<endl<<"reply type: "<<reply->type<<endl;
        if (reply->type == REDIS_REPLY_ARRAY) {
                for (int j = 0; j < reply->elements; j++) {
                        printf("%u) %s\n", j, reply->element[j]->str);
                }
        }
}
freeReplyObject(reply);

Output

reply type: 2
0)bin1_value
1)bin3_val

reply type: 2
0) (null)
thakurajayL commented 5 years ago

I am not sure about the syntax you used, redisClusterCommand(cc,"hmget %s %s",set_name.c_str(),bin_names.c_str()) Can you please form the string first and then pass it like below string cmd = "hmget" + set_name + bin_names; redisClusterCommand(cc, cmd.c_str());

Thanks Ajay

LazyHippogriff commented 5 years ago
redisReply *reply;
string set_name="test_hash";
string bin_names="bin1_name bin3_name";
string command = string("hmget ")+set_name+string(" ")+bin_names;
cout<<endl<<command<<endl;
reply = (redisReply*)(redisClusterCommand(cc,"%s",command.c_str()));
if (cc != NULL && cc->err)
        printf("Error: %s\n", cc->errstr);
        else
{
        cout<<endl<<"reply type: "<<reply->type<<endl;
        if (reply->type == REDIS_REPLY_ARRAY) {
                for (int j = 0; j < reply->elements; j++) {
                        printf("%u) %s\n", j, reply->element[j]->str);
                }
        }
}
freeReplyObject(reply);

Output


hmget test_hash bin1_name bin3_name
Error: Parse command error. Cmd type: 0, state: 5, break position: 44.
LazyHippogriff commented 5 years ago

Sorry my code above is not exactly as per your suggestion. You're right. Your suggestion worked.

redisReply *reply;
string set_name="test_hash";
string bin_names="bin1_name bin3_name";
string command = string("hmget ")+set_name+string(" ")+bin_names;
cout<<endl<<command<<endl;
reply = (redisReply*)(redisClusterCommand(cc, command.c_str()));
if (cc != NULL && cc->err)
        printf("Error: %s\n", cc->errstr);
        else
{
        cout<<endl<<"reply type: "<<reply->type<<endl;
        if (reply->type == REDIS_REPLY_ARRAY) {
                for (int j = 0; j < reply->elements; j++) {
                        printf("%u) %s\n", j, reply->element[j]->str);
                }
        }
}
freeReplyObject(reply);

Output

hmget test_hash bin1_name bin3_name

reply type: 2
0) bin1_value
1) bin3_val