sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

[QUESTION] #312

Closed rqb500 closed 2 years ago

rqb500 commented 2 years ago

Describe the problem I encountered a compilation problem when I started using redis plus plus.I downloaded and installed redis plus plus with vcpkg, but when running the sample code, the compiler prompts me the following error:

Severity Code Description Project File Line Suppression State Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::optional' (or there is no acceptable conversion) redistest E:\123\vcpkg\vcpkg\installed\x64-windows\include\sw\redis++\command_args.h 158

problem code: ` auto redis = Redis("tcp://127.0.0.1:6379"); auto mset_with_ttl_script = "(\ local len = #KEYS\ if (len == 0 or len + 1 ~= #ARGV) then return 0 end\ local ttl = tonumber(ARGV[len + 1])\ if (not ttl or ttl <= 0) then return 0 end\ for i = 1, len do redis.call(\"SET\", KEYS[i], ARGV[i], \"EX\", ttl) end\ return 1\ )";

// Set multiple key-value pairs with TTL of 60 seconds.
std::vector<OptionalString> vals;
redis.hmget("hash", { "field1", "field2" }, std::back_inserter(vals));
auto keys = { "key1", "key2", "key3" };
std::vector<std::string> args = { "val1", "val2", "val3", "60" };
redis.eval<long long>(mset_with_ttl_script, keys.begin(), keys.end(), vals.begin(), vals.end());//error here`

what should i do for correct using redis-plus-plus

Environment:

sewenew commented 2 years ago

There're two problems with your code:

auto mset_with_ttl_script = "(              // <-------- you missed a *R*
local len = #KEYS
if (len == 0 or len + 1 ~= #ARGV) then return 0 end
local ttl = tonumber(ARGV[len + 1])
if (not ttl or ttl <= 0) then return 0 end
for i = 1, len do redis.call("SET", KEYS[i], ARGV[i], "EX", ttl) end
return 1
)";

It should be: auto mset_with_ttl_script = R"(your script here)";

redis.eval<long long>(mset_with_ttl_script, keys.begin(), keys.end(),
             vals.begin(), vals.end());          // <--------- this should be args

You should pass args instead of vals to eval.

Regards

rqb500 commented 2 years ago

thanks, thats help much