Skip to content

Commit b6ae71e

Browse files
committed
fix
1 parent d140814 commit b6ae71e

File tree

3 files changed

+173
-156
lines changed

3 files changed

+173
-156
lines changed

include/cppredis/client.hpp

Lines changed: 94 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ namespace cpp_redis {
276276
return client_->decr(std::forward<std::string>(key));
277277
}
278278

279-
virtual int decr_increment(std::string&& key,int increment)
279+
int decr_increment(std::string&& key,int increment)
280280
{
281281
static_assert(is_sting_, "This API Support String Request");
282282
if (client_ == nullptr || key.empty()) {
@@ -367,52 +367,77 @@ namespace cpp_redis {
367367
std::forward<std::string>(append_value));
368368
}
369369

370-
std::tuple<bool, int> list_rpush(std::string&& key, std::string&& value)
370+
template<typename T>
371+
int list_rpush(std::string&& key, T&& value)
371372
{
372373
static_assert(is_list_, "This API Support List Request");
373374

374-
if (client_ == nullptr ||
375-
key.empty() || value.empty()) {
376-
return { false,-1 };
375+
if (client_ == nullptr || key.empty()) {
376+
return 0;
377+
}
378+
379+
any_type_to_string(value);
380+
381+
if (keys_.empty()){
382+
return 0;
377383
}
378384

379-
return client_->list_rpush(std::forward<std::string>(key), std::forward<std::string>(value));
385+
return client_->list_rpush(std::forward<std::string>(key),std::move(keys_[0]));
380386
}
381387

382-
std::tuple<bool, int> list_rpush_if(std::string&& key, std::string&& value)
388+
template<typename T>
389+
int list_rpush_if(std::string&& key,T&& value)
383390
{
384391
static_assert(is_list_, "This API Support List Request");
385392

386-
if (client_ == nullptr ||
387-
key.empty() || value.empty) {
388-
return { false,-1 };
393+
if (client_ == nullptr || key.empty()) {
394+
return 0;
389395
}
390396

391-
return client_->list_rpush_if(std::forward<std::string>(key), std::forward<std::string>(value));
397+
any_type_to_string(value);
398+
399+
if (keys_.empty()) {
400+
return 0;
401+
}
402+
403+
return client_->list_rpush_if(std::forward<std::string>(key),std::move(keys_[0]));
392404
}
393405

394-
std::tuple<bool, int> list_lpush(std::string&& key, std::string&& value)
406+
template<typename T>
407+
int list_lpush(std::string&& key,T&& value)
395408
{
396409
static_assert(is_list_, "This API Support List Request");
397410

398-
if (client_ == nullptr ||
399-
key.empty() || value.empty()) {
400-
return { false,-1 };
411+
if (client_ == nullptr || key.empty()) {
412+
return 0;
401413
}
402414

403-
return client_->list_lpush(std::forward<std::string>(key), std::forward<std::string>(value));
415+
any_type_to_string(value);
416+
417+
if (keys_.empty()) {
418+
return 0;
419+
}
420+
421+
return client_->list_lpush(std::forward<std::string>(key), std::move(keys_[0]));
404422
}
405423

406-
std::tuple<bool, int> list_lpush_if(std::string&& key, std::string&& value)
424+
template<typename T>
425+
int list_lpush_if(std::string&& key,T&& value)
407426
{
408427
static_assert(is_list_, "This API Support List Request");
409428

410-
if (client_ == nullptr ||
411-
key.empty() || value.empty()) {
412-
return { false,-1 };
429+
if (client_ == nullptr || key.empty()) {
430+
return 0;
431+
}
432+
433+
any_type_to_string(value);
434+
435+
if (keys_.empty()) {
436+
return 0;
413437
}
414438

415-
return client_->list_lpush_if(std::forward<std::string>(key), std::forward<std::string>(value));
439+
440+
return client_->list_lpush_if(std::forward<std::string>(key),std::move(keys_[0]));
416441
}
417442

418443
int32_t list_size(std::string&& key)
@@ -503,33 +528,45 @@ namespace cpp_redis {
503528
return client_->list_index(std::forward<std::string>(key), index);
504529
}
505530

506-
bool list_set(std::string&& key, std::string&& value, int index)
531+
template<typename T>
532+
bool list_set(std::string&& key, T&& value, int index)
507533
{
508534
static_assert(is_list_, "This API Support List Request");
509535

510536
if (client_ == nullptr || key.empty()) {
511537
return false;
512538
}
513539

540+
any_type_to_string(value);
541+
if (keys_.empty()){
542+
return false;
543+
}
544+
514545
return client_->list_set(std::forward<std::string>(key),
515-
std::forward<std::string>(value),unit::int_to_string(index));
546+
std::move(keys_[0]),unit::int_to_string(index));
516547
}
517548

518549

519550
//没有移除就为0 ,有移除就大于0,count表示list的起始位置,一般从0开始删除,-1表示最后一个元素删除
520551
//如果从0开始删除,有多少删除多少
521552
//如果从-1开始删除,就只会删除一个元素
522553
//数量为>=|count|
523-
std::tuple<bool, int> list_del_elem(std::string&& key, std::string&& value, int count = 0)
554+
template<typename T>
555+
int list_del_elem(std::string&& key,T&& value, int count = 0)
524556
{
525557
static_assert(is_list_, "This API Support List Request");
526558

527559
if (client_ == nullptr || key.empty()) {
528-
return { false,-1 };
560+
return 0;
561+
}
562+
563+
any_type_to_string(value);
564+
if (keys_.empty()) {
565+
return 0;
529566
}
530567

531-
return client_->list_del_elem(std::forward<std::string>(key),
532-
std::forward<std::string>(value),unit::int_to_string(count));
568+
return client_->list_del_elem(std::forward<std::string>(key),std::move(keys_[0]),
569+
unit::int_to_string(count));
533570
}
534571

535572
std::string list_rpoplpush(std::string&& src_key, std::string&& dst_key)
@@ -556,28 +593,43 @@ namespace cpp_redis {
556593
return client_->list_brpoplpush(std::forward<std::string>(src_key), std::forward<std::string>(dst_key), timeout);
557594
}
558595

559-
int list_insert_before(std::string&& key, std::string&& dst_value, std::string&& insert_value)
596+
template<typename T1, typename T2>
597+
int list_insert_before(std::string&& key,T1&& dst_value, T2&& insert_value)
560598
{
561599
static_assert(is_list_, "This API Support List Request");
562600

563601
if (client_ == nullptr || key.empty()) {
564-
return -1;
602+
return 0;
603+
}
604+
605+
606+
any_type_to_string(dst_value);
607+
any_type_to_string(insert_value);
608+
609+
if (keys_.empty() || keys_.size() < 2) {
610+
return 0;
565611
}
566612

567-
return client_->list_insert_before(std::forward<std::string>(key),
568-
std::forward<std::string>(dst_value), std::forward<std::string>(insert_value));
613+
return client_->list_insert_before(std::forward<std::string>(key), std::move(keys_[0]), std::move(keys_[1]));
569614
}
570615

571-
int list_insert_after(std::string&& key, std::string&& dst_value, std::string&& insert_value)
616+
template<typename T1,typename T2>
617+
int list_insert_after(std::string&& key,T1&& dst_value,T2&& insert_value)
572618
{
573619
static_assert(is_list_, "This API Support List Request");
574620

575621
if (client_== nullptr || key.empty()) {
576-
return -1;
622+
return 0;
623+
}
624+
625+
any_type_to_string(dst_value);
626+
any_type_to_string(insert_value);
627+
628+
if (keys_.empty() || keys_.size()<2){
629+
return 0;
577630
}
578631

579-
return client_->list_insert_after(std::forward<std::string>(key),
580-
std::forward<std::string>(dst_value), std::forward<std::string>(insert_value));
632+
return client_->list_insert_after(std::forward<std::string>(key),std::move(keys_[0]),std::move(keys_[1]));
581633
}
582634

583635
template<typename...Args>
@@ -1121,7 +1173,7 @@ namespace cpp_redis {
11211173
}
11221174

11231175
keys_.push_back(std::forward<std::string>(key));
1124-
hash_make_keys(std::forward<Args>(fields)...);
1176+
any_type_to_string(std::forward<Args>(fields)...);
11251177
if ( keys_.empty() || keys_.size() != Size){
11261178
return -1;
11271179
}
@@ -1199,7 +1251,7 @@ namespace cpp_redis {
11991251
}
12001252

12011253
keys_.push_back(std::forward<std::string>(key));
1202-
hash_make_keys(std::forward<Args>(keys)...);
1254+
any_type_to_string(std::forward<Args>(keys)...);
12031255

12041256
if (keys_.size() != Size) {
12051257
return false;
@@ -1218,7 +1270,7 @@ namespace cpp_redis {
12181270
}
12191271

12201272
keys_.push_back(std::forward<std::string>(key));
1221-
hash_make_keys(std::forward<Args>(keys)...);
1273+
any_type_to_string(std::forward<Args>(keys)...);
12221274

12231275
if (keys_.size() != Size) {
12241276
return { {} };
@@ -1260,25 +1312,25 @@ namespace cpp_redis {
12601312
return client_->hash_get_all(std::forward<std::string>(key));
12611313
}
12621314
private:
1263-
void hash_make_keys()
1315+
void any_type_to_string()
12641316
{
12651317

12661318
}
12671319

12681320
template<typename T, typename...Args>
1269-
void hash_make_keys(T&& header, Args&&...args)
1321+
void any_type_to_string(T&& header, Args&&...args)
12701322
{
12711323
std::string value;
12721324
#if (_MSC_VER >= 1700 && _MSC_VER <= 1900) //vs2012-vs2015
12731325
#else
1274-
if constexpr(std::is_same<T,bool>::value){
1326+
if constexpr(std::is_same<typename std::decay<decltype(header)>::type, bool>::value){
12751327
value = header ? "true" : "false";
12761328
}else{
12771329
value = to_string(header);
12781330
}
12791331
#endif
12801332
keys_.push_back(std::move(value));
1281-
hash_make_keys(std::forward<Args>(args)...);
1333+
any_type_to_string(std::forward<Args>(args)...);
12821334

12831335
}
12841336

include/cppredis/client_interface.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -257,29 +257,29 @@ namespace cpp_redis {
257257
return -1;
258258
}
259259

260-
virtual std::tuple<bool, int> list_rpush(std::string&& key, std::string&& value)
260+
virtual int list_rpush(std::string&& key, std::string&& value)
261261
{
262-
return { false,-1 };
262+
return -1 ;
263263
}
264264

265-
virtual std::tuple<bool, int> list_rpush_if(std::string&& key, std::string&& value)
265+
virtual int list_rpush_if(std::string&& key, std::string&& value)
266266
{
267-
return { false,-1 };
267+
return -1;
268268
}
269269

270-
virtual std::tuple<bool, int> list_lpush(std::string&& key, std::string&& value)
270+
virtual int list_lpush(std::string&& key, std::string&& value)
271271
{
272-
return { false,-1 };
272+
return -1;
273273
}
274274

275-
virtual std::tuple<bool, int> list_lpush_if(std::string&& key, std::string&& value)
275+
virtual int list_lpush_if(std::string&& key, std::string&& value)
276276
{
277-
return { false,-1 };
277+
return -1;
278278
}
279279

280280
virtual int32_t list_size(std::string&& key)
281281
{
282-
return INT32_MAX;
282+
return -1;
283283
}
284284

285285
virtual RESULTS_TYPE list_range(std::string&& key, int start, int end)
@@ -322,9 +322,9 @@ namespace cpp_redis {
322322
return "";
323323
}
324324

325-
virtual std::tuple<bool, int> list_del_elem(std::string&& key, std::string&& value,std::string&&count)
325+
virtual int list_del_elem(std::string&& key, std::string&& value,std::string&&count)
326326
{
327-
return { false,-1 };
327+
return 0;
328328
}
329329

330330
virtual std::string list_rpoplpush(std::string&& src_key, std::string&& dst_key)

0 commit comments

Comments
 (0)