Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mset Callback interface #551

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/sw/redis++/async_redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ class AsyncRedis {
Future<void> mset(std::initializer_list<T> il) {
return mset(il.begin(), il.end());
}

template <typename Input, typename Callback>
auto mset(Input first, Input last, Callback &&cb)
-> typename std::enable_if<IsInvocable<typename std::decay<Callback>::type, Future<void> &&>::value, void>::type {
range_check("MSET", first, last);

_callback_fmt_command<void>(std::forward<Callback>(cb), fmt::mset<Input>, first, last);
}

template <typename Input>
Future<bool> msetnx(Input first, Input last) {
Expand Down
8 changes: 8 additions & 0 deletions src/sw/redis++/async_redis_cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ class AsyncRedisCluster {
return mset(il.begin(), il.end());
}

template <typename Input, typename Callback>
auto mset(Input first, Input last, Callback &&cb)
-> typename std::enable_if<IsInvocable<typename std::decay<Callback>::type, Future<void> &&>::value, void>::type {
range_check("MSET", first, last);

_callback_fmt_command<void>(std::forward<Callback>(cb), fmt::mset<Input>, first, last);
}

template <typename Input>
Future<bool> msetnx(Input first, Input last) {
range_check("MSETNX", first, last);
Expand Down
7 changes: 7 additions & 0 deletions test/src/sw/redis++/async_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ void AsyncTest<RedisInstance>::_test_hash() {
this->set_ready();
});
_wait();

set_ready(false);
std::map<std::string, std::string> mkeys = {{test_key("str")+"1", "val1"}, {test_key("str")+"2", "val2"}};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Use std::unordered_map instead of std::map, since this file doesn't include , some compiler might fail to compile.
  2. Register these two keys in KeyDeleter at the beginning of this method, so that they will be automatically deleted when the test is done.
auto key = test_key("set");
auto key1 = test_key("str1")
auto key2 = test_key("str2");

KeyDeleter<RedisInstance> deleter(_redis, {key, key1, key2});

// other code here

std::map<std::string, std::string> mkeys = {{key1, "val1"}, {key2, "val2"}}

_redis.mset(mkeys.begin(), mkeys.end(), [this](Future<void> &&fut) {
this->set_ready();
});
_wait();
}

template <typename RedisInstance>
Expand Down
Loading