-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
268 lines (221 loc) · 6.73 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// Created by qicosmos on 2020/9/9.
//
#include <iostream>
#include <set>
#include <regex>
#include "asio_redis_client.h"
using namespace purecpp;
boost::asio::io_service ios;
boost::asio::io_service::work work(ios);
std::thread thd([]{
ios.run();
});
static const std::string host_name = "127.0.0.1";
std::shared_ptr<asio_redis_client> create_client(){
auto client = std::make_shared<asio_redis_client>(ios);
client->enable_auto_reconnect(true);
bool r = client->connect(host_name, 6379);
assert(r);
return client;
}
void async_connect(){
auto client = std::make_shared<asio_redis_client>(ios);
client->enable_auto_reconnect(true);
client->async_connect(host_name, 6379, [client](RedisValue value){
if(value.isOk()){
client->set("hello", "world", [](RedisValue value) {
std::cout << "set: " << value.toString() << '\n';
});
client->get("hello", [](RedisValue value) {
std::cout << "get: " << value.toString() << '\n';
});
}
});
while(true){
std::string str;
std::cin >> str;
}
}
void test_retry(){
auto client = create_client();
client->command("get", {"hello"}, 2/*retry_times*/,[](RedisValue value) {
std::cout << "get result: " << value.toString() << '\n';
});
client->command("set", {"hello", "world"}, 2/*retry_times*/,[](RedisValue value) {
std::cout << "set: " << value.toString() << '\n';
});
std::string str;
std::cin >> str;
}
void get_set() {
auto client = create_client();
client->auth("123456", [](RedisValue value) {
if(value.isError()){
std::cout<<"redis error:"<<value.toString()<<'\n';
}
std::cout << "auth: " << value.toString() << '\n';
});
client->set("hello", "world", [](RedisValue value) {
std::cout << "set: " << value.toString() << '\n';
});
client->get("hello", [](RedisValue value) {
std::cout << "get: " << value.toString() << '\n';
});
client->command("info", {"stats"}, [](RedisValue value) {
std::cout << "info stats: " << value.toString() << '\n';
});
client->command("get", {"hello"}, [](RedisValue value) {
std::cout << "get result: " << value.toString() << '\n';
});
client->command( "get", {"hello"}, 2,[](RedisValue value) {
std::cout << "get: " << value.toString() << '\n';
});
client->del("hello", [](RedisValue value) {
std::cout << "del: " << value.inspect() << '\n';
});
std::string str;
std::cin >> str;
}
bool stop = false;
void create_publisher() {
std::thread pub_thd([] {
auto publisher = create_client();
for (int i = 0; i < 3000; i++) {
if (stop) {
break;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
publisher->publish("mychannel", "hello world", [](RedisValue value) {
std::cout << "publish mychannel ok, number:" << value.inspect() << '\n';
});
}
});
pub_thd.detach();
}
void pub_sub() {
create_publisher();
auto client = create_client();
client->subscribe("mychannel", [=](RedisValue value) {
std::cout << "subscribe mychannel: " << value.toString() << '\n';
// client->unsubscribe("mychannel", [](RedisValue value) {
// std::cout << "unsubscribe mychannel: " << value.toString() << '\n';
// });
});
std::string str;
std::cin >> str;
client->unsubscribe("mychannel", [](RedisValue value) {
std::cout << "unsubscribe mychannel: " << value.toString() << '\n';
});
stop = true;
}
void reconnect() {
auto client = std::make_shared<asio_redis_client>(ios);
client->enable_auto_reconnect(true);
client->connect(host_name, 6379);
std::string str;
std::cin >> str;
client->ping([](RedisValue value) {
std::cout << "ping: " << value.toString() << '\n';
});
}
void reconnect_withtimes() {
auto client = std::make_shared<asio_redis_client>(ios);
client->enable_auto_reconnect(true);
client->connect_with_trytimes(host_name, 6379, 30);
std::string str;
std::cin >> str;
client->ping([](RedisValue value) {
std::cout << "ping: " << value.toString() << '\n';
});
}
void callback_hell() {
auto client = create_client();
client->auth("123456", [=](RedisValue value) {
std::cout << "auth: " << value.toString() << '\n';
client->set("hello", "world", [=](RedisValue value) {
std::cout << "set: " << value.toString() << '\n';
client->get("hello", [=](RedisValue value) {
std::cout << "get: " << value.toString() << '\n';
client->del("hello", [=](RedisValue value) {
std::cout << "del: " << value.inspect() << '\n';
});
});
});
});
}
#ifdef USE_FUTURE
void future(){
auto client = create_client();
auto auth_future = client->auth("123456");
auto set_future = client->set("hello", "world");
auto get_future = client->get("hello");
auto del_future = client->del("hello");
std::cout<<"future------------\n";
std::cout<<"auth result:"<<auth_future.Get().toString()<<"\n";
std::cout<<"set result:"<<set_future.Get().toString()<<'\n';
std::cout<<"get result:"<<get_future.Get().toString()<<'\n';
std::cout<<"del result: ok"<<del_future.Get().toString()<<'\n';
client->close();
}
void future_then(){
auto client = create_client();
auto future = client->auth("123456").Then([&](RedisValue value){
std::cout<<"auth result:"<<value.toString()<<'\n';
return client->set("hello", "world").Get();
}).Then([&](RedisValue value){
std::cout<<"set result:"<<value.toString()<<'\n';
return client->get("hello").Get();
}).Then([&](RedisValue value){
std::cout<<"get result:"<<value.toString()<<'\n';
return client->del("hello").Get();
}).Then([](RedisValue value){
std::cout<<value.toString();
return "del result: ok";
});
std::cout<<"future then---------------\n";
auto result = future.Get();
std::cout<<result<<'\n';
}
void future_then_finally(){
auto client = create_client();
client->auth("123456").Then([=](RedisValue value){
std::cout<<"auth result:"<<value.toString()<<'\n';
return client->set("hello", "world").Get();
}).Then([=](RedisValue value){
std::cout<<"set result:"<<value.toString()<<'\n';
return client->get("hello").Get();
}).Then([=](RedisValue value){
std::cout<<"get result:"<<value.toString()<<'\n';
return client->del("hello").Get();
}).Then([](RedisValue value){
std::cout<<value.toString();
return "del result: ok";
}).Finally([](std::string result){
std::cout<<result<<'\n';
});
std::cout<<"future then finally---------------\n";
}
#endif
void test_future() {
#ifdef USE_FUTURE
future();
future_then();
future_then_finally();
#endif
}
int main() {
// async_connect();
// reconnect();
// reconnect_withtimes();
test_retry();
get_set();
pub_sub();
callback_hell();
test_future();
std::string str;
std::cin >> str;
ios.stop();
thd.join();
return 0;
}