From 838216b3115ad45e06dbf77b9b2d7838a9675dac Mon Sep 17 00:00:00 2001 From: Runming Wu Date: Tue, 18 Apr 2023 10:34:18 -0700 Subject: [PATCH] Add support for incrby to be used to reserve a range of VIDCOUNTERs as opposed to incrementing one by one. Change-Id: I610ded944783c459e6ab05f97379c2a4721e025b --- common/dbconnector.cpp | 8 ++++++++ common/dbconnector.h | 2 ++ tests/redis_ut.cpp | 9 +++++++++ 3 files changed, 19 insertions(+) diff --git a/common/dbconnector.cpp b/common/dbconnector.cpp index d5807781..95a5d709 100755 --- a/common/dbconnector.cpp +++ b/common/dbconnector.cpp @@ -759,6 +759,14 @@ int64_t DBConnector::incr(const string &key) return r.getContext()->integer; } +int64_t DBConnector::incrby(const string &key, int increment) +{ + RedisCommand sincr; + sincr.format("INCRBY %s %d", key.c_str(), increment); + RedisReply r(this, sincr, REDIS_REPLY_INTEGER); + return r.getContext()->integer; +} + int64_t DBConnector::decr(const string &key) { RedisCommand sdecr; diff --git a/common/dbconnector.h b/common/dbconnector.h index 42c50531..e3a0b50a 100644 --- a/common/dbconnector.h +++ b/common/dbconnector.h @@ -230,6 +230,8 @@ class DBConnector : public RedisContext int64_t incr(const std::string &key); + int64_t incrby(const std::string &key, int increment); + int64_t decr(const std::string &key); int64_t rpush(const std::string &list, const std::string &item); diff --git a/tests/redis_ut.cpp b/tests/redis_ut.cpp index 16384c0d..0cebbcbb 100644 --- a/tests/redis_ut.cpp +++ b/tests/redis_ut.cpp @@ -477,6 +477,15 @@ TEST(DBConnector, RedisClient) cout << "Done." << endl; } +TEST(DBConnector, IncrBy) +{ + DBConnector db("TEST_DB", 0, true); + clearDB(); + db.set("x", 0); + auto y = db.incrby("x", 10); + EXPECT_EQ(y, 10); +} + TEST(DBConnector, HmsetAndDel) { DBConnector db("TEST_DB", 0, true);