2
0
mirror of https://github.com/boostorg/redis.git synced 2026-02-08 23:12:14 +00:00
2020-12-13 09:52:35 +01:00
2020-11-28 21:24:11 +01:00
2020-12-13 09:52:35 +01:00
2020-12-13 09:52:35 +01:00
2020-12-12 18:08:08 +01:00
2020-12-12 18:08:08 +01:00
2020-09-19 10:55:46 +02:00
2019-11-19 22:19:03 +01:00
2020-12-12 18:08:08 +01:00
2020-12-12 18:09:50 +01:00

Aedis

Aedis is a redis client designed for seamless integration with async code while providing a easy and intuitive interface. To use this library include aedis.hpp in your project.

Tutoria and examples

Below we show how to use the library focused in sync and async code.

Sync

void sync_example1()
{
   io_context ioc {1};

   tcp::resolver resv(ioc);
   tcp::socket socket {ioc};
   net::connect(socket, resv.resolve("127.0.0.1", "6379"));

   resp::pipeline p;
   p.ping();

   net::write(socket, buffer(p.payload));

   resp::buffer buffer;
   resp::response res;
   resp::read(socket, buffer, res);

   // res.result contains the response as std::vector<std::string>.
}

The example above is overly simple. In real world cases it is necessary, for many reasons to keep reading from the socket, for example to detect the connection has been lost or to be able to deal with redis unsolicited events. A more realistic example therefore is

void sync_example2()
{
   io_context ioc {1};

   tcp::resolver resv(ioc);
   tcp::socket socket {ioc};
   net::connect(socket, resv.resolve("127.0.0.1", "6379"));

   resp::pipeline p;
   p.multi();
   p.ping();
   p.set("Name", {"Marcelo"});
   p.incr("Age");
   p.exec();
   p.quit();

   net::write(socket, buffer(p.payload));

   resp::buffer buffer;
   for (;;) {
      boost::system::error_code ec;
      resp::response res;
      resp::read(socket, buffer, res, ec);
      if (ec) {
	 std::cerr << ec.message() << std::endl;
	 break;
      }
      resp::print(res.result);
   }
}

In this example we add more commands to the pipeline, they will be all sent together to redis improving performance. Second we keep reading until the socket is closed by redis after it receives the quit command.

Async

The sync examples above are good as introduction and can be also used in production. However to don't scale, this is specially problematic on backends. Fourtunately in C++20 it became trivial to convert the sync into asyn code. The example below shows an example.

net::awaitable<void> async_example1()
{
   auto ex = co_await this_coro::executor;

   tcp::resolver resv(ex);
   auto const r = resv.resolve("127.0.0.1", "6379");

   tcp_socket socket {ex};
   co_await async_connect(socket, r);

   std::map<std::string, std::string> map
   { {{"Name"},      {"Marcelo"}} 
   , {{"Education"}, {"Physics"}}
   , {{"Job"},       {"Programmer"}}
   };

   resp::pipeline p;
   p.hset("map", map);
   p.hincrby("map", "Age", 40);
   p.hmget("map", {"Name", "Education", "Job"});
   p.quit();

   co_await async_write(socket, buffer(p.payload));

   resp::buffer buffer;
   for (;;) {
      resp::response res;
      co_await resp::async_read(socket, buffer, res);
      resp::print(res.res);
   }
}

Though short the example above ilustrates many important points

  • STL containers are suported when appropriate.
  • Commands are sent to redis in pipeline to improve performance.
Description
Mirrored via gitea-mirror
Readme 6.7 MiB
Languages
C++ 96%
Python 1.4%
CMake 1.2%
Java 0.3%
Rust 0.3%
Other 0.7%