mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 18:42:41 +01:00
Add scard
This commit is contained in:
@@ -30,6 +30,10 @@ namespace Redis {
|
|||||||
class Redis_API Command : public Array
|
class Redis_API Command : public Array
|
||||||
/// Helper class for creating commands. This class contains
|
/// Helper class for creating commands. This class contains
|
||||||
/// factory methods for commonly used Redis commands.
|
/// factory methods for commonly used Redis commands.
|
||||||
|
/// There are two ways of building commands:
|
||||||
|
/// 1. Use this class (which uses option 2 in the factory methods)
|
||||||
|
/// 2. Use the Array class and build the command using the add method
|
||||||
|
/// or << operator.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Command(const std::string& command);
|
Command(const std::string& command);
|
||||||
@@ -105,6 +109,18 @@ public:
|
|||||||
static Command mset(const std::map<std::string, std::string>& keyvalues, bool create = true);
|
static Command mset(const std::map<std::string, std::string>& keyvalues, bool create = true);
|
||||||
/// Returns a MSET or MSETNX (when create is false) command
|
/// Returns a MSET or MSETNX (when create is false) command
|
||||||
|
|
||||||
|
static Command sadd(const std::string& key, const std::string& value);
|
||||||
|
/// Returns a SADD command
|
||||||
|
|
||||||
|
static Command sadd(const std::string& key, const std::vector<std::string>& values);
|
||||||
|
/// Returns a SADD command
|
||||||
|
|
||||||
|
static Command scard(const std::string& key);
|
||||||
|
/// Returns a SCARD command
|
||||||
|
|
||||||
|
static Command smembers(const std::string& key);
|
||||||
|
/// Returns a SMEMBERS command
|
||||||
|
|
||||||
static Command set(const std::string& key, const std::string& value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true);
|
static Command set(const std::string& key, const std::string& value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true);
|
||||||
/// Returns a SET command to set the key with a value
|
/// Returns a SET command to set the key with a value
|
||||||
|
|
||||||
|
|||||||
@@ -245,6 +245,46 @@ Command Command::mset(const std::map<std::string, std::string>& keyvalues, bool
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Command Command::sadd(const std::string& key, const std::string& value)
|
||||||
|
{
|
||||||
|
Command cmd("SADD");
|
||||||
|
|
||||||
|
cmd << key << value;
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command Command::sadd(const std::string& key, const std::vector<std::string>& values)
|
||||||
|
{
|
||||||
|
Command cmd("SADD");
|
||||||
|
|
||||||
|
cmd << key;
|
||||||
|
for(std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
|
||||||
|
{
|
||||||
|
cmd << *it;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command Command::scard(const std::string& key)
|
||||||
|
{
|
||||||
|
Command cmd("SCARD");
|
||||||
|
|
||||||
|
cmd << key;
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command Command::smembers(const std::string& key)
|
||||||
|
{
|
||||||
|
Command cmd("SMEMBERS");
|
||||||
|
|
||||||
|
cmd << key;
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
Command Command::set(const std::string& key, const std::string& value, bool overwrite, const Poco::Timespan& expireTime, bool create)
|
Command Command::set(const std::string& key, const std::string& value, bool overwrite, const Poco::Timespan& expireTime, bool create)
|
||||||
{
|
{
|
||||||
Command cmd("SET");
|
Command cmd("SET");
|
||||||
|
|||||||
@@ -1110,6 +1110,94 @@ void RedisTest::testPubSub()
|
|||||||
_redis.flush();
|
_redis.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RedisTest::testSAdd()
|
||||||
|
{
|
||||||
|
if (!_connected)
|
||||||
|
{
|
||||||
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
delKey("myset");
|
||||||
|
|
||||||
|
Command sadd = Command::sadd("myset", "Hello");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||||
|
assert(result == 1);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
|
||||||
|
sadd = Command::sadd("myset", "World");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||||
|
assert(result == 1);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
|
||||||
|
sadd = Command::sadd("myset", "World");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||||
|
assert(result == 0);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RedisTest::testSCard()
|
||||||
|
{
|
||||||
|
if (!_connected)
|
||||||
|
{
|
||||||
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
delKey("myset");
|
||||||
|
|
||||||
|
Command sadd = Command::sadd("myset", "Hello");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||||
|
assert(result == 1);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
|
||||||
|
sadd = Command::sadd("myset", "World");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||||
|
assert(result == 1);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
|
||||||
|
Command scard = Command::scard("myset");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Poco::Int64 result = _redis.execute<Poco::Int64>(scard);
|
||||||
|
assert(result == 2);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RedisTest::testSet()
|
void RedisTest::testSet()
|
||||||
{
|
{
|
||||||
if (!_connected)
|
if (!_connected)
|
||||||
@@ -1184,6 +1272,53 @@ void RedisTest::testStrlen()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RedisTest::testSMembers()
|
||||||
|
{
|
||||||
|
if (!_connected)
|
||||||
|
{
|
||||||
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
delKey("myset");
|
||||||
|
|
||||||
|
Command sadd = Command::sadd("myset", "Hello");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||||
|
assert(result == 1);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
|
||||||
|
sadd = Command::sadd("myset", "World");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||||
|
assert(result == 1);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
|
||||||
|
Command smembers = Command::smembers("myset");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Array result = _redis.execute<Array>(smembers);
|
||||||
|
|
||||||
|
assert(result.size() == 2);
|
||||||
|
assert(result.get<BulkString>(0).value().compare("World") == 0);
|
||||||
|
assert(result.get<BulkString>(1).value().compare("Hello") == 0);
|
||||||
|
}
|
||||||
|
catch(RedisException &e)
|
||||||
|
{
|
||||||
|
fail(e.message());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RedisTest::testRename()
|
void RedisTest::testRename()
|
||||||
{
|
{
|
||||||
if (!_connected)
|
if (!_connected)
|
||||||
@@ -1537,7 +1672,10 @@ CppUnit::Test* RedisTest::suite()
|
|||||||
CppUnit_addTest(pSuite, RedisTest, testPipeliningWithSendCommands);
|
CppUnit_addTest(pSuite, RedisTest, testPipeliningWithSendCommands);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testPipeliningWithWriteCommand);
|
CppUnit_addTest(pSuite, RedisTest, testPipeliningWithWriteCommand);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testPubSub);
|
CppUnit_addTest(pSuite, RedisTest, testPubSub);
|
||||||
|
CppUnit_addTest(pSuite, RedisTest, testSAdd);
|
||||||
|
CppUnit_addTest(pSuite, RedisTest, testSCard);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testSet);
|
CppUnit_addTest(pSuite, RedisTest, testSet);
|
||||||
|
CppUnit_addTest(pSuite, RedisTest, testSMembers);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testStrlen);
|
CppUnit_addTest(pSuite, RedisTest, testStrlen);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testRename);
|
CppUnit_addTest(pSuite, RedisTest, testRename);
|
||||||
CppUnit_addTest(pSuite, RedisTest, testRenameNx);
|
CppUnit_addTest(pSuite, RedisTest, testRenameNx);
|
||||||
|
|||||||
@@ -51,7 +51,10 @@ public:
|
|||||||
void testPipeliningWithSendCommands();
|
void testPipeliningWithSendCommands();
|
||||||
void testPipeliningWithWriteCommand();
|
void testPipeliningWithWriteCommand();
|
||||||
void testPubSub();
|
void testPubSub();
|
||||||
|
void testSAdd();
|
||||||
|
void testSCard();
|
||||||
void testSet();
|
void testSet();
|
||||||
|
void testSMembers();
|
||||||
void testStrlen();
|
void testStrlen();
|
||||||
void testRename();
|
void testRename();
|
||||||
void testRenameNx();
|
void testRenameNx();
|
||||||
|
|||||||
Reference in New Issue
Block a user