added new commands; fixed handling of broken connections

This commit is contained in:
Günter Obiltschnig 2019-04-05 13:03:57 +02:00
parent 2e26da9b34
commit 196540ce34
4 changed files with 67 additions and 2 deletions

View File

@ -133,7 +133,7 @@ public:
/// Array and void. When the reply is an Error, it will throw
/// a RedisException.
{
T result;
T result = T();
writeCommand(command, true);
readReply(result);
return result;

View File

@ -84,6 +84,9 @@ public:
static Command get(const std::string& key);
/// Creates and returns an GET command.
static Command exists(const std::string& key);
/// Creates and returns an EXISTS command.
static Command hdel(const std::string& hash, const std::string& field);
/// Creates and returns an HDEL command.
@ -253,8 +256,20 @@ public:
static Command rpush(const std::string& list, const StringVec& value, bool create = true);
/// Creates and returns a RPUSH or RPUSHX (when create is false) command.
static Command expire(const std::string& key, Int64 seconds);
/// Creates and returns an EXPIRE command.
static Command ping();
/// Creates and returns a PING command.
static Command multi();
/// Creates and returns a MULTI command.
static Command exec();
/// Creates and returns a EXEC command.
static Command discard();
/// Creates and returns a DISCARD command.
};

View File

@ -73,7 +73,7 @@ void Client::connect()
poco_assert(! _input);
poco_assert(! _output);
_socket.connect(_address);
_socket = Net::StreamSocket(_address);
_input = new RedisInputStream(_socket);
_output = new RedisOutputStream(_socket);
}
@ -105,6 +105,7 @@ void Client::connect(const Timespan& timeout)
poco_assert(! _input);
poco_assert(! _output);
_socket = Net::StreamSocket();
_socket.connect(_address, timeout);
_input = new RedisInputStream(_socket);
_output = new RedisOutputStream(_socket);
@ -166,6 +167,11 @@ RedisType::Ptr Client::readReply()
poco_assert(_input);
int c = _input->get();
if (c == -1)
{
disconnect();
throw RedisException("Lost connection to Redis server");
}
RedisType::Ptr result = RedisType::createRedisType(c);
if (result.isNull())
{

View File

@ -119,6 +119,16 @@ Command Command::get(const std::string& key)
}
Command Command::exists(const std::string& key)
{
Command cmd("EXISTS");
cmd << key;
return cmd;
}
Command Command::hdel(const std::string& hash, const std::string& field)
{
Command cmd("HDEL");
@ -683,6 +693,16 @@ Command Command::rpush(const std::string& list, const StringVec& values, bool cr
}
Command Command::expire(const std::string& key, Int64 seconds)
{
Command cmd("EXPIRE");
cmd << key << NumberFormatter::format(seconds);
return cmd;
}
Command Command::ping()
{
Command cmd("PING");
@ -691,4 +711,28 @@ Command Command::ping()
}
Command Command::multi()
{
Command cmd("MULTI");
return cmd;
}
Command Command::exec()
{
Command cmd("EXEC");
return cmd;
}
Command Command::discard()
{
Command cmd("DISCARD");
return cmd;
}
} } // namespace Poco::Redis