工作须要对Hiredis进行了简单封装,实现功能:

1、API进行统一,对外仅仅提供一个接口。

2、屏蔽上层应用对连接的细节处理;

3、底层採用队列的方式保持连接池,保存连接会话。

4、重连时採用时间戳进行控制,每隔一定时间(3s)重连一次。防止频繁重试造成的不必要浪费。

先看一下Hiredis的经常使用数据结构与API:

//hiredis/hiredis.h
/* Context for a connection to Redis */
typedef struct redisContext {
int err; /* Error flags, 0 when there is no error */
char errstr[128]; /* String representation of error when applicable */
int fd;
int flags;
char *obuf; /* Write buffer */
redisReader *reader; /* Protocol reader */
} redisContext; /* This is the reply object returned by redisCommand() */
#define REDIS_REPLY_STRING 1
#define REDIS_REPLY_ARRAY 2
#define REDIS_REPLY_INTEGER 3
#define REDIS_REPLY_NIL 4
#define REDIS_REPLY_STATUS 5
#define REDIS_REPLY_ERROR 6
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
int len; /* Length of string */
char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply; redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv);
void redisFree(redisContext *c);

以下直接上封装后的代码:

class KGRedisClient
{
public:
KGRedisClient(string ip, int port, int timeout = 2000);
virtual ~KGRedisClient(); bool ExecuteCmd(const char *cmd, size_t len, string &response);
redisReply* ExecuteCmd(const char *cmd, size_t len); private:
int m_timeout;
int m_serverPort;
string m_setverIp;
CCriticalSection m_lock;
std::queue<redisContext *> m_clients; time_t m_beginInvalidTime;
static const int m_maxReconnectInterval = 3; redisContext* CreateContext();
void ReleaseContext(redisContext *ctx, bool active);
bool CheckStatus(redisContext *ctx);
}; KGRedisClient::KGRedisClient(string ip, int port, int timeout)
{
m_timeout = timeout;
m_serverPort = port;
m_setverIp = ip; m_beginInvalidTime = 0;
} KGRedisClient::~KGRedisClient()
{
CAutoLock autolock(m_lock);
while(!m_clients.empty())
{
redisContext *ctx = m_clients.front();
redisFree(ctx);
m_clients.pop();
}
} bool KGRedisClient::ExecuteCmd(const char *cmd, size_t len,string &response)
{
redisReply *reply = ExecuteCmd(cmd, len);
if(reply == NULL) return false; boost::shared_ptr<redisReply> autoFree(reply, freeReplyObject);
if(reply->type == REDIS_REPLY_INTEGER)
{
response = _IntToStrA(reply->integer);
return true;
}
else if(reply->type == REDIS_REPLY_STRING)
{
response.assign(reply->str, reply->len);
return true;
}
else if(reply->type == REDIS_REPLY_STATUS)
{
response.assign(reply->str, reply->len);
return true;
}
else if(reply->type == REDIS_REPLY_NIL)
{
response = "";
return true;
}
else if(reply->type == REDIS_REPLY_ERROR)
{
response.assign(reply->str, reply->len);
return false;
}
else if(reply->type == REDIS_REPLY_ARRAY)
{
response = "Not Support Array Result!!!";
return false;
}
else
{
response = "Undefine Reply Type";
return false;
}
} redisReply* KGRedisClient::ExecuteCmd(const char *cmd, size_t len)
{
redisContext *ctx = CreateContext();
if(ctx == NULL) return NULL; redisReply *reply = (redisReply*)redisCommand(ctx, "%b", cmd, len); ReleaseContext(ctx, reply != NULL); return reply;
} redisContext* KGRedisClient::CreateContext()
{
{
CAutoLock autolock(m_lock);
if(!m_clients.empty())
{
redisContext *ctx = m_clients.front();
m_clients.pop(); return ctx;
}
} time_t now = time(NULL);
if(now < m_beginInvalidTime + m_maxReconnectInterval) return NULL; struct timeval tv;
tv.tv_sec = m_timeout / 1000;
tv.tv_usec = (m_timeout % 1000) * 1000;;
redisContext *ctx = redisConnectWithTimeout(m_setverIp.c_str(), m_serverPort, tv);
if(ctx == NULL || ctx->err != 0)
{
if(ctx != NULL) redisFree(ctx); m_beginInvalidTime = time(NULL); return NULL;
} return ctx;
} void KGRedisClient::ReleaseContext(redisContext *ctx, bool active)
{
if(ctx == NULL) return;
if(!active) {redisFree(ctx); return;} CAutoLock autolock(m_lock);
m_clients.push(ctx);
} bool KGRedisClient::CheckStatus(redisContext *ctx)
{
redisReply *reply = (redisReply*)redisCommand(ctx, "ping");
if(reply == NULL) return false; boost::shared_ptr<redisReply> autoFree(reply, freeReplyObject); if(reply->type != REDIS_REPLY_STATUS) return false;
if(strcasecmp(reply->str,"PONG") != 0) return false; return true;
}

稍加解释:

成员变量:m_clients用于保存连接池。

成员变量:m_beginInvalidTime、m_maxReconnectInterval 用于控制断掉时的频繁连接。

对外API:ExecuteCmd(const char *cmd, string &response);

最新文章

  1. 运行python程序
  2. R与Java
  3. Android上dip、dp、px、sp等单位说明
  4. 基于vitamio的网络电视直播源码
  5. 说说C#的async和await(转)
  6. 《Head First Servlet JSP》学习笔记一
  7. thinkphp ajax 实例 实现
  8. mysql通过SOURCE导入SQL时报错处理
  9. 《数字图像处理原理与实践(MATLAB版)》一书之代码Part6
  10. IPhone开发“此证书是由未知颁发机构签名”解决办法
  11. zabbix配置邮件告警
  12. 正则表达式的一些探索(偏JavaScript)
  13. 三层实现办公用品表CRUD(全过程)-ASP
  14. jvm--深入理解java虚拟机 精华总结(面试)(转)
  15. 我和ARM的那些事儿3 beep之旅,最详细的ARM裸机工程设置
  16. 王者荣耀交流协会 - 第7次Scrum会议(第三周)
  17. 在MVC5中使用Ninject 依赖注入
  18. 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar &amp;&amp; Malek and Summer Semester
  19. $MySQL常用操作命令备忘
  20. MVC5中的路由

热门文章

  1. pandas dataframe 做机器学习训练数据=》直接使用iloc或者as_matrix即可
  2. Python: PS 图像调整--黑白
  3. [poj 2912] Rochambeau 解题报告 (带权并查集)
  4. 题解 P1179 【数字统计】
  5. 去掉vs2010字符串下红色波浪线
  6. 解决Struts中文乱码问题总结
  7. ZOJ 2588 Burning Bridges(无向连通图求割边)
  8. 一张图片让你了解android的事件分发机制
  9. 《AndroidStudio每日一贴》7. 怎样将本地变更文件移到其他的changelist?
  10. 独立python环境之virtualenv和virtualenvwrapper