Skip to content

Commit

Permalink
Add support for incrby to be used to reserve a range of VIDCOUNTERs as
Browse files Browse the repository at this point in the history
opposed to incrementing one by one.

Change-Id: I610ded944783c459e6ab05f97379c2a4721e025b
  • Loading branch information
mint570 committed Nov 7, 2023
1 parent ec0fa46 commit 838216b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 838216b

Please sign in to comment.