Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for incrby #831

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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