mirror of
https://github.com/boostorg/redis.git
synced 2026-02-09 11:22:22 +00:00
172 lines
3.8 KiB
C++
172 lines
3.8 KiB
C++
/* Copyright (c) 2019 - 2020 Marcelo Zimbres Silva (mzimbres at gmail dot com)
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include "aedis.hpp"
|
|
|
|
namespace net = aedis::net;
|
|
using tcp = net::ip::tcp;
|
|
using tcp_socket = net::use_awaitable_t<>::as_default_on_t<tcp::socket>;
|
|
|
|
namespace this_coro = net::this_coro;
|
|
|
|
using namespace net;
|
|
using namespace aedis;
|
|
|
|
awaitable<void> example1(tcp::resolver::results_type const& r)
|
|
{
|
|
tcp_socket socket {co_await this_coro::executor};
|
|
|
|
co_await async_connect(socket, r);
|
|
|
|
auto cmd = ping();
|
|
co_await async_write(socket, buffer(cmd));
|
|
|
|
resp::buffer buffer;
|
|
resp::response res;
|
|
co_await resp::async_read(socket, buffer, res);
|
|
|
|
resp::print(res.res);
|
|
}
|
|
|
|
awaitable<void> example2(tcp::resolver::results_type const& r)
|
|
{
|
|
tcp_socket socket {co_await this_coro::executor};
|
|
|
|
co_await async_connect(socket, r);
|
|
|
|
auto cmd = multi()
|
|
+ ping()
|
|
+ incr("age")
|
|
+ exec()
|
|
+ quit()
|
|
;
|
|
|
|
co_await async_write(socket, buffer(cmd));
|
|
|
|
resp::buffer buffer;
|
|
for (;;) {
|
|
resp::response res;
|
|
co_await resp::async_read(socket, buffer, res);
|
|
resp::print(res.res);
|
|
}
|
|
}
|
|
|
|
awaitable<void> example3(tcp::resolver::results_type const& r)
|
|
{
|
|
tcp_socket socket {co_await this_coro::executor};
|
|
|
|
co_await async_connect(socket, r);
|
|
|
|
std::list<std::string> a
|
|
{"one" ,"two", "three"};
|
|
|
|
std::set<std::string> b
|
|
{"a" ,"b", "c"};
|
|
|
|
std::map<std::string, std::string> c
|
|
{ {{"Name"}, {"Marcelo"}}
|
|
, {{"Education"}, {"Physics"}}
|
|
, {{"Job"}, {"Programmer"}}
|
|
};
|
|
|
|
std::map<int, std::string> d
|
|
{ {1, {"foo"}}
|
|
, {2, {"bar"}}
|
|
, {3, {"foobar"}}
|
|
};
|
|
|
|
auto cmd = ping()
|
|
+ role()
|
|
+ flushall()
|
|
+ rpush("a", a)
|
|
+ lrange("a")
|
|
+ del("a")
|
|
+ multi()
|
|
+ rpush("b", b)
|
|
+ lrange("b")
|
|
+ del("b")
|
|
+ hset("c", c)
|
|
+ hincrby("c", "Age", 40)
|
|
+ hmget("c", {"Name", "Education", "Job"})
|
|
+ hvals("c")
|
|
+ hlen("c")
|
|
+ hgetall("c")
|
|
+ zadd({"d"}, d)
|
|
+ zrange("d")
|
|
+ zrangebyscore("foo", 2, -1)
|
|
+ set("f", {"39"})
|
|
+ incr("f")
|
|
+ get("f")
|
|
+ expire("f", 10)
|
|
+ publish("g", "A message")
|
|
+ exec()
|
|
+ set("h", {"h"})
|
|
+ append("h", "h")
|
|
+ get("h")
|
|
+ auth("password")
|
|
+ bitcount("h")
|
|
+ quit()
|
|
;
|
|
|
|
co_await async_write(socket, buffer(cmd));
|
|
|
|
resp::buffer buffer;
|
|
for (;;) {
|
|
resp::response res;
|
|
co_await resp::async_read(socket, buffer, res);
|
|
resp::print(res.res);
|
|
}
|
|
}
|
|
|
|
awaitable<void> example4(tcp::resolver::results_type const& r)
|
|
{
|
|
tcp_socket socket {co_await this_coro::executor};
|
|
|
|
co_await async_connect(socket, r);
|
|
|
|
auto cmd = subscribe("channel");
|
|
co_await async_write(socket, buffer(cmd));
|
|
resp::buffer buffer;
|
|
for (;;) {
|
|
resp::response res;
|
|
co_await resp::async_read(socket, buffer, res);
|
|
resp::print(res.res);
|
|
}
|
|
}
|
|
|
|
awaitable<void> example5()
|
|
{
|
|
tcp_socket socket {co_await this_coro::executor};
|
|
|
|
sentinel_op2::config cfg
|
|
{ {"127.0.0.1", "26379"}
|
|
, {"mymaster"}
|
|
, {"master"}
|
|
};
|
|
|
|
instance inst;
|
|
co_await async_get_instance2(socket, cfg, inst);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
io_context ioc {1};
|
|
tcp::resolver res(ioc.get_executor());
|
|
auto const r = res.resolve("127.0.0.1", "6379");
|
|
|
|
co_spawn(ioc, example1(r), detached);
|
|
co_spawn(ioc, example2(r), detached);
|
|
co_spawn(ioc, example3(r), detached);
|
|
co_spawn(ioc, example4(r), detached);
|
|
co_spawn(ioc, example5(), detached);
|
|
|
|
ioc.run();
|
|
}
|
|
|