mirror of
https://github.com/pocoproject/poco.git
synced 2025-05-03 07:52:29 +02:00
Add hset/hget
This commit is contained in:
parent
f7bb5d319f
commit
90eb6dcd18
@ -69,6 +69,12 @@ public:
|
|||||||
static Command get(const std::string& key);
|
static Command get(const std::string& key);
|
||||||
/// Returns an GET command
|
/// 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);
|
static Command incr(const std::string& key, Int64 by = 0);
|
||||||
/// Returns an INCR or INCRBY command. Calls INCR when by is omitted or zero.
|
/// Returns an INCR or INCRBY command. Calls INCR when by is omitted or zero.
|
||||||
|
|
||||||
|
@ -118,6 +118,24 @@ Command Command::get(const std::string& key)
|
|||||||
return cmd;
|
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 Command::incr(const std::string& key, Int64 by)
|
||||||
{
|
{
|
||||||
Command cmd(by == 0 ? "INCR" : "INCRBY");
|
Command cmd(by == 0 ? "INCR" : "INCRBY");
|
||||||
|
@ -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()
|
void RedisTest::testIncr()
|
||||||
{
|
{
|
||||||
if (!_connected)
|
if (!_connected)
|
||||||
@ -2402,11 +2435,12 @@ CppUnit::Test* RedisTest::suite()
|
|||||||
CppUnit_addTest(pSuite, RedisTest, testBRpop);
|
CppUnit_addTest(pSuite, RedisTest, testBRpop);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testDecr);
|
CppUnit_addTest(pSuite, RedisTest, testDecr);
|
||||||
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, testEcho);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testError);
|
CppUnit_addTest(pSuite, RedisTest, testError);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testEval);
|
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, testLIndex);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testLInsert);
|
CppUnit_addTest(pSuite, RedisTest, testLInsert);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testLPop);
|
CppUnit_addTest(pSuite, RedisTest, testLPop);
|
||||||
|
@ -37,6 +37,7 @@ public:
|
|||||||
void testEcho();
|
void testEcho();
|
||||||
void testError();
|
void testError();
|
||||||
void testEval();
|
void testEval();
|
||||||
|
void testHSet();
|
||||||
void testIncr();
|
void testIncr();
|
||||||
void testIncrBy();
|
void testIncrBy();
|
||||||
void testLIndex();
|
void testLIndex();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user