Skip to content

Commit

Permalink
代码优化,增加zrangebyscore和zremrangebyscore两个命令
Browse files Browse the repository at this point in the history
  • Loading branch information
leiwenbin committed May 15, 2019
1 parent c7062b1 commit fde504a
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/xRedisClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ bool xRedisClient::commandargv_integer(const RedisDBIdx& dbi, const VDATA& vData

bool xRedisClient::ScanFun(const char* cmd, const RedisDBIdx& dbi, const std::string* key,
int64_t& cursor, const char* pattern, uint32_t count, ArrayReply& array, xRedisContext& ctx) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
VDATA vCmdData;
vCmdData.push_back(cmd);
if (NULL != key) {
Expand Down
4 changes: 2 additions & 2 deletions src/xRedisClient_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ void xRedisClient::quit() {

bool xRedisClient::echo(const RedisDBIdx& dbi, const string& str, std::string& value) {
if (0 == str.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_string(dbi, value, "echo %s", str.c_str());
}

bool xRedisClient::select(const RedisDBIdx& dbi, int32_t pos) {
if (pos < 1) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_bool(dbi, "SELECT %d", pos);
}

Expand Down
28 changes: 14 additions & 14 deletions src/xRedisClient_hashs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <redis/xredis/xRedisPool.h>

bool xRedisClient::hdel(const RedisDBIdx& dbi, const string& key, const string& field, int64_t& count) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, count, "HDEL %s %s", key.c_str(), field.c_str());
}

Expand All @@ -19,32 +19,32 @@ bool xRedisClient::hdel(const RedisDBIdx& dbi, const string& key, const KEYS& vf
vCmdData.push_back("HDEL");
vCmdData.push_back(key);
addparam(vCmdData, vfiled);
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return commandargv_integer(dbi, vCmdData, count);
}

bool xRedisClient::hexist(const RedisDBIdx& dbi, const string& key, const string& field) {
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_bool(dbi, "HEXISTS %s %s", key.c_str(), field.c_str());
}

bool xRedisClient::hget(const RedisDBIdx& dbi, const string& key, const string& field, string& value) {
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_string(dbi, value, "HGET %s %s", key.c_str(), field.c_str());
}

bool xRedisClient::hgetall(const RedisDBIdx& dbi, const string& key, ArrayReply& array) {
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_array(dbi, array, "HGETALL %s", key.c_str());
}

bool xRedisClient::hincrby(const RedisDBIdx& dbi, const string& key, const string& field, int64_t increment, int64_t& num) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, num, "HINCRBY %s %s %lld", key.c_str(), field.c_str(), increment);
}

bool xRedisClient::hincrbyfloat(const RedisDBIdx& dbi, const string& key, const string& field, float increment, float& value) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
bool bRet = false;
RedisConn* pRedisConn = mRedisPool->GetConnection(dbi.mType, dbi.mIndex);
if (NULL == pRedisConn)
Expand All @@ -62,12 +62,12 @@ bool xRedisClient::hincrbyfloat(const RedisDBIdx& dbi, const string& key, const
}

bool xRedisClient::hkeys(const RedisDBIdx& dbi, const string& key, KEYS& keys) {
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_list(dbi, keys, "HKEYS %s", key.c_str());
}

bool xRedisClient::hlen(const RedisDBIdx& dbi, const string& key, int64_t& count) {
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_integer(dbi, count, "HLEN %s", key.c_str());
}

Expand All @@ -76,7 +76,7 @@ bool xRedisClient::hmget(const RedisDBIdx& dbi, const string& key, const KEYS& f
vCmdData.push_back("HMGET");
vCmdData.push_back(key);
addparam(vCmdData, field);
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return commandargv_array(dbi, vCmdData, array);
}

Expand All @@ -85,22 +85,22 @@ bool xRedisClient::hmset(const RedisDBIdx& dbi, const string& key, const VDATA&
vCmdData.push_back("HMSET");
vCmdData.push_back(key);
addparam(vCmdData, vData);
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return commandargv_status(dbi, vCmdData);
}

bool xRedisClient::hset(const RedisDBIdx& dbi, const string& key, const string& field, const string& value, int64_t& retval) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, retval, "HSET %s %s %s", key.c_str(), field.c_str(), value.c_str());
}

bool xRedisClient::hsetnx(const RedisDBIdx& dbi, const string& key, const string& field, const string& value) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_bool(dbi, "HSETNX %s %s %s", key.c_str(), field.c_str(), value.c_str());
}

bool xRedisClient::hvals(const RedisDBIdx& dbi, const string& key, VALUES& values) {
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_list(dbi, values, "HVALS %s", key.c_str());
}

