/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com) * * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE.txt) */ #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) #include #include #include #include "common.hpp" #include #include #include #include #include #include // Include this in no more than one .cpp file. #include namespace net = boost::asio; using namespace net::experimental::awaitable_operators; using aedis::resp3::request; using aedis::adapt; using namespace boost::json; struct user { std::string name; std::string age; std::string country; }; void tag_invoke(value_from_tag, value& jv, user const& u) { jv = { {"name", u.name} , {"age", u.age} , {"country", u.country} }; } template void extract(object const& obj, T& t, boost::string_view key) { t = value_to(obj.at(key)); } auto tag_invoke(value_to_tag, value const& jv) { user u; object const& obj = jv.as_object(); extract(obj, u.name, "name"); extract(obj, u.age, "age"); extract(obj, u.country, "country"); return u; } // Serializes void to_bulk(std::pmr::string& to, user const& u) { aedis::resp3::to_bulk(to, serialize(value_from(u))); } // Deserializes void from_bulk(user& u, boost::string_view sv, boost::system::error_code&) { value jv = parse(sv); u = value_to(jv); } auto operator<<(std::ostream& os, user const& u) -> std::ostream& { os << "Name: " << u.name << "\n" << "Age: " << u.age << "\n" << "Country: " << u.country; return os; } auto operator<(user const& a, user const& b) { return std::tie(a.name, a.age, a.country) < std::tie(b.name, b.age, b.country); } net::awaitable async_main() { std::set users {{"Joao", "58", "Brazil"} , {"Serge", "60", "France"}}; request req; req.get_config().cancel_on_connection_lost = true; req.push("HELLO", 3); req.push_range("SADD", "sadd-key", users); // Sends req.push("SMEMBERS", "sadd-key"); // Retrieves req.push("QUIT"); std::tuple, std::string> resp; auto conn = std::make_shared(co_await net::this_coro::executor); co_await connect(conn, "127.0.0.1", "6379"); co_await (conn->async_run() || conn->async_exec(req, adapt(resp))); for (auto const& e: std::get<2>(resp)) std::cout << e << "\n"; } #endif // defined(BOOST_ASIO_HAS_CO_AWAIT)