boostorg / redis

An async redis client designed for performance and scalability
https://www.boost.org/doc/libs/develop/libs/redis/doc/html/index.html
Boost Software License 1.0
212 stars 38 forks source link

boost.redis #187

Open zxyAcmen opened 3 months ago

zxyAcmen commented 3 months ago

include

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 co_main(config cfg) { auto conn = std::make_shared(co_await asio::this_coro::executor); conn->async_run(cfg, {}, asio::consign(asio::detached, conn)); std::cout << "host:===============" << std::endl; // 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;
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] image

Can you provide a complete example? This would be a very friendly way for new users of the boost library? thanks

mzimbres commented 3 months ago

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;
   }
}