|
9 | 9 | // copies of the Software, and to permit persons to whom the Software is |
10 | 10 | // furnished to do so, subject to the following conditions: |
11 | 11 | // |
12 | | -// The above copyright notice and this permission notice shall be included in all |
13 | | -// copies or substantial portions of the Software. |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
14 | 14 | // |
15 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 | 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 | 21 | // SOFTWARE. |
22 | | -#include <string> |
23 | 22 | #include <cpp_redis/cpp_redis> |
24 | 23 | #include <cpp_redis/misc/macro.hpp> |
| 24 | +#include <string> |
25 | 25 |
|
26 | 26 | #define ENABLE_SESSION = 1 |
27 | 27 |
|
28 | 28 | #ifdef _WIN32 |
29 | 29 | #include <Winsock2.h> |
30 | | -#endif /* _WIN32 */ |
| 30 | +#endif //! _WIN32 |
31 | 31 |
|
32 | 32 | int |
33 | 33 | main(void) { |
34 | 34 | #ifdef _WIN32 |
35 | | - //! Windows netword DLL init |
36 | | - WORD version = MAKEWORD(2, 2); |
37 | | - WSADATA data; |
38 | | - |
39 | | - if (WSAStartup(version, &data) != 0) { |
40 | | - std::cerr << "WSAStartup() failure" << std::endl; |
41 | | - return -1; |
42 | | - } |
43 | | -#endif /* _WIN32 */ |
44 | | - |
45 | | - //! Enable logging |
46 | | - cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger); |
47 | | - |
48 | | - cpp_redis::client client; |
49 | | - |
50 | | - client.connect("127.0.0.1", 6379, |
51 | | - [](const std::string &host, std::size_t port, cpp_redis::connect_state status) { |
52 | | - if (status == cpp_redis::connect_state::dropped) { |
53 | | - std::cout << "client disconnected from " << host << ":" << port << std::endl; |
54 | | - } |
55 | | - }); |
56 | | - |
57 | | - auto replcmd = [](const cpp_redis::reply &reply) { |
58 | | - std::cout << "set hello 42: " << reply << std::endl; |
59 | | - // if (reply.is_string()) |
60 | | - // do_something_with_string(reply.as_string()) |
61 | | - }; |
62 | | - |
63 | | - const std::string group_name = "groupone"; |
64 | | - const std::string session_name = "sessone"; |
65 | | - const std::string consumer_name = "ABCD"; |
66 | | - |
67 | | - std::multimap<std::string, std::string> ins; |
68 | | - ins.insert(std::pair<std::string, std::string>{"message", "hello"}); |
| 35 | + //! Windows netword DLL init |
| 36 | + WORD version = MAKEWORD(2, 2); |
| 37 | + WSADATA data; |
| 38 | + |
| 39 | + if (WSAStartup(version, &data) != 0) { |
| 40 | + std::cerr << "WSAStartup() failure" << std::endl; |
| 41 | + return -1; |
| 42 | + } |
| 43 | +#endif //! _WIN32 |
| 44 | + |
| 45 | + //! Enable logging |
| 46 | + cpp_redis::active_logger = |
| 47 | + std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger); |
| 48 | + |
| 49 | + cpp_redis::client client; |
| 50 | + |
| 51 | + client.connect("127.0.0.1", |
| 52 | + 6379, |
| 53 | + [](const std::string &host, |
| 54 | + std::size_t port, |
| 55 | + cpp_redis::connect_state status) { |
| 56 | + if (status == cpp_redis::connect_state::dropped) { |
| 57 | + std::cout << "client disconnected from " << host << ":" |
| 58 | + << port << std::endl; |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + auto replcmd = [](const cpp_redis::reply_t &reply) { |
| 63 | + std::cout << "set hello 42: " << reply << std::endl; |
| 64 | + // if (reply.is_string()) |
| 65 | + // do_something_with_string(reply.as_string()) |
| 66 | + }; |
| 67 | + |
| 68 | + const std::string group_name = "groupone"; |
| 69 | + const std::string session_name = "sessone"; |
| 70 | + const std::string consumer_name = "ABCD"; |
| 71 | + |
| 72 | + std::multimap<std::string, std::string> ins; |
| 73 | + ins.insert(std::pair<std::string, std::string>{"message", "hello"}); |
69 | 74 |
|
70 | 75 | #ifdef ENABLE_SESSION |
71 | 76 |
|
72 | | - client.xadd(session_name, "*", ins, replcmd); |
73 | | - client.xgroup_create(session_name, group_name, "0", replcmd); |
74 | | - |
75 | | - client.sync_commit(); |
76 | | - |
77 | | - client.xrange(session_name, {"-", "+", 10}, replcmd); |
78 | | - |
79 | | - client.xreadgroup({group_name, |
80 | | - consumer_name, |
81 | | - {{session_name}, {">"}}, |
82 | | - 1, // Count |
83 | | - 0, // block milli |
84 | | - false, // no ack |
85 | | - }, [](cpp_redis::reply &reply) { |
86 | | - std::cout << "set hello 42: " << reply << std::endl; |
87 | | - auto msg = reply.as_array(); |
88 | | - std::cout << "Mes: " << msg[0] << std::endl; |
89 | | - // if (reply.is_string()) |
90 | | - // do_something_with_string(reply.as_string()) |
91 | | - }); |
| 77 | + client.xadd(session_name, "*", ins, replcmd); |
| 78 | + client.xgroup_create(session_name, group_name, "0", replcmd); |
| 79 | + |
| 80 | + client.sync_commit(); |
| 81 | + |
| 82 | + client.xrange(session_name, {"-", "+", 10}, replcmd); |
| 83 | + |
| 84 | + client.xreadgroup( |
| 85 | + { |
| 86 | + group_name, |
| 87 | + consumer_name, |
| 88 | + {{session_name}, {">"}}, |
| 89 | + 1, // Count |
| 90 | + 0, // block milli |
| 91 | + false, // no ack |
| 92 | + }, |
| 93 | + [](cpp_redis::reply_t &reply) { |
| 94 | + std::cout << "set hello 42: " << reply << std::endl; |
| 95 | + auto msg = reply.as_array(); |
| 96 | + std::cout << "Mes: " << msg[0] << std::endl; |
| 97 | + // if (reply.is_string()) |
| 98 | + // do_something_with_string(reply.as_string()) |
| 99 | + }); |
92 | 100 |
|
93 | 101 | #else |
94 | 102 |
|
95 | | - // same as client.send({ "SET", "hello", "42" }, ...) |
96 | | - client.set("hello", "42", [](cpp_redis::reply &reply) { |
97 | | - std::cout << "set hello 42: " << reply << std::endl; |
98 | | - // if (reply.is_string()) |
99 | | - // do_something_with_string(reply.as_string()) |
100 | | - }); |
101 | | - |
102 | | - // same as client.send({ "DECRBY", "hello", 12 }, ...) |
103 | | - client.decrby("hello", 12, [](cpp_redis::reply &reply) { |
104 | | - std::cout << "decrby hello 12: " << reply << std::endl; |
105 | | - // if (reply.is_integer()) |
106 | | - // do_something_with_integer(reply.as_integer()) |
107 | | - }); |
108 | | - |
109 | | - // same as client.send({ "GET", "hello" }, ...) |
110 | | - client.get("hello", [](cpp_redis::reply &reply) { |
111 | | - std::cout << "get hello: " << reply << std::endl; |
112 | | - // if (reply.is_string()) |
113 | | - // do_something_with_string(reply.as_string()) |
114 | | - }); |
| 103 | + // same as client.send({ "SET", "hello", "42" }, ...) |
| 104 | + client.set("hello", "42", [](cpp_redis::reply_t &reply) { |
| 105 | + std::cout << "set hello 42: " << reply << std::endl; |
| 106 | + // if (reply.is_string()) |
| 107 | + // do_something_with_string(reply.as_string()) |
| 108 | + }); |
| 109 | + |
| 110 | + // same as client.send({ "DECRBY", "hello", 12 }, ...) |
| 111 | + client.decrby("hello", 12, [](cpp_redis::reply_t &reply) { |
| 112 | + std::cout << "decrby hello 12: " << reply << std::endl; |
| 113 | + // if (reply.is_integer()) |
| 114 | + // do_something_with_integer(reply.as_integer()) |
| 115 | + }); |
| 116 | + |
| 117 | + // same as client.send({ "GET", "hello" }, ...) |
| 118 | + client.get("hello", [](cpp_redis::reply_t &reply) { |
| 119 | + std::cout << "get hello: " << reply << std::endl; |
| 120 | + // if (reply.is_string()) |
| 121 | + // do_something_with_string(reply.as_string()) |
| 122 | + }); |
115 | 123 |
|
116 | 124 | #endif |
117 | 125 |
|
118 | | - // commands are pipelined and only sent when client.commit() is called |
119 | | - // client.commit(); |
| 126 | + // commands are pipelined and only sent when client.commit() is called |
| 127 | + // client.commit(); |
120 | 128 |
|
121 | | - // synchronous commit, no timeout |
122 | | - client.sync_commit(); |
| 129 | + // synchronous commit, no timeout |
| 130 | + client.sync_commit(); |
123 | 131 |
|
124 | | - // synchronous commit, timeout |
125 | | - // client.sync_commit(std::chrono::milliseconds(100)); |
| 132 | + // synchronous commit, timeout |
| 133 | + // client.sync_commit(std::chrono::milliseconds(100)); |
126 | 134 |
|
127 | 135 | #ifdef _WIN32 |
128 | | - WSACleanup(); |
129 | | -#endif /* _WIN32 */ |
| 136 | + WSACleanup(); |
| 137 | +#endif //! _WIN32 |
130 | 138 |
|
131 | | - return 0; |
| 139 | + return 0; |
132 | 140 | } |
0 commit comments