Skip to content

Commit

Permalink
Allow omitting template paremeter in execCommandSync. (drogonframewor…
Browse files Browse the repository at this point in the history
  • Loading branch information
hwc0919 authored Aug 31, 2023
1 parent 53c8430 commit e2e5d6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
30 changes: 22 additions & 8 deletions nosql_lib/redis/inc/drogon/nosql/RedisClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,40 @@ class DROGON_EXPORT RedisClient
std::string_view command,
Args &&...args)
{
std::shared_ptr<std::promise<T>> pro(new std::promise<T>);
std::future<T> f = pro->get_future();
return execCommandSync<std::decay_t<decltype(processFunc)>>(
std::move(processFunc), command, std::forward<Args>(args)...);
}

/**
* @brief Execute a redis command synchronously
* Return type can be deduced automatically in this version.
*/
template <typename F, typename... Args>
std::invoke_result_t<F, const RedisResult &> execCommandSync(
F &&processFunc,
std::string_view command,
Args &&...args)
{
using Ret = std::invoke_result_t<F, const RedisResult &>;
std::promise<Ret> prom;
execCommandAsync(
[process = std::move(processFunc), pro](const RedisResult &result) {
[&processFunc, &prom](const RedisResult &result) {
try
{
pro->set_value(process(result));
prom.set_value(processFunc(result));
}
catch (...)
{
pro->set_exception(std::current_exception());
prom.set_exception(std::current_exception());
}
},
[pro](const RedisException &err) {
pro->set_exception(std::make_exception_ptr(err));
[&prom](const RedisException &err) {
prom.set_exception(std::make_exception_ptr(err));
},
command,
std::forward<Args>(args)...);

return f.get();
return prom.get_future().get();
}

/**
Expand Down
9 changes: 6 additions & 3 deletions nosql_lib/redis/tests/redis_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,14 @@ DROGON_TEST(RedisTest)
SUCCESS();
}

// 12. Test omit template parameter
try
{
redisClient->execCommandSync<int>([](const RedisResult &) { return 1; },
"del %s",
"sync_key");
auto i = redisClient->execCommandSync(
[](const RedisResult &r) { return r.asInteger(); },
"del %s",
"sync_key");
MANDATE(i == 1);
}
catch (const RedisException &err)
{
Expand Down

0 comments on commit e2e5d6d

Please sign in to comment.