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);