diff --git a/Redis/include/Poco/Redis/Command.h b/Redis/include/Poco/Redis/Command.h new file mode 100644 index 000000000..ae0dd52b5 --- /dev/null +++ b/Redis/include/Poco/Redis/Command.h @@ -0,0 +1,47 @@ +// +// Command.h +// +// $Id$ +// +// Library: Redis +// Package: Redis +// Module: Command +// +// Definition of the Command class. +// +// Copyright (c) 2012, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + +#ifndef Redis_Command_INCLUDED +#define Redis_Command_INCLUDED + +#include "Poco/Redis/Redis.h" +#include "Poco/Redis/Array.h" + +namespace Poco { +namespace Redis { + +class Redis_API Command : public Array + /// Helper class for creating commands. This class contains + /// factory methods for common used Redis commands. +{ +public: + Command(const std::string& command); + /// Constructor + + Command(const Command& copy); + /// Copy constructor + + virtual ~Command(); + /// Destructor + + 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 +}; + +}} // namespace Poco::Redis + +#endif // Redis_Command_INCLUDED diff --git a/Redis/src/Command.cpp b/Redis/src/Command.cpp new file mode 100644 index 000000000..648e06f49 --- /dev/null +++ b/Redis/src/Command.cpp @@ -0,0 +1,54 @@ +// +// Command.cpp +// +// $Id$ +// +// Library: Redis +// Package: Redis +// Module: Command +// +// Implementation of the Command class. +// +// Copyright (c) 2012, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + +#include "Poco/Redis/Command.h" + +namespace Poco { +namespace Redis { + +Command::Command(const std::string& command) : Array() +{ + add(command); +} + +Command::Command(const Command& copy) : Array(copy) +{ +} + +Command::~Command() +{ +} + +Command Command::set(const std::string& key, const std::string& value, bool overwrite, const Poco::Timespan& expireTime, bool create) +{ + Command cmd("SET"); + cmd.add(key); + cmd.add(value); + if ( ! overwrite ) cmd.add("NX"); + if ( ! create ) cmd.add("XX"); + + if ( expireTime.totalMicroseconds() > 0 ) + { + cmd.add("PX"); + cmd.add(expireTime.totalMilliseconds()); + } + + return cmd; +} + + +}} // Poco::Redis \ No newline at end of file