Expand Down
24 changes: 12 additions & 12 deletions src/xRedisClient_keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
bool xRedisClient::del(const RedisDBIdx& dbi, const string& key) {
if (0 == key.length()) return false;

SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_bool(dbi, "DEL %s", key.c_str());
}

Expand All @@ -31,13 +31,13 @@ bool xRedisClient::del(const DBIArray& vdbi, const KEYS& vkey, int64_t& count) {

bool xRedisClient::exists(const RedisDBIdx& dbi, const string& key) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_bool(dbi, "EXISTS %s", key.c_str());
}

bool xRedisClient::expire(const RedisDBIdx& dbi, const string& key, uint32_t second) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
int64_t ret = -1;
if (!command_integer(dbi, ret, "EXPIRE %s %u", key.c_str(), second))
return false;
Expand All @@ -52,13 +52,13 @@ bool xRedisClient::expire(const RedisDBIdx& dbi, const string& key, uint32_t sec

bool xRedisClient::expireat(const RedisDBIdx& dbi, const string& key, uint32_t timestamp) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_bool(dbi, "EXPIREAT %s %u", key.c_str(), timestamp);
}

bool xRedisClient::persist(const RedisDBIdx& dbi, const string& key) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_bool(dbi, "PERSIST %s %u", key.c_str());
}

Expand All @@ -70,29 +70,29 @@ bool xRedisClient::pexpire(const RedisDBIdx& dbi, const string& key, uint32_t mi
bool xRedisClient::pexpireat(const RedisDBIdx& dbi, const string& key, uint32_t millisecondstimestamp) {
if (0 == key.length())
return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_bool(dbi, "PEXPIREAT %s %u", key.c_str(), millisecondstimestamp);
}

bool xRedisClient::pttl(const RedisDBIdx& dbi, const string& key, int64_t& milliseconds) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, milliseconds, "PTTL %s", key.c_str());
}

bool xRedisClient::ttl(const RedisDBIdx& dbi, const string& key, int64_t& seconds) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_integer(dbi, seconds, "TTL %s", key.c_str());
}

bool xRedisClient::type(const RedisDBIdx& dbi, const std::string& key, std::string& value) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_string(dbi, value, "TYPE %s", key.c_str());
}

bool xRedisClient::randomkey(const RedisDBIdx& dbi, KEY& key) {
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_string(dbi, key, "RANDOMKEY");
}

Expand Down Expand Up @@ -129,13 +129,13 @@ bool xRedisClient::sort(const RedisDBIdx& dbi, ArrayReply& array, const string&
vCmdData.push_back(sort_order[order]);
if (destination)
vCmdData.push_back(destination);
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return commandargv_array(dbi, vCmdData, array);
}

bool xRedisClient::keys(const RedisDBIdx& dbi, const string& key, KEYS& keys) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_list(dbi, keys, "KEYS %s", key.c_str());
}

Expand Down
32 changes: 16 additions & 16 deletions src/xRedisClient_lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@

bool xRedisClient::lindex(const RedisDBIdx& dbi, const string& key, int64_t index, VALUE& value) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_string(dbi, value, "LINDEX %s %lld", key.c_str(), index);
}

bool xRedisClient::linsert(const RedisDBIdx& dbi, const string& key, const LMODEL mod, const string& pivot, const string& value, int64_t& retval) {
static const char* lmodel[2] = {"BEFORE", "AFTER"};
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, retval, "LINSERT %s %s %s %s", key.c_str(), lmodel[mod], pivot.c_str(), value.c_str());
}

bool xRedisClient::llen(const RedisDBIdx& dbi, const string& key, int64_t& retval) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_integer(dbi, retval, "LLEN %s", key.c_str());
}

bool xRedisClient::lpop(const RedisDBIdx& dbi, const string& key, string& value) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_string(dbi, value, "LPOP %s", key.c_str());
}

Expand All @@ -40,55 +40,55 @@ bool xRedisClient::lpush(const RedisDBIdx& dbi, const string& key, const VALUES&
vCmdData.push_back("LPUSH");
vCmdData.push_back(key);
addparam(vCmdData, vValue);
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return commandargv_integer(dbi, vCmdData, length);
}

bool xRedisClient::lpushx(const RedisDBIdx& dbi, const string& key, const string& value, int64_t& length) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, length, "LPUSHX %s %s", key.c_str(), value.c_str());
}

bool xRedisClient::blpop(const RedisDBIdx& dbi, const std::string& key, VALUES& values, int64_t& timeout) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_list(dbi, values, "BLPOP %s %ld", key.c_str(), timeout);
}

bool xRedisClient::lrange(const RedisDBIdx& dbi, const string& key, int64_t start, int64_t end, ArrayReply& array) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_array(dbi, array, "LRANGE %s %lld %lld", key.c_str(), start, end);
}

bool xRedisClient::lrem(const RedisDBIdx& dbi, const string& key, int32_t count, const string& value, int64_t num) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, num, "LREM %s %d %s", key.c_str(), count, value.c_str());
}

bool xRedisClient::lset(const RedisDBIdx& dbi, const string& key, int32_t index, const string& value) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_status(dbi, "LSET %s %d %s", key.c_str(), index, value.c_str());
}

bool xRedisClient::ltrim(const RedisDBIdx& dbi, const string& key, int32_t start, int32_t end) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_status(dbi, "LTRIM %s %d %d", key.c_str(), start, end);
}

bool xRedisClient::rpop(const RedisDBIdx& dbi, const string& key, string& value) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_string(dbi, value, "RPOP %s", key.c_str());
}

bool xRedisClient::rpoplpush(const RedisDBIdx& dbi, const string& key_src, const string& key_dest, string& value) {
if ((0 == key_src.length()) || (0 == key_dest.length())) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_string(dbi, value, "RPOPLPUSH %s %s", key_src.c_str(), key_dest.c_str());
}

Expand All @@ -98,19 +98,19 @@ bool xRedisClient::rpush(const RedisDBIdx& dbi, const string& key, const VALUES&
vCmdData.push_back("RPUSH");
vCmdData.push_back(key);
addparam(vCmdData, vValue);
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return commandargv_integer(dbi, vCmdData, length);
}

bool xRedisClient::rpushx(const RedisDBIdx& dbi, const string& key, const string& value, int64_t& length) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, length, "RPUSHX %s %s", key.c_str(), value.c_str());
}

bool xRedisClient::brpop(const RedisDBIdx& dbi, const std::string& key, VALUES& values, int64_t& timeout) {
if (0 == key.length()) return false;
SETDEFAULTIOTYPE(SLAVE);
SETDEFAULTIOTYPE(SLAVE)
return command_list(dbi, values, "BRPOP %s %ld", key.c_str(), timeout);
}

16 changes: 8 additions & 8 deletions src/xRedisClient_pubsub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
#include <redis/xredis/xRedisPool.h>

bool xRedisClient::psubscribe(const RedisDBIdx& dbi, const KEYS& patterns, xRedisContext& ctx) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
VDATA vCmdData;
vCmdData.push_back("PSUBSCRIBE");
addparam(vCmdData, patterns);
return commandargv_array_ex(dbi, vCmdData, ctx);
}

bool xRedisClient::publish(const RedisDBIdx& dbi, const KEY& channel, const std::string& message, int64_t& count) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, count, "PUBLISH %s %s", channel.c_str(), message.c_str(), count);
}

bool xRedisClient::pubsub_channels(const RedisDBIdx& dbi, const std::string& pattern, ArrayReply& reply) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_array(dbi, reply, "pubsub channels %s", pattern.c_str());
}

bool xRedisClient::pubsub_numsub(const RedisDBIdx& dbi, const KEYS& keys, ArrayReply& reply) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
VDATA vCmdData;
vCmdData.push_back("pubsub");
vCmdData.push_back("numsub");
Expand All @@ -37,12 +37,12 @@ bool xRedisClient::pubsub_numsub(const RedisDBIdx& dbi, const KEYS& keys, ArrayR
}

bool xRedisClient::pubsub_numpat(const RedisDBIdx& dbi, int64_t& count) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
return command_integer(dbi, count, "pubsub numpat");
}

bool xRedisClient::punsubscribe(const RedisDBIdx& dbi, const KEYS& patterns, xRedisContext& ctx) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
VDATA vCmdData;
vCmdData.push_back("PUNSUBSCRIBE");
addparam(vCmdData, patterns);
Expand Down Expand Up @@ -71,15 +71,15 @@ bool xRedisClient::punsubscribe(const RedisDBIdx& dbi, const KEYS& patterns, xRe
}

bool xRedisClient::subscribe(const RedisDBIdx& dbi, const KEYS& channels, xRedisContext& ctx) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
VDATA vCmdData;
vCmdData.push_back("SUBSCRIBE");
addparam(vCmdData, channels);
return commandargv_array_ex(dbi, vCmdData, ctx);
}

bool xRedisClient::unsubscribe(const RedisDBIdx& dbi, const KEYS& channels, xRedisContext& ctx) {
SETDEFAULTIOTYPE(MASTER);
SETDEFAULTIOTYPE(MASTER)
VDATA vCmdData;
vCmdData.push_back("UNSUBSCRIBE");
addparam(vCmdData, channels);
Expand Down
Loading

0 comments on commit fde504a

Please sign in to comment.