因项目使用vs2012,其对C++11支持度有限,故hiredis
使用的 微软win版本redis2.6中项目工程
使用示例
MyRedis redis;
RedisWords word;
word.re("SET").ci("key").ci("%d",9);
redis.Exec(&word);
// 内部使用pipeline实现
MyRedis::Pipe pipe;
pipe.Append(CacheNS::UserEvent::Friend::get_ns_counter_friend_wait(strUserId_).c_str());
pipe.Append(CacheNS::UserEvent::Hello::get_ns_counter_unread_hello(strUserId_).c_str());
pipe.Append(CacheNS::UserEvent::Friend::get_ns_friendchat_not_read_userid(strUserId_).c_str());
pipe.Append(CacheNS::UserEvent::QuanZi::get_ns_quanzi_not_read_userid(strUserId_).c_str());
pipe.Append(CacheNS::UserEvent::QuanZi::get_ns_quanzi_wait_agree_userid(strUserId_).c_str());
redis.Exec(&pipe);
// 直接获取结果
pipe.ResultGetInt(0);
// 获取批量结果
AutoRedisResult result;
// 批量结果选择取出
pipe.Result(result,2);
redis.Exec("SET a 5");
redisw.h
// redisw.h
#ifndef REDISW_H
#define REDISW_H
#include "Hiredis\vs2012\include\hiredis.h"
#define DEFAULT_EXP_TIME 43200 // 12*60*60 seconds
class RedisWords{
public:
const static int MAXLEN_STRINGWORD=512;
private:
unsigned int used;
bool pendingalloc;*/
std::vector<char*> swords;
std::vector<size_t> swordslen;
struct BUF_STATE* xbuffer;
friend class MyRedis;
friend struct BUF_STATE;
void clr();
void pushString(const char *cmd, va_list ap);
void pushBin(unsigned int len,void *data);
void push(const RedisWords& src);
public:
RedisWords(){
xbuffer=0;}
RedisWords(const char *cmd, ...){
xbuffer=0;va_list ap;va_start(ap, cmd);pushString(cmd,ap);va_end(ap);}
RedisWords(const char *cmd, va_list ap){
xbuffer=0;pushString(cmd,ap);}
RedisWords(unsigned int len,char *data){
xbuffer=0;pushBin(len,data);}
RedisWords(const RedisWords& src){
xbuffer=0;push(src);}
RedisWords(RedisWords&& src);
~RedisWords();
RedisWords& operator =(const RedisWords& src){
clr();push(src);return *this;}
static RedisWords co(const char *cmd, ...){
va_list ap;va_start(ap, cmd);RedisWords wd(cmd,ap);;return wd;}
RedisWords& re(const char *cmd, ...){
clr();va_list ap;va_start(ap, cmd);pushString(cmd,ap);va_end(ap);return *this;}
RedisWords& ci(const char *format, ...){
va_list ap;va_start(ap, format);pushString(format,ap);va_end(ap);return *this;}
RedisWords& ci(unsigned int len,void *data){
pushBin(len,data);return *this;}
};
class RedisResultDef{
public:
long long defll;
std::string defstr;
RedisResultDef():defll(0),defstr(""){
}
};
class AutoRedisResult{
public:
AutoRedisResult():resp(0),autofree(false),pdef(0){
}
~AutoRedisResult(){
if(resp&&autofree){
freeReplyObject(resp);}}
redisReply* Obj(){
return resp;}
int getInt() const{
if(!resp)return (int)pdef->defll; return (resp->type == REDIS_REPLY_INTEGER)?(int)resp->integer:((resp->type == REDIS_REPLY_STRING)?(int)atoi(resp->str):(int)pdef->defll);}
long long getLL() const{
if(!resp)return pdef->defll;return (resp->type == REDIS_REPLY_INTEGER)?resp->integer:((resp->type == REDIS_REPLY_STRING)?_atoi64(resp->str):(int)pdef->defll);}
const char* getString() const{
if(!resp)return pdef->defstr.c_str();return (resp->type == REDIS_REPLY_STRING)?(resp->str):pdef->defstr.c_str();}
template<class T> bool getData(T& data) const {
if(!resp){
return false;}
if(resp->type != REDIS_REPLY_STRING || resp->len <= 0) {
return false;}
Win32Tools::biostream bis;
bis.attach(resp->str, resp->len);
bis >> data;
return true;
}
size_t getSize() const{
if(!resp)return 0;return (resp->type == REDIS_REPLY_ARRAY)?(resp->elements):0;}
int getInt(unsigned int index) const{
if(!resp)return (int)pdef->defll;VIOASSERT(index<resp->elements);return (resp->element[index]->type == REDIS_REPLY_INTEGER)?(int)resp->integer:((resp->element[index]->type == REDIS_REPLY_STRING)?(int)atoi(resp->element[index]->str):(int)pdef->defll);}//resp->element[index]->integer);}
long long getLL(unsigned int index) const{
if(!resp)return pdef->defll;VIOASSERT(index<resp->elements);return (resp->element[index]->type == REDIS_REPLY_INTEGER)?resp->integer:((resp->element[index]->type == REDIS_REPLY_STRING)?_atoi64(resp->element[index]->str):(int)pdef->defll);}//resp->element[index]->integer);}
const char* getString(unsigned int index) const{
if(!resp)return pdef->defstr.c_str();VIOASSERT(index<resp->elements);return (resp->element[index]->type == REDIS_REPLY_STRING)?(resp->element[index]->str):pdef->defstr.c_str();}
template<class T> bool getData(unsigned int index, T& data) const{
if(!resp){
return false;}
VIOASSERT(index < resp->elements);
if(resp->element[index]->type != REDIS_REPLY_STRING || resp->element[index]->len <= 0) {
return false;}
Win32Tools::biostream bis;
bis.attach(resp->element[index]->str, resp->element[index]->len);
bis >> data;
return true;
}
private:
AutoRedisResult(redisReply* _resp):resp((redisReply*)_resp),autofree(false),pdef(0){
}
AutoRedisResult(const AutoRedisResult& src):resp(0),autofree(false),pdef(0){
VIOASSERT(0);}
AutoRedisResult& operator =(redisReply* _resp){
if(resp&&autofree){
freeReplyObject(resp);} resp=_resp;autofree=false;return *this;}
AutoRedisResult& operator =(const AutoRedisResult* _resp){
VIOASSERT(0);return *this;}
redisReply* resp;
bool autofree;
RedisResultDef* pdef;
friend class MyRedis;
};
class MyRedis{
struct redisContext* content;
friend class MyRedisPipe;
public:
MyRedis():content(0){
}
~MyRedis(){
if(content){
redisFree(content);}}
RedisResultDef def;
void setDef(int i){
def.defll=i;}
void setDef(const char* s){
def.defstr=s;}
static std::string RedisAddr;
static int RedisPort;
static std::string RedisAuth;
static int RedisDbindex;
class <