Add hset/hget

This commit is contained in:
fbraem 2015-11-17 20:31:23 +01:00
parent f7bb5d319f
commit 90eb6dcd18
4 changed files with 61 additions and 2 deletions

View File

@ -69,6 +69,12 @@ public:
static Command get(const std::string& key);
/// Returns an GET command
static Command hget(const std::string& hash, const std::string& field);
/// Returns an HGET command
static Command hset(const std::string& hash, const std::string& field, const std::string& value, bool create = true);
/// Returns an HSET or HSETNX (when create is false) command
static Command incr(const std::string& key, Int64 by = 0);
/// Returns an INCR or INCRBY command. Calls INCR when by is omitted or zero.

View File

@ -118,6 +118,24 @@ Command Command::get(const std::string& key)
return cmd;
}
Command Command::hget(const std::string& hash, const std::string& field)
{
Command cmd("HGET");
cmd << hash << field;
return cmd;
}
Command Command::hset(const std::string& hash, const std::string& field, const std::string& value, bool create)
{
Command cmd(create ? "HSET" : "HSETNX");
cmd << hash << field << value;
return cmd;
}
Command Command::incr(const std::string& key, Int64 by)
{
Command cmd(by == 0 ? "INCR" : "INCRBY");

View File

@ -374,6 +374,39 @@ void RedisTest::testEval()
}
void RedisTest::testHSet()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("myhash");
Command hset = Command::hset("myhash", "field1", "Hello");
try
{
Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
assert(value == 1);
}
catch(RedisException &e)
{
fail(e.message());
}
Command hget = Command::hget("myhash", "field1");
try
{
BulkString s = _redis.execute<BulkString>(hget);
assert(s.value().compare("Hello") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testIncr()
{
if (!_connected)
@ -2402,11 +2435,12 @@ CppUnit::Test* RedisTest::suite()
CppUnit_addTest(pSuite, RedisTest, testBRpop);
CppUnit_addTest(pSuite, RedisTest, testDecr);
CppUnit_addTest(pSuite, RedisTest, testDecr);
CppUnit_addTest(pSuite, RedisTest, testIncr);
CppUnit_addTest(pSuite, RedisTest, testIncrBy);
CppUnit_addTest(pSuite, RedisTest, testEcho);
CppUnit_addTest(pSuite, RedisTest, testError);
CppUnit_addTest(pSuite, RedisTest, testEval);
CppUnit_addTest(pSuite, RedisTest, testHSet);
CppUnit_addTest(pSuite, RedisTest, testIncr);
CppUnit_addTest(pSuite, RedisTest, testIncrBy);
CppUnit_addTest(pSuite, RedisTest, testLIndex);
CppUnit_addTest(pSuite, RedisTest, testLInsert);
CppUnit_addTest(pSuite, RedisTest, testLPop);

View File

@ -37,6 +37,7 @@ public:
void testEcho();
void testError();
void testEval();
void testHSet();
void testIncr();
void testIncrBy();
void testLIndex();