HiRedis 接口封装(非线程安全)

本文介绍了在Visual Studio 2012环境下,由于对C++11支持不足,采用微软win版本的redis2.6进行接口封装的详细过程,包括`redisw.h`、`redisw.cpp`和`biostream.h`三个关键文件的使用示例。
摘要由CSDN通过智能技术生成

因项目使用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 <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值