forked from Cylix/cpp_redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis_client.cpp
More file actions
48 lines (40 loc) · 1.36 KB
/
Copy pathredis_client.cpp
File metadata and controls
48 lines (40 loc) · 1.36 KB
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
#include <cpp_redis/cpp_redis>
#include <signal.h>
#include <iostream>
volatile std::atomic_bool should_exit(false);
cpp_redis::redis_client client;
void
sigint_handler(int) {
std::cout << "disconnected (sigint handler)" << std::endl;
client.disconnect();
should_exit = true;
}
int
main(void) {
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);
client.connect("127.0.0.1", 6379, [] (cpp_redis::redis_client&) {
std::cout << "client disconnected (disconnection handler)" << std::endl;
should_exit = true;
});
// same as client.send({ "SET", "hello", "42" }, ...)
client.set("hello", "42", [] (cpp_redis::reply& reply) {
std::cout << reply.as_string() << std::endl;
});
// same as client.send({ "DECRBY", "hello", 12 }, ...)
client.decrby("hello", 12, [] (cpp_redis::reply& reply) {
std::cout << reply.as_integer() << std::endl;
});
// same as client.send({ "GET", "hello" }, ...)
client.get("hello", [] (cpp_redis::reply& reply) {
std::cout << reply.as_string() << std::endl;
});
// commands are pipelined and only sent when client.commit() is called
client.commit();
// synchronous commit, no timeout
// client.sync_commit();
// synchronous commit, timeout
// client.sync_commit(std::chrono::milliseconds(100));
signal(SIGINT, &sigint_handler);
while (!should_exit);
return 0;
}