(redisCommand(mCtx,"PING"));
bool bRet = (NULL!=reply);
freeReplyObject(reply);
return bRet;
}
//计算频率
bool checkRedisNetwork()
{
time_t tick = time(NULL);
if(tick >= mLastTime + PING_REDIS_SEC)
{
if(Ping() == false)
{
DEBUG_LOG("检测redis断开");
this->RedisConnect();
}
mLastTime = tick;
}
}
redisContext *getCtx() const
{
return mCtx;
}
redisContext *mCtx;
private:
char *mStrerr;
string mHost; // redis host
unsigned int mPort; // redis sever port
string mPass; // redis server password
unsigned int mTimeout; // connect timeout second
time_t mLastTime; //
};
#endif
/*
* ----------------------------------------------------------------------------
* Copyright (c) 2013-2014, xSky
* All rights reserved.
* Distributed under GPL license.
* ----------------------------------------------------------------------------
*/
#include "redis_client.h"
#include
#include
bool xRedisClient::command_bool( const char *cmd, ...)
{
bool bRet = false;
va_list args;
va_start(args, cmd);
redisReply *reply = static_cast(redisvCommand(mCtx, cmd, args));
va_end(args);
if (CheckReply(reply))
{
bRet = (reply->integer==1)?true:false;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::command_status( const char* cmd, ...)
{
bool bRet = false;
va_list args;
va_start(args, cmd);
redisReply *reply = static_cast(redisvCommand(mCtx, cmd, args));
va_end(args);
if (CheckReply(reply))
{
bRet = true;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::command_integer( int64_t &retval, const char* cmd, ...)
{
bool bRet = false;
if(this->mCtx == NULL)
{
return bRet;
}
va_list args;
va_start(args, cmd);
redisReply *reply = static_cast(redisvCommand(this->mCtx, cmd, args));
va_end(args);
if (CheckReply(reply))
{
retval = reply->integer;
bRet = true;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::command_string( string &data, const char* cmd, ...)
{
bool bRet = false;
if(this->mCtx == NULL)
{
return bRet;
}
va_list args;
va_start(args, cmd);
redisReply *reply = static_cast(redisvCommand(this->mCtx, cmd, args));
va_end(args);
if (CheckReply(reply))
{
data = reply->str;
bRet = true;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::command_list( VALUES &vValue, const char* cmd, ...)
{
bool bRet = false;
va_list args;
va_start(args, cmd);
redisReply *reply = static_cast(redisvCommand(this->mCtx, cmd, args));
va_end(args);
if (CheckReply(reply))
{
for (unsigned int i =0; ielements; i++)
{
vValue.push_back(reply->element[i]->str);
}
bRet = true;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::command_array( ArrayReply& array, const char* cmd, ...){
bool bRet = false;
va_list args;
va_start(args, cmd);
redisReply *reply = static_cast(redisvCommand(this->mCtx, cmd, args));
va_end(args);
if (CheckReply(reply))
{
for (unsigned int i =0; ielements; i++)
{
DataItem item;
item.type = reply->element[i]->type;
item.str = reply->element[i]->str;
array.push_back(item);
}
bRet = true;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::commandargv_bool( const VDATA& vData) {
bool bRet = false;
vector argv( vData.size() );
vector argvlen( vData.size() );
unsigned int j = 0;
for ( vector::const_iterator i = vData.begin(); i != vData.end(); ++i, ++j )
{
argv[j] = i->c_str(), argvlen[j] = i->size();
}
redisReply *reply = static_cast(redisCommandArgv(this->mCtx, argv.size(), &(argv[0]), &(argvlen[0])));
if (CheckReply(reply))
{
bRet = (reply->integer==1)?true:false;
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::commandargv_status( const VDATA& vData)
{
bool bRet = false;
vector argv( vData.size() );
vector argvlen( vData.size() );
unsigned int j = 0;
for ( vector::const_iterator i = vData.begin(); i != vData.end(); ++i, ++j )
{
argv[j] = i->c_str(), argvlen[j] = i->size();
}
redisReply *reply = static_cast(redisCommandArgv(this->mCtx, argv.size(), &(argv[0]), &(argvlen[0])));
if (CheckReply(reply))
{
bRet = true;
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::commandargv_array( const VDATA& vDataIn, ArrayReply& array)
{
bool bRet = false;
vector argv( vDataIn.size() );
vector argvlen( vDataIn.size() );
unsigned int j = 0;
for ( vector::const_iterator i = vDataIn.begin(); i != vDataIn.end(); ++i, ++j )
{
argv[j] = i->c_str(), argvlen[j] = i->size();
}
redisReply *reply = static_cast(redisCommandArgv(this->mCtx, argv.size(), &(argv[0]), &(argvlen[0])));
if (CheckReply(reply))
{
for (unsigned int i =0; ielements; i++)
{
DataItem item;
item.type = reply->element[i]->type;
item.str = reply->element[i]->str;
array.push_back(item);
}
bRet = true;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::commandargv_array( const VDATA& vDataIn, VALUES& array)
{
bool bRet = false;
vector argv( vDataIn.size() );
vector argvlen( vDataIn.size() );
unsigned int j = 0;
for ( vector::const_iterator i = vDataIn.begin(); i != vDataIn.end(); ++i, ++j )
{
argv[j] = i->c_str(), argvlen[j] = i->size();
}
redisReply *reply = static_cast(redisCommandArgv(this->mCtx, argv.size(), &(argv[0]), &(argvlen[0])));
if (CheckReply(reply))
{
for (unsigned int i =0; ielements; i++)
{
array.push_back(reply->element[i]->str);
}
bRet = true;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::commandargv_integer( const VDATA& vDataIn, int64_t& retval)
{
bool bRet = false;
vector argv( vDataIn.size() );
vector argvlen( vDataIn.size() );
unsigned int j = 0;
for ( vector::const_iterator iter = vDataIn.begin(); iter != vDataIn.end(); ++iter, ++j )
{
argv[j] = iter->c_str(), argvlen[j] = iter->size();
}
redisReply *reply = static_cast(redisCommandArgv(this->mCtx, argv.size(), &(argv[0]), &(argvlen[0])));
if (CheckReply(reply))
{
retval = reply->integer;
bRet = true;
} else
{
SetErrInfo(reply->str);
}
FreeReply(reply);
return bRet;
}
bool xRedisClient::incr( const string& key, int& result)
{
return command_integer((int64_t&)result, "INCR %s", key.c_str());
}
bool xRedisClient::set( const string& key, const string& value)
{
VDATA vCmdData;
vCmdData.push_back("SET");
vCmdData.push_back(key);
vCmdData.push_back(value);
return commandargv_status( vCmdData);
}
bool xRedisClient::del(const string& key)
{
return command_bool( "DEL %s", key.c_str());
}
bool xRedisClient::hdel(const string& key, const string& filed, int64_t& count)
{
return command_integer( count, "HDEL %s %s %s", key.c_str(), filed.c_str());
}
bool xRedisClient::hdel(const string& key, const KEYS& vfiled, int64_t& count)
{
VDATA vCmdData;
vCmdData.push_back("HDEL");
vCmdData.push_back(key);
addparam(vCmdData, vfiled);
return commandargv_integer(vCmdData, count);
}
bool xRedisClient::hexist(const string& key, const string& filed)
{
return command_bool("HEXIST %s %s %s", key.c_str(), filed.c_str());
}
bool xRedisClient::hget(const string& key, const string& filed, string& value)
{
return command_string(value, "HGET %s %s %s", key.c_str(), filed.c_str());
}
bool xRedisClient::hgetall(const string& key, ArrayReply& array)
{
return command_array( array, "HGETALL %s", key.c_str());
}
bool xRedisClient::hkeys(const string& key, KEYS& keys)
{
return command_list(keys, "HKEYS %s", key.c_str());
}
bool xRedisClient::hlen(const string& key, int64_t& count)
{
return command_integer(count, "HLEN %s", key.c_str());
}
bool xRedisClient::hmget(const string& key, const KEYS& filed, ArrayReply& array)
{
VDATA vCmdData;
vCmdData.push_back("HMGET");
vCmdData.push_back(key);
addparam(vCmdData, filed);
return commandargv_array(vCmdData, array);
}
bool xRedisClient::hmset(const string& key, const VDATA& vData)
{
VDATA vCmdData;
vCmdData.push_back("HMGET");
vCmdData.push_back(key);
addparam(vCmdData, vData);
return commandargv_bool(vCmdData);
}
bool xRedisClient::hset(const string& key, const string& filed, const string& value, int64_t& retval)
{
return command_integer( retval, "HSET %s %s %s", key.c_str(), filed.c_str(), value.c_str());
}
bool xRedisClient::hvals(const string& key, VALUES& values)
{
return command_list( values, "HVALS %s", key.c_str());
}
bool xRedisClient::exists(const string& key)
{
return command_bool( "EXISTS %s", key.c_str());
}
bool xRedisClient::randomkey( KEY& key)
{
return command_string( key, "RANDOMKEY");
}
bool xRedisClient::llen(const string& key, int64_t& retval)
{
return command_integer(retval, "LLEN %s", key.c_str());
}
bool xRedisClient::lpop(const string& key, string& value)
{
return command_string(value, "LPOP %s", key.c_str());
}
bool xRedisClient::lpush(const string& key, const VALUES& vValue, int64_t& length)
{
VDATA vCmdData;
vCmdData.push_back("lpush");
vCmdData.push_back(key);
addparam(vCmdData, vValue);
return commandargv_integer(vCmdData, length);
}
bool xRedisClient::lrange( const string& key, const int64_t start, const int64_t end, ArrayReply& array)
{
return command_array(array, "LRANGE %s", key.c_str(), start, end);
}
bool xRedisClient::ltrim(const string& key, const int start, const int end)
{
return command_status( "ltrim %s %d %d", key.c_str(), start, end);
}
bool xRedisClient::rpop(const string& key, string& value)
{
return command_string(value, "LPOP %s", key.c_str());
}
bool xRedisClient::rpoplpush(const string& key_src, const string& key_dest, string& value)
{
return command_string(value, "RPOPLPUSH %s %s", key_src.c_str(), key_dest.c_str());
}
bool xRedisClient::rpush(const string& key, const VALUES& vValue, int64_t& length)
{
VDATA vCmdData;
vCmdData.push_back("rpush");
vCmdData.push_back(key);
addparam(vCmdData, vValue);
return commandargv_integer( vCmdData, length);
}
bool xRedisClient::rpushx( const string& key, const string& value, int64_t& length)
{
return command_integer(length, "RPUSHX %s %s", key.c_str(), value.c_str());
}
//sets
bool xRedisClient::sadd( const string& key, const VALUES& vValue, int64_t& count)
{
VDATA vCmdData;
vCmdData.push_back("SADD");
vCmdData.push_back(key);
addparam(vCmdData, vValue);
return commandargv_integer(vCmdData, count);
}
bool xRedisClient::smove(const KEY& srckey, const KEY& deskey, const VALUE& member)
{
return command_bool( "SMOVE %s", srckey.c_str(), deskey.c_str(), member.c_str());
}
bool xRedisClient::spop(const KEY& key, VALUE& member)
{
return command_string(member, "SPOP %s", key.c_str());
}
bool xRedisClient::srandmember(const KEY& key, VALUES& members, int count)
{
if (0==count)
{
return command_list( members, "SRANDMEMBER %s", key.c_str());
}
return command_list( members, "SRANDMEMBER %s %d", key.c_str(), count);
}
bool xRedisClient::zadd( const KEY& key, const VALUES& vValues, int64_t& count)
{
VDATA vCmdData;
vCmdData.push_back("ZADD");
vCmdData.push_back(key);
addparam(vCmdData, vValues);
return commandargv_integer( vCmdData, count);
}
bool xRedisClient::setbit(const string& key, const int offset, const int64_t newbitValue, int64_t oldbitValue)
{
return command_integer(oldbitValue, "SETBIT %s %d %lld", key.c_str(), offset, newbitValue);
}
bool xRedisClient::get(const string& key, string& value)
{
return command_string(value, "GET %s", key.c_str());
}
bool xRedisClient::getbit( const string& key, const int& offset, int& bit )
{
return command_integer((int64_t&)bit, "GETBIT %s %d", key.c_str(), offset);
}
bool xRedisClient::getrange(const string& key, const int start, const int end, string& out)
{
return command_string(out, "GETRANGE %s %d %d", key.c_str(), start, end);
}
bool xRedisClient::getset(const string& key, const string& newValue, string& oldValue)
{
return command_string(oldValue, "GETSET %s %s", key.c_str(), newValue.c_str());
}
bool xRedisClient::setrange(const string& key, const int offset, const string& value, int& length)
{
return command_integer((int64_t&)length, "setrange %s %d %s", key. c_str(), offset, value.c_str());
}
bool xRedisClient::incrby(const string& key, const int by, int& result)
{
return command_integer((int64_t&)result, "INCR %s %d", key.c_str(), by);
}
bool xRedisClient::decr( const string& key, int& result)
{
return command_integer((int64_t&)result,"decr %s", key.c_str());
}
bool xRedisClient::decrby( const string& key, const int by, int& result)
{
return command_integer((int64_t&)result, "decrby %s %d", key.c_str(), by);
}