From d1fc9c7cc61111958a30a9e366a7d6acea1306c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E?= Date: Fri, 19 Apr 2019 04:03:09 +0800 Subject: [PATCH] Add mult exec example (#31) --- examples/CMakeLists.txt | 1 + examples/cpp_redis_multi_exec.cpp | 35 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 examples/cpp_redis_multi_exec.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4ffa6de1..d4ddd507 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -47,6 +47,7 @@ set(EXAMPLES cpp_redis_client cpp_redis_kill cpp_redis_streams_client cpp_redis_high_availability_client + cpp_redis_multi_exec ) foreach(EXAMPLE IN ITEMS ${EXAMPLES}) diff --git a/examples/cpp_redis_multi_exec.cpp b/examples/cpp_redis_multi_exec.cpp new file mode 100644 index 00000000..e47fdc17 --- /dev/null +++ b/examples/cpp_redis_multi_exec.cpp @@ -0,0 +1,35 @@ +// The MIT License (MIT) + +#include "winsock_initializer.h" +#include +#include + +int +main(void) { + winsock_initializer winsock_init; + cpp_redis::client client; + + client.connect("127.0.0.1", 6379, + [](const std::string& host, std::size_t port, cpp_redis::connect_state status) { + if (status == cpp_redis::connect_state::dropped) { + std::cout << "client disconnected from " << host << ":" << port << std::endl; + } + }); + client.multi([](cpp_redis::reply& reply) { + std::cout << "transaction start:" << reply << std::endl; + }); + client.set("hello", "42", [](cpp_redis::reply& reply) { + std::cout << "set hello 42: " << reply << std::endl; + }); + client.decrby("hello", 12, [](cpp_redis::reply& reply) { + std::cout << "decrby hello 12: " << reply << std::endl; + }); + // Previous two commands will be queued until exec called + client.exec([](cpp_redis::reply& reply) { + std::cout << "transaction end:" << reply << std::endl; + }); + + client.sync_commit(); + + return 0; +}