1
0
Fork 0
mirror of https://github.com/KingDuckZ/incredis synced 2024-11-23 00:33:46 +00:00

Add set/get/flushdb/dbsize convenience methods.

This commit is contained in:
King_DuckZ 2016-12-02 14:16:13 +00:00
parent 68a73d3eb6
commit 20edb06241
5 changed files with 77 additions and 2 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
tags

View file

@ -82,6 +82,14 @@ namespace redis {
//Script //Script
bool script_flush ( void ); bool script_flush ( void );
//Misc
bool flushdb ( void );
RedisInt dbsize ( void );
//String
opt_string get ( boost::string_ref parKey );
bool set ( boost::string_ref parKey, boost::string_ref parField );
private: private:
static opt_string_list reply_to_string_list ( const Reply& parReply ); static opt_string_list reply_to_string_list ( const Reply& parReply );

View file

@ -33,6 +33,12 @@ namespace redis {
ZADD_None ZADD_None
}; };
enum ADD_Mode {
ADD_XX,
ADD_NX,
ADD_None
};
IncRedisBatch ( void ) = delete; IncRedisBatch ( void ) = delete;
IncRedisBatch ( IncRedisBatch&& ) = default; IncRedisBatch ( IncRedisBatch&& ) = default;
IncRedisBatch ( const Batch& ) = delete; IncRedisBatch ( const Batch& ) = delete;
@ -50,6 +56,11 @@ namespace redis {
template <typename... Args> template <typename... Args>
IncRedisBatch& del ( Args&&... parArgs ); IncRedisBatch& del ( Args&&... parArgs );
//String
IncRedisBatch& set ( boost::string_ref parKey, boost::string_ref parField, ADD_Mode parMode );
template <typename... Args>
IncRedisBatch& set ( boost::string_ref parKey, boost::string_ref parField, ADD_Mode parMode, Args&&... parArgs );
//Hash //Hash
IncRedisBatch& hget ( boost::string_ref parKey, boost::string_ref parField ); IncRedisBatch& hget ( boost::string_ref parKey, boost::string_ref parField );
template <typename... Args> template <typename... Args>
@ -136,6 +147,24 @@ namespace redis {
return *this; return *this;
} }
template <typename... Args>
IncRedisBatch& IncRedisBatch::set (boost::string_ref parKey, boost::string_ref parField, ADD_Mode parMode, Args&&... parArgs) {
using dhandy::bt::index_range;
switch(parMode) {
case ADD_None:
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "SET", parKey, parField, std::forward<Args>(parArgs)...);
break;
case ADD_NX:
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "SET", parKey, parField, "NX", std::forward<Args>(parArgs)...);
break;
case ADD_XX:
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "SET", parKey, parField, "XX", std::forward<Args>(parArgs)...);
break;
}
return *this;
}
namespace implem { namespace implem {
template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T, bool STRINGIZE=(IDX>=IGNORE_COUNT) && ((IDX-IGNORE_COUNT)%2)==0> template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T, bool STRINGIZE=(IDX>=IGNORE_COUNT) && ((IDX-IGNORE_COUNT)%2)==0>
struct stringize_or_forward_impl { struct stringize_or_forward_impl {
@ -144,7 +173,7 @@ namespace redis {
}; };
template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T> template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T>
struct stringize_or_forward_impl<IGNORE_COUNT, IDX, T, true> { struct stringize_or_forward_impl<IGNORE_COUNT, IDX, T, true> {
static_assert(std::is_floating_point<T>::value, "Scores must be given as floating point values"); static_assert(std::is_floating_point<T>::value, "Value must be given as floating point number");
typedef std::string type; typedef std::string type;
static std::string do_it ( T parT ) { return boost::lexical_cast<std::string>(parT); } static std::string do_it ( T parT ) { return boost::lexical_cast<std::string>(parT); }
}; };

View file

@ -127,11 +127,33 @@ namespace redis {
} }
bool IncRedis::script_flush() { bool IncRedis::script_flush() {
const auto ret = get<StatusString>(m_command.run("SCRIPT", "FLUSH")); const auto ret = redis::get<StatusString>(m_command.run("SCRIPT", "FLUSH"));
return ret.is_ok(); return ret.is_ok();
} }
bool IncRedis::flushdb() {
const auto ret = redis::get<StatusString>(m_command.run("FLUSHDB"));
return ret.is_ok();
}
RedisInt IncRedis::dbsize() {
const auto ret = redis::get<RedisInt>(m_command.run("DBSIZE"));
return ret;
}
auto IncRedis::reply_to_string_list (const Reply& parReply) -> opt_string_list { auto IncRedis::reply_to_string_list (const Reply& parReply) -> opt_string_list {
return optional_string_list(parReply); return optional_string_list(parReply);
} }
auto IncRedis::get (boost::string_ref parKey) -> opt_string {
return optional_string(m_command.run("GET", parKey));
}
bool IncRedis::set (boost::string_ref parKey, boost::string_ref parField) {
auto batch = make_batch();
batch.set(parKey, parField, IncRedisBatch::ADD_None);
assert(batch.replies().size() == 1);
const auto ret = redis::get<StatusString>(batch.replies().front());
return ret.is_ok();
}
} //namespace redis } //namespace redis

View file

@ -74,6 +74,21 @@ namespace redis {
return *this; return *this;
} }
IncRedisBatch& IncRedisBatch::set (boost::string_ref parKey, boost::string_ref parField, ADD_Mode parMode) {
switch(parMode) {
case ADD_None:
m_batch.run("SET", parKey, parField);
break;
case ADD_NX:
m_batch.run("SET", parKey, parField, "NX");
break;
case ADD_XX:
m_batch.run("SET", parKey, parField, "XX");
break;
}
return *this;
}
IncRedisBatch& IncRedisBatch::zrangebyscore (boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores) { IncRedisBatch& IncRedisBatch::zrangebyscore (boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores) {
auto lower_bound = make_boundary(parMin, not parMinIncl); auto lower_bound = make_boundary(parMin, not parMinIncl);
auto upper_bound = make_boundary(parMax, not parMaxIncl); auto upper_bound = make_boundary(parMax, not parMaxIncl);