@@ -527,4 +527,165 @@ string DBConnector::getClientName()
527
527
}
528
528
}
529
529
530
+ int64_t DBConnector::del (const string &key)
531
+ {
532
+ RedisCommand sdel;
533
+ sdel.format (" DEL %s" , key.c_str ());
534
+ RedisReply r (this , sdel, REDIS_REPLY_INTEGER);
535
+ return r.getContext ()->integer ;
536
+ }
537
+
538
+ bool DBConnector::exists (const string &key)
539
+ {
540
+ RedisCommand rexists;
541
+ if (key.find_first_of (" \t " ) != string::npos)
542
+ {
543
+ SWSS_LOG_ERROR (" EXISTS failed, invalid space or tab in single key: %s" , key.c_str ());
544
+ throw runtime_error (" EXISTS failed, invalid space or tab in single key" );
545
+ }
546
+ rexists.format (" EXISTS %s" , key.c_str ());
547
+ RedisReply r (this , rexists, REDIS_REPLY_INTEGER);
548
+ return (r.getContext ()->integer > 0 );
549
+ }
550
+
551
+ int64_t DBConnector::hdel (const string &key, const string &field)
552
+ {
553
+ RedisCommand shdel;
554
+ shdel.format (" HDEL %s %s" , key.c_str (), field.c_str ());
555
+ RedisReply r (this , shdel, REDIS_REPLY_INTEGER);
556
+ return r.getContext ()->integer ;
557
+ }
558
+
559
+ int64_t DBConnector::hdel (const std::string &key, const std::vector<std::string> &fields)
560
+ {
561
+ RedisCommand shdel;
562
+ shdel.formatHDEL (key, fields);
563
+ RedisReply r (this , shdel, REDIS_REPLY_INTEGER);
564
+ return r.getContext ()->integer ;
565
+ }
566
+
567
+ void DBConnector::hset (const string &key, const string &field, const string &value)
568
+ {
569
+ RedisCommand shset;
570
+ shset.format (" HSET %s %s %s" , key.c_str (), field.c_str (), value.c_str ());
571
+ RedisReply r (this , shset, REDIS_REPLY_INTEGER);
572
+ }
573
+
574
+ void DBConnector::set (const string &key, const string &value)
575
+ {
576
+ RedisCommand sset;
577
+ sset.format (" SET %s %s" , key.c_str (), value.c_str ());
578
+ RedisReply r (this , sset, REDIS_REPLY_STATUS);
579
+ }
580
+
581
+ unordered_map<string, string> DBConnector::hgetall (const string &key)
582
+ {
583
+ unordered_map<string, string> map;
584
+ hgetall (key, std::inserter (map, map.end ()));
585
+ return map;
586
+ }
587
+
588
+ vector<string> DBConnector::keys (const string &key)
589
+ {
590
+ RedisCommand skeys;
591
+ skeys.format (" KEYS %s" , key.c_str ());
592
+ RedisReply r (this , skeys, REDIS_REPLY_ARRAY);
593
+
594
+ auto ctx = r.getContext ();
595
+
596
+ vector<string> list;
597
+ for (unsigned int i = 0 ; i < ctx->elements ; i++)
598
+ list.emplace_back (ctx->element [i]->str );
599
+
600
+ return list;
601
+ }
602
+
603
+ int64_t DBConnector::incr (const string &key)
604
+ {
605
+ RedisCommand sincr;
606
+ sincr.format (" INCR %s" , key.c_str ());
607
+ RedisReply r (this , sincr, REDIS_REPLY_INTEGER);
608
+ return r.getContext ()->integer ;
609
+ }
610
+
611
+ int64_t DBConnector::decr (const string &key)
612
+ {
613
+ RedisCommand sdecr;
614
+ sdecr.format (" DECR %s" , key.c_str ());
615
+ RedisReply r (this , sdecr, REDIS_REPLY_INTEGER);
616
+ return r.getContext ()->integer ;
617
+ }
618
+
619
+ shared_ptr<string> DBConnector::get (const string &key)
620
+ {
621
+ RedisCommand sget;
622
+ sget.format (" GET %s" , key.c_str ());
623
+ RedisReply r (this , sget);
624
+ auto reply = r.getContext ();
625
+
626
+ if (reply->type == REDIS_REPLY_NIL)
627
+ {
628
+ return shared_ptr<string>(NULL );
629
+ }
630
+
631
+ if (reply->type == REDIS_REPLY_STRING)
632
+ {
633
+ shared_ptr<string> ptr (new string (reply->str ));
634
+ return ptr;
635
+ }
636
+
637
+ throw runtime_error (" GET failed, memory exception" );
638
+ }
639
+
640
+ shared_ptr<string> DBConnector::hget (const string &key, const string &field)
641
+ {
642
+ RedisCommand shget;
643
+ shget.format (" HGET %s %s" , key.c_str (), field.c_str ());
644
+ RedisReply r (this , shget);
645
+ auto reply = r.getContext ();
646
+
647
+ if (reply->type == REDIS_REPLY_NIL)
648
+ {
649
+ return shared_ptr<string>(NULL );
650
+ }
651
+
652
+ if (reply->type == REDIS_REPLY_STRING)
653
+ {
654
+ shared_ptr<string> ptr (new string (reply->str ));
655
+ return ptr;
656
+ }
657
+
658
+ SWSS_LOG_ERROR (" HGET failed, reply-type: %d, %s: %s" , reply->type , key.c_str (), field.c_str ());
659
+ throw runtime_error (" HGET failed, unexpected reply type, memory exception" );
660
+ }
661
+
662
+ int64_t DBConnector::rpush (const string &list, const string &item)
663
+ {
664
+ RedisCommand srpush;
665
+ srpush.format (" RPUSH %s %s" , list.c_str (), item.c_str ());
666
+ RedisReply r (this , srpush, REDIS_REPLY_INTEGER);
667
+ return r.getContext ()->integer ;
668
+ }
669
+
670
+ shared_ptr<string> DBConnector::blpop (const string &list, int timeout)
671
+ {
672
+ RedisCommand sblpop;
673
+ sblpop.format (" BLPOP %s %d" , list.c_str (), timeout);
674
+ RedisReply r (this , sblpop);
675
+ auto reply = r.getContext ();
676
+
677
+ if (reply->type == REDIS_REPLY_NIL)
678
+ {
679
+ return shared_ptr<string>(NULL );
680
+ }
681
+
682
+ if (reply->type == REDIS_REPLY_STRING)
683
+ {
684
+ shared_ptr<string> ptr (new string (reply->str ));
685
+ return ptr;
686
+ }
687
+
688
+ throw runtime_error (" GET failed, memory exception" );
689
+ }
690
+
530
691
}
0 commit comments