-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_rw.cc
55 lines (49 loc) · 1.18 KB
/
redis_rw.cc
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
/*
* Copyright (c) 2017-2018 SYZ
*
* https://github.com/redis/hiredis.git
*
* g++ redis_rw.cc -std=c++11 -I$HIREDIS_HOME/include -L$HIREDIS_HOME/lib
* -lhiredis -o release/redis_rw
*
* ./redis_rw localhost key value
* ./redis_rw localhost key
*/
#include <hiredis/hiredis.h>
#include <stdlib.h>
#include <iostream>
int main(int argc, char* argv[]) {
if (argc < 3) {
printf("usage: redis_rw localhost key value\n");
}
const char* host = argv[1];
const char* key = argv[2];
const char* value = argv[3];
int32_t port = 6379;
struct timeval timeout = {1, 500000};
redisContext* prc_ = nullptr;
do {
std::string cmd;
redisReply* reply = nullptr;
if (argc > 3) {
cmd = "SET ";
cmd += key;
cmd += " ";
cmd += value;
} else {
cmd = "GET ";
cmd += key;
}
prc_ = redisConnectWithTimeout(host, port, timeout);
if (prc_ == nullptr) break;
if (prc_->err) {
std::cout << prc_->errstr << std::endl;
break;
}
reply = static_cast<redisReply*>(redisCommand(prc_, cmd.c_str()));
std::cout << reply->str << std::endl;
freeReplyObject(reply);
} while (0);
redisFree(prc_);
return 0;
}