-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_interface.hpp
More file actions
851 lines (662 loc) · 17.5 KB
/
client_interface.hpp
File metadata and controls
851 lines (662 loc) · 17.5 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#ifndef client_interface_h__
#define client_interface_h__
#include <assert.h>
#include "cpp_redis_request.hpp"
#include "cpp_redis_net.hpp"
namespace cpp_redis {
class client {
public:
client() {
try
{
socket_ = cpp_redis::traits::make_unique<cpp_redis_net>();
request_ = cpp_redis::traits::make_unique<cpp_redis_request>();
}
catch (const std::exception & ex)
{
std::cout << ex.what() << std::endl;
socket_ = nullptr;
request_ = nullptr;
}
}
virtual ~client() {
close();
}
//从左边弹出元素,元素弹完了,redis直接删除
std::string get_current_error()
{
check_args();
const auto res = socket_->get_responese();
return res->get_error();
}
std::string get_current_status()
{
check_args();
const auto res = socket_->get_responese();
return res->get_status();
}
/********ip,port,password,db_num********************/
template<typename...Args>
constexpr bool connect_to(std::string&& ip, Args&&...args) {
check_args();
return socket_->connect_to(std::move(ip), std::forward<Args>(args)...);
}
bool set_db_num(int&& num)
{
check_args();
if (num < 0) {
return false;
}
return select_db(std::move(num));
}
bool auth(std::string&& password)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::auth), std::forward<std::string>(password));
if (!socket_->send_msg(std::move(msg))){
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() == status::unconnected_ ||
res->get_result_code() == status::errors_) {
return false;
}
return true;
}
bool delete_key(std::string&& key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::del), std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))){
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() == status::errors_) {
return false;
}
const auto int_results = res->get_int_results();
if (int_results.empty()) {
return false;
}
if (int_results[0] == 0) {
return false;
}
return true;
}
bool is_key_exist(std::string&& key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::exists), std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))){
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() == status::errors_) {
return false;
}
const auto int_results = res->get_int_results();
if (int_results.empty()) {
return false;
}
if (int_results[0] == 0) {
return false;
}
return true;
}
//给key加上时间
bool expire(std::string&& key, std::size_t seconds)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::expire), std::forward<std::string>(key), unit::int_to_string(seconds));
if (!socket_->send_msg(std::move(msg))){
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_) {
return false;
}
return true;
}
bool pexpire(std::string&& key, std::size_t milliseconds)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::pexpire),
std::forward<std::string>(key), unit::int_to_string(milliseconds));
if (!socket_->send_msg(std::move(msg))){
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_) {
return false;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? (results[0] ==1):false);
}
//给key加上在什么时间过期
bool expire_at(std::string&& key, std::size_t unix_timestamp)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::expire_at),
std::forward<std::string>(key), unit::int_to_string(unix_timestamp));
if (!socket_->send_msg(std::move(msg))){
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_) {
return false;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? (results[0] == 1) : false);
}
//给key加上在什么时间过期
bool pexpire_at(std::string&& key, std::size_t unix_milli_timestamp)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::pexpire_at),
std::forward<std::string>(key), unit::int_to_string(unix_milli_timestamp));
if (!socket_->send_msg(std::move(msg))){
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_) {
return false;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? (results[0] == 1) : false);
}
//key的剩余时间 -1:表示永久 -2:表示不存在
int remainder_ttl(std::string&& key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::ttl), std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))){
return -2;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_) {
return -2;
}
const auto int_results = res->get_int_results();
if (int_results.empty()) {
return -2;
}
return int_results[0];
}
//key的剩余时间 -1:表示永久 -2:表示不存在
int64_t remainder_pttl(std::string&& key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::pttl),
std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))){
return -2;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_) {
return -2;
}
const auto int_results = res->get_int_results();
if (int_results.empty()) {
return -2;
}
return int_results[0];
}
bool rename_key(std::string&& key,std::string &&new_key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::rename),
std::forward<std::string>(key),std::forward<std::string>(new_key));
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;
}
bool renamenx_key(std::string&& key, std::string&& new_key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::renamenx),
std::forward<std::string>(key), std::forward<std::string>(new_key));
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;
}
bool start_multi()
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::multi));
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;
}
bool exec()
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::exec));
if (!socket_->send_msg(std::move(msg))) {
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() == status::errors_) {
return false;
}
return true;
}
bool discard()
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::discard));
if (!socket_->send_msg(std::move(msg))) {
return false;
}
const auto res = socket_->get_responese();
return true;
}
bool remove_expire(std::string&& key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::remove_expire),std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))) {
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() !=status::int_result_){
return false;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] ==1 :false);
}
virtual bool setex(std::string&& key, std::string&& value,std::string && seconds)
{
return false;
}
virtual bool psetex(std::string&& key, std::string&& value,std::string && milliseconds)
{
return false;
}
virtual bool set(std::string&& key, std::string&& value)
{
return false;
}
virtual int setnx(std::string&& key, std::string&& value)
{
return 0;
}
virtual std::vector<std::string> multi_get_keys(std::vector<std::string>&& keys)
{
return {};
}
virtual bool multi_set_keys(std::vector<std::string>&& keys)
{
return false;
}
virtual int multi_set_if_not_set(std::vector<std::string>&& keys)
{
return false;
}
virtual bool setnx_has_seconds(std::string&& key, std::string&& value,std::string&& seconds)
{
return false;
}
virtual bool setxx(std::string&& key, std::string&& value, std::string&& seconds)
{
return false;
}
virtual bool setnx_has_milliseconds(std::string&& key, std::string&& value,std::string&& milliseconds)
{
return false;
}
virtual bool setxx_has_milliseconds(std::string&& key, std::string&& value, std::string&& milliseconds)
{
return false;
}
virtual std::string get_range(std::string&&key, std::string&& start, std::string&& end)
{
return "";
}
virtual int incr_by_increment(std::string&& key,std::string&& increment)
{
return -1 ;
}
virtual std::string incr_by_float(std::string&& key, std::string&& increment)
{
return "";
}
virtual int incr(std::string&& key)
{
return -1;
}
virtual int decr(std::string&& key)
{
return -1;
}
virtual int decr_increment(std::string&& key,std::string && increment)
{
return -1;
}
virtual std::string get_reflect_value(std::string&& key)
{
return { false,-1 };
}
virtual std::string get_set_key(std::string&& key, std::string&& value)
{
return { false,-1 };
}
virtual std::string substr_reflect_value(std::string&& key, int start, int end)
{
return { false,-1 };
}
//在指定key追加值
virtual int append_value(std::string&& key, std::string&& append_value)
{
return -1;
}
virtual int list_rpush(std::string&& key, std::string&& value)
{
return -1 ;
}
virtual int list_rpush_if(std::string&& key, std::string&& value)
{
return -1;
}
virtual int list_lpush(std::string&& key, std::string&& value)
{
return -1;
}
virtual int list_lpush_if(std::string&& key, std::string&& value)
{
return -1;
}
virtual int32_t list_size(std::string&& key)
{
return -1;
}
virtual std::vector<std::string> list_range(std::string&& key, int start, int end)
{
return {};
}
virtual std::string list_lpop(std::string&& key)
{
return "";
}
virtual std::string list_rpop(std::string&& key)
{
return "";
}
virtual std::string list_brpop(std::string&& key, size_t timeout = 0)
{
return "";
}
virtual std::string list_blpop(std::string&& key, size_t timeout = 0)
{
return "";
}
virtual bool list_trim(std::string&& key, int start, int end)
{
return "";
}
virtual std::string list_index(std::string&& key, int index)
{
return "";
}
virtual bool list_set(std::string&& key, std::string&& value,std::string && index)
{
return "";
}
virtual int list_del_elem(std::string&& key, std::string&& value,std::string&&count)
{
return 0;
}
virtual std::string list_rpoplpush(std::string&& src_key, std::string&& dst_key)
{
return "";
}
virtual std::string list_brpoplpush(std::string&& src_key, std::string&& dst_key, int timeout = 0)
{
return "";
}
virtual int list_insert_before(std::string&& key, std::string&& dst_value, std::string&& insert_value)
{
return INT32_MAX;
}
virtual int list_insert_after(std::string&& key, std::string&& dst_value, std::string&& insert_value)
{
return INT32_MAX;
}
virtual int set_add(std::vector<std::string>&& keys)
{
return 0;
}
virtual int set_delete_elem(std::vector<std::string>&& keys)
{
return 0;
}
virtual bool set_is_member(std::string&& key, std::string&& value)
{
return false;
}
virtual std::string set_rdel_elem(std::string&& key)
{
return "";
}
virtual std::vector <std::string> set_sinter(std::vector<std::string>&& keys)
{
return {};
}
virtual int set_inter_store(std::vector<std::string>&& keys)
{
return 0;
}
virtual std::vector <std::string> set_union(std::vector<std::string>&& keys)
{
return {};
}
virtual int set_union_store(std::vector<std::string>&& keys)
{
return 0;
}
virtual std::vector <std::string> set_rand_elem(std::string&& key, int count)
{
return {};
}
virtual bool set_move_elem(std::string&& src_key, std::string&& dst_key, std::string&& member)
{
return false;
}
virtual std::vector <std::string> set_diff(std::vector<std::string>&& keys)
{
return {};
}
virtual size_t set_get_size(std::string&& key)
{
return 0;
}
virtual std::vector <std::string> set_get_all_member(std::string&& key)
{
return {};
}
virtual int set_diff_store(std::vector<std::string>&& keys)
{
return -1;
}
virtual int zset_add(std::vector<std::string>&& keys)
{
return -1;
}
virtual std::string zset_score(std::string&& key, std::string&& member)
{
return "";
}
virtual std::string zset_incrby(std::string&& key,std::string&& increment,std::string&& member)
{
return "";
}
virtual int zset_card(std::string&& key)
{
return 0;
}
virtual int zset_count(std::string&& key, std::string&& min, std::string&& max)
{
return 0;
}
virtual std::vector <std::string> zset_range(std::string&& key, std::string&& begin, std::string&& end,bool with_scores)
{
return { {} };
}
virtual std::vector <std::string> zset_revrange(std::string&& key, std::string&& begin, std::string&& end, bool with_scores)
{
return { {} };
}
virtual std::vector <std::string> zset_range_score(std::string&& key, std::string&& min, std::string&& max,
bool with_scores, bool limit, std::string&& limit_min, std::string&& limit_max)
{
return { {} };
}
virtual std::vector <std::string> zset_revrange_score(std::string&& key, std::string&& max, std::string&& min,
bool with_scores, bool limit, std::string&& limit_min,std::string&&limit_max)
{
return { {} };
}
virtual int zset_rank(std::string&& key, std::string&& member)
{
return -1;
}
virtual int zset_revrank(std::string&& key, std::string&& member)
{
return -1;
}
virtual bool zset_rem(std::vector<std::string>&& keys)
{
return false;
}
virtual int zset_remrangeby_rank(std::string&& key, std::string&& begin, std::string&& end)
{
return -1;
}
virtual int zset_remrangebyscore(std::string&& key, std::string&& min, std::string&& max)
{
return -1;
}
virtual std::vector <std::string> zset_rangebylex(std::string&& key, std::string&& min,
std::string&& max, bool limit, std::string&& limit_min, std::string&& limit_max)
{
return { {} };
}
virtual int zset_lexcount(std::string&& key, std::string&& min, std::string&& max)
{
return -1;
}
virtual int zset_remrangebylex(std::string&& key, std::string&& min,std::string&& max)
{
return 0;
}
virtual int zset_union_store(std::vector<std::string>&& keys,aggregate_mothod mothod)
{
return -1;
}
virtual int zset_inter_store(std::vector<std::string>&& keys, aggregate_mothod mothod)
{
return -1;
}
virtual int hash_set(std::string&& key, std::string&& field, std::string&& value)
{
return 0;
}
virtual int hash_setx(std::string&& key, std::string&& field, std::string&& value)
{
return 0;
}
virtual int hash_exists(std::string&& key,std::string&& field)
{
return 0;
}
virtual std::string hash_get(std::string&& key, std::string&& field)
{
return "";
}
virtual int hash_del(std::vector<std::string>&& fields)
{
return -1;
}
virtual int hash_len(std::string&& key)
{
return 0;
}
virtual int hash_strlen(std::string&& key, std::string&& field)
{
return 0;
}
virtual int hash_incrby(std::string&& key, std::string && field, std::string&& increment)
{
return 0;
}
virtual std::string hash_incrby_float(std::string&& key, std::string&& field, std::string&& increment)
{
return "";
}
virtual bool hash_mset(std::vector<std::string>&& keys)
{
return false;
}
virtual std::vector <std::string> hash_mget(std::vector<std::string>&& keys)
{
return { {} };
}
virtual std::vector <std::string> hash_keys(std::string&& key)
{
return{ {} };
}
virtual std::vector <std::string> hash_vals(std::string&& key)
{
return{ {} };
}
virtual std::vector <std::string> hash_get_all(std::string&& key)
{
return{ {} };
}
private:
bool select_db(int&& num)
{
check_args();
if (num < 0) {
return false;
}
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::select), std::move(unit::int_to_string(num)));
if (!socket_->send_msg(std::move(msg))){
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() == status::unconnected_ ||
res->get_result_code() == status::errors_) {
return false;
}
return true;
}
void close() {
check_args();
socket_->close();
}
protected:
void check_args()
{
assert(socket_ != nullptr);
assert(request_ != nullptr);
}
std::unique_ptr<cpp_redis_request>request_;
std::unique_ptr<cpp_redis_net>socket_;
};
}
#endif // client_interface_h__