redis
redis copied to clipboard
boost.redis
#include
#include "json/json.h"
#include "boost/redis/src.hpp" // #include <boost/lexical_cast.hpp> #include <boost/thread/locks.hpp> #include <boost/thread/shared_mutex.hpp>
#include <boost/redis/connection.hpp> #include <boost/redis/config.hpp> #include <boost/asio/co_spawn.hpp> #include <boost/asio/use_awaitable.hpp> #include <boost/asio/io_context.hpp>
#include <boost/asio/deferred.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/consign.hpp>
// #include
namespace asio = boost::asio; using boost::redis::config; using boost::redis::logger; using boost::redis::connection; using boost::redis::request; using boost::redis::response;
asio::awaitable
// Response where the PONG response will be stored.
response<std::string> resp;
// Executes the request.
co_await conn->async_exec(req, resp, asio::deferred);
conn->cancel();
std::cout << "PING: " << std::get<0>(resp).value() << std::endl;
sleep(1000*1000*1000);
}
auto main(int argc, char * argv[]) -> int {
try { config cfg;
if (argc == 5) {
cfg.addr.host = argv[1];
cfg.addr.port = argv[2];
// cfg.username = argv[3];
cfg.password = argv[4];
cfg.use_ssl = true;
}
std::cout << "host: " << cfg.addr.host
<< "port: " << cfg.addr.port
<< "name: " << cfg.username
<< "passwd: " << cfg.password
<< std::endl;
asio::io_context ioc;
asio::co_spawn(ioc, co_main(cfg), [](std::exception_ptr p) {
if (p) {
std::rethrow_exception(p);
}
});
ioc.run();
sleep(1000*1000*1000);
} catch (std::exception const& e) { std::cerr << "(main)== " << e.what() << std::endl; return 1; } }
This problem has been consistently occurring
(main)== Operation canceled [system:125]
Can you provide a complete example? This would be a very friendly way for new users of the boost library? thanks
Hi @zxyAcmen, here is a complete example (notice I am passing boost::redis::logger::level::debug
to async_run
to get detailed information about internal steps)
#include <boost/redis/connection.hpp>
#include <boost/asio/deferred.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/consign.hpp>
#include <iostream>
#include <boost/redis/src.hpp>
namespace asio = boost::asio;
using boost::redis::config;
using boost::redis::connection;
using boost::redis::logger;
using boost::redis::request;
using boost::redis::response;
auto co_main(config cfg) -> asio::awaitable<void>
{
auto conn = std::make_shared<connection>(co_await asio::this_coro::executor);
conn->async_run(cfg, {logger::level::debug}, asio::consign(asio::detached, conn));
// A request containing only a ping command.
request req;
req.push("PING", "Hello world");
// Response where the PONG response will be stored.
response<std::string> resp;
// Executes the request.
co_await conn->async_exec(req, resp, asio::deferred);
conn->cancel();
std::cout << "PING: " << std::get<0>(resp).value() << std::endl;
}
auto main(int argc, char * argv[]) -> int
{
try {
config cfg;
asio::io_context ioc;
asio::co_spawn(ioc, co_main(cfg), [](std::exception_ptr p) {
if (p)
std::rethrow_exception(p);
});
ioc.run();
} catch (std::exception const& e) {
std::cerr << "(main) " << e.what() << std::endl;
return 1;
}
}