mirror of
https://github.com/boostorg/redis.git
synced 2026-01-19 04:42:09 +00:00
Adds support for optional fields
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
/* Copyright (c) 2018-2025 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
@@ -24,13 +24,25 @@ using boost::asio::awaitable;
|
||||
using boost::asio::detached;
|
||||
using boost::asio::consign;
|
||||
|
||||
template<class T>
|
||||
std::ostream& operator<<(std::ostream& os, std::optional<T> const& opt)
|
||||
{
|
||||
if (opt.has_value())
|
||||
std::cout << opt.value();
|
||||
else
|
||||
std::cout << "null";
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
void print(std::map<std::string, std::string> const& cont)
|
||||
{
|
||||
for (auto const& e: cont)
|
||||
std::cout << e.first << ": " << e.second << "\n";
|
||||
}
|
||||
|
||||
void print(std::vector<int> const& cont)
|
||||
template <class T>
|
||||
void print(std::vector<T> const& cont)
|
||||
{
|
||||
for (auto const& e: cont) std::cout << e << " ";
|
||||
std::cout << "\n";
|
||||
@@ -48,6 +60,7 @@ auto store(std::shared_ptr<connection> conn) -> awaitable<void>
|
||||
request req;
|
||||
req.push_range("RPUSH", "rpush-key", vec);
|
||||
req.push_range("HSET", "hset-key", map);
|
||||
req.push("SET", "key", "value");
|
||||
|
||||
co_await conn->async_exec(req, ignore);
|
||||
}
|
||||
@@ -67,6 +80,21 @@ auto hgetall(std::shared_ptr<connection> conn) -> awaitable<void>
|
||||
print(std::get<0>(resp).value());
|
||||
}
|
||||
|
||||
auto mget(std::shared_ptr<connection> conn) -> awaitable<void>
|
||||
{
|
||||
// A request contains multiple commands.
|
||||
request req;
|
||||
req.push("MGET", "key", "non-existing-key");
|
||||
|
||||
// Responses as tuple elements.
|
||||
response<std::vector<std::optional<std::string>>> resp;
|
||||
|
||||
// Executes the request and reads the response.
|
||||
co_await conn->async_exec(req, resp);
|
||||
|
||||
print(std::get<0>(resp).value());
|
||||
}
|
||||
|
||||
// Retrieves in a transaction.
|
||||
auto transaction(std::shared_ptr<connection> conn) -> awaitable<void>
|
||||
{
|
||||
@@ -74,19 +102,26 @@ auto transaction(std::shared_ptr<connection> conn) -> awaitable<void>
|
||||
req.push("MULTI");
|
||||
req.push("LRANGE", "rpush-key", 0, -1); // Retrieves
|
||||
req.push("HGETALL", "hset-key"); // Retrieves
|
||||
req.push("MGET", "key", "non-existing-key");
|
||||
req.push("EXEC");
|
||||
|
||||
response<
|
||||
ignore_t, // multi
|
||||
ignore_t, // lrange
|
||||
ignore_t, // hgetall
|
||||
response<std::optional<std::vector<int>>, std::optional<std::map<std::string, std::string>>> // exec
|
||||
ignore_t, // mget
|
||||
response<
|
||||
std::optional<std::vector<int>>,
|
||||
std::optional<std::map<std::string, std::string>>,
|
||||
std::optional<std::vector<std::optional<std::string>>>
|
||||
> // exec
|
||||
> resp;
|
||||
|
||||
co_await conn->async_exec(req, resp);
|
||||
|
||||
print(std::get<0>(std::get<3>(resp).value()).value().value());
|
||||
print(std::get<1>(std::get<3>(resp).value()).value().value());
|
||||
print(std::get<0>(std::get<4>(resp).value()).value().value());
|
||||
print(std::get<1>(std::get<4>(resp).value()).value().value());
|
||||
print(std::get<2>(std::get<4>(resp).value()).value().value());
|
||||
}
|
||||
|
||||
// Called from the main function (see main.cpp)
|
||||
@@ -98,6 +133,7 @@ awaitable<void> co_main(config cfg)
|
||||
co_await store(conn);
|
||||
co_await transaction(conn);
|
||||
co_await hgetall(conn);
|
||||
co_await mget(conn);
|
||||
conn->cancel();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,14 @@
|
||||
#include <boost/redis/resp3/serialization.hpp>
|
||||
|
||||
namespace asio = boost::asio;
|
||||
namespace resp3 = boost::redis::resp3;
|
||||
using namespace boost::describe;
|
||||
using boost::redis::request;
|
||||
using boost::redis::response;
|
||||
using boost::redis::ignore_t;
|
||||
using boost::redis::config;
|
||||
using boost::redis::connection;
|
||||
using boost::redis::resp3::node_view;
|
||||
|
||||
// Struct that will be stored in Redis using json serialization.
|
||||
struct user {
|
||||
@@ -40,10 +42,18 @@ BOOST_DESCRIBE_STRUCT(user, (), (name, age, country))
|
||||
|
||||
// Boost.Redis customization points (example/json.hpp)
|
||||
void boost_redis_to_bulk(std::string& to, user const& u)
|
||||
{ boost::redis::resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u))); }
|
||||
{
|
||||
resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u)));
|
||||
}
|
||||
|
||||
void boost_redis_from_bulk(user& u, std::string_view sv, boost::system::error_code&)
|
||||
{ u = boost::json::value_to<user>(boost::json::parse(sv)); }
|
||||
void
|
||||
boost_redis_from_bulk(
|
||||
user& u,
|
||||
node_view const& node,
|
||||
boost::system::error_code&)
|
||||
{
|
||||
u = boost::json::value_to<user>(boost::json::parse(node.value));
|
||||
}
|
||||
|
||||
auto co_main(config cfg) -> asio::awaitable<void>
|
||||
{
|
||||
|
||||
@@ -19,12 +19,14 @@
|
||||
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
|
||||
|
||||
namespace asio = boost::asio;
|
||||
namespace resp3 = boost::redis::resp3;
|
||||
using boost::redis::request;
|
||||
using boost::redis::response;
|
||||
using boost::redis::operation;
|
||||
using boost::redis::ignore_t;
|
||||
using boost::redis::config;
|
||||
using boost::redis::connection;
|
||||
using boost::redis::resp3::node_view;
|
||||
|
||||
// The protobuf type described in example/person.proto
|
||||
using tutorial::person;
|
||||
@@ -42,12 +44,16 @@ void boost_redis_to_bulk(std::string& to, person const& u)
|
||||
if (!u.SerializeToString(&tmp))
|
||||
throw boost::system::system_error(boost::redis::error::invalid_data_type);
|
||||
|
||||
boost::redis::resp3::boost_redis_to_bulk(to, tmp);
|
||||
resp3::boost_redis_to_bulk(to, tmp);
|
||||
}
|
||||
|
||||
void boost_redis_from_bulk(person& u, std::string_view sv, boost::system::error_code& ec)
|
||||
void
|
||||
boost_redis_from_bulk(
|
||||
person& u,
|
||||
node_view const& node,
|
||||
boost::system::error_code& ec)
|
||||
{
|
||||
std::string const tmp {sv};
|
||||
std::string const tmp {node.value};
|
||||
if (!u.ParseFromString(tmp))
|
||||
ec = boost::redis::error::invalid_data_type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user