-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_hash_client.hpp
More file actions
297 lines (234 loc) · 8.3 KB
/
cpp_hash_client.hpp
File metadata and controls
297 lines (234 loc) · 8.3 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef cpp_hash_client_h__
#define cpp_hash_client_h__
#include "client_interface.hpp"
namespace cpp_redis
{
class hash_client :public client {
public:
hash_client() = default;
virtual ~hash_client() {
}
//当 HSET 命令在哈希表中新创建 field 域并成功为它设置值时, 命令返回 1
//如果域 field 已经存在于哈希表, 并且 HSET 命令成功使用新值覆盖了它的旧值, 那么命令返回 0
virtual int hash_set(std::string&& key, std::string&& field, std::string&& value)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_set),std::forward<std::string>(key),
std::forward<std::string>(field), std::forward<std::string>(value));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_){
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty())?results[0]:0);
}
//和string数据结构一样,可以实现分布式锁
//HSETNX 命令在设置成功时返回 1 , 在给定域已经存在而放弃执行设置操作时返回 0.
//如果给定域已经存在于哈希表当中, 那么命令将放弃执行设置操作。
virtual int hash_setx(std::string&& key, std::string&& field, std::string&& value)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_setx), std::forward<std::string>(key),
std::forward<std::string>(field), std::forward<std::string>(value));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual int hash_exists(std::string&& key, std::string&& field)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_exists), std::forward<std::string>(key),
std::forward<std::string>(field));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual std::string hash_get(std::string&& key, std::string&& field)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_get),std::forward<std::string>(key), std::forward<std::string>(field));
if (!socket_->send_msg(std::move(msg))) {
return "";
}
const auto res = socket_->get_responese();
if (res->get_result_code()!= status::results_ &&
res->get_result_code() != status::status_){
return "";
}
const auto results = res->get_results();
if (results.empty()){
return "";
}
return ((results[0] == g_nil) ? "" : std::move(results[0]));
}
virtual int hash_del(std::vector<std::string>&& fields)
{
check_args();
std::string msg = request_->req_n_keys(request_->get_cmd(redis_cmd::hash_del), std::forward<std::vector<std::string>>(fields));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_){
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual int hash_len(std::string&& key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_len),std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual int hash_strlen(std::string&& key, std::string&& field)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_strlen), std::forward<std::string>(key),
std::forward<std::string>(field));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
//返回增加值
//一个新的哈希表被创建并执行HINCRBY 命令(注意地方)
virtual int hash_incrby(std::string&& key, std::string&& field, std::string&& increment)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_incrby), std::forward<std::string>(key),
std::forward<std::string>(field),std::forward<std::string>(increment));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual std::string hash_incrby_float(std::string&& key, std::string&& field, std::string&& increment)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_incrby_float), std::forward<std::string>(key),
std::forward<std::string>(field), std::forward<std::string>(increment));
if (!socket_->send_msg(std::move(msg))) {
return "";
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_ &&
res->get_result_code() != status::status_) {
return"";
}
const auto results = res->get_results();
return ((!results.empty()) ? std::move(results[0]) : "");
}
virtual bool hash_mset(std::vector<std::string>&& keys)
{
std::string msg = request_->req_n_keys(request_->get_cmd(redis_cmd::hash_mset),
std::forward<std::vector<std::string>>(keys));
if (!socket_->send_msg(std::move(msg))) {
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::status_){
return false;
}
return true;
}
virtual std::vector <std::string> hash_mget(std::vector<std::string>&& keys)
{
std::string msg = request_->req_n_keys(request_->get_cmd(redis_cmd::hash_mget), std::forward<std::vector<std::string>>(keys));
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_ &&
res->get_result_code() != status::status_){
return {} ;
}
return std::move(res->get_results());
}
//返回所有的keys
virtual std::vector <std::string> hash_keys(std::string&& key)
{
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_keys), std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_ &&
res->get_result_code() != status::status_){
return {} ;
}
return std::move(res->get_results());
}
//返回key中的所有值
virtual std::vector <std::string> hash_vals(std::string&& key)
{
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_vals),
std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_ &&
res->get_result_code() != status::status_){
return {} ;
}
return std::move(res->get_results());
}
//返回key中的域和值
virtual std::vector <std::string> hash_get_all(std::string&& key)
{
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::hash_get_all),
std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_ &&
res->get_result_code() != status::status_){
return {} ;
}
return std::move(res->get_results());
}
};
}
#endif // cpp_hash_client_h__