项目中需要用到redis就封装了一下,基于hiredis,只封装了string和哈希的部分方法。编译时加入-D__USER_LOCK__添加线程安全。

suntelRedisCli.h


#ifndef __SUNTELREDISCLI_H__
#define __SUNTELREDISCLI_H__ #include <hiredis/hiredis.h> #ifdef __USE_LOCK__
#include <pthread.h>
#endif #define REDIS_OK 0
#define REDIS_ERROR 0xFFFFFFFF class CRedisCli
{
public:
    CRedisCli();
    ~CRedisCli();
    /*
    * 连接到redis server
    */
    int ConnectDB(const char *hostName,const int port);
    int ConnectDB();
    int Auth(const char *password);     /*
    * 系统管理
    */
    int SelectDB(int no);
    int FlushDB();
    int FlushAll();     /*
    * string类
    */
    int Set(const char *key,const char *format,...);
    int Get(const char *key,char *value);
    int Del(const char *key);     /*
    * 哈希类
    */
    int HMSet(const char *key,const char *format,...);
    int HMGet(const char *key,size_t *elements,char **element);//返回element     int HSetField(const char *key,const char *field,const char *format,...);
    int HGetField(const char *key,const char *field,char *value);     int HDel(const char *key); private:
    #ifdef __USE_LOCK__
    pthread_mutex_t m_mutex;
    #endif
    redisContext* m_context;
    redisReply*   m_reply;
    char m_redisHost[32];
    int  m_redisPort;
    char m_redisPswd[32];
}; #endif

suntelRedisCli.cpp


#include <string.h>
#include <stdio.h>
#include "suntelRedisCli.h" CRedisCli::CRedisCli()
{
    m_context = NULL;
    m_reply = NULL;
    strcpy(m_redisHost,"127.0.0.1");
    m_redisPort = 6379;
    memset(m_redisPswd,0x00,sizeof(m_redisPswd));
#ifdef __USE_LOCK__
    pthread_mutex_init(&m_mutex, NULL);
#endif
} CRedisCli::~CRedisCli()
{
    if(m_context)
        redisFree(m_context);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_destroy(&m_mutex);
#endif
} int CRedisCli::ConnectDB(const char *hostName,const int port)
{
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    strncpy(m_redisHost,hostName,sizeof(m_redisHost)-1);
    m_redisPort = port;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif     return ConnectDB();
} int CRedisCli::ConnectDB()
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_context = redisConnect(m_redisHost,m_redisPort);
    if(m_context == NULL || m_context->err)
    {
        if(m_context){
            fprintf(stderr,"Connection error: %s\n",m_context->errstr);
            redisFree(m_context);
            m_context = NULL;
        }
        else{
            fprintf(stderr,"Connection error: can't allocate redis context\n");
        }
        ret = REDIS_ERROR;
    }
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
}
int CRedisCli::Auth(const char *password)
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif     strncpy(m_redisPswd,password,sizeof(m_redisPswd)-1);
    m_reply = (redisReply *)redisCommand(m_context,"auth %s",m_redisPswd);     if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::SelectDB(int no)
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif     m_reply = (redisReply *)redisCommand(m_context,"select %d",no);     if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::FlushDB()
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisCommand(m_context,"flushdb");     if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::FlushAll()
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisCommand(m_context,"flushall");     if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::Set(const char *key,const char *format,...)
{
    va_list ap;
    va_start(ap,format);
    char buf[512]={0x00};
    snprintf(buf,512,"SET %s %s",key,format);     int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisvCommand(m_context,buf,ap);
    va_end(ap);
    if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::Get(const char *key,char * value)
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisCommand(m_context,"GET %s",key);     if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     if(m_reply->type == REDIS_REPLY_STRING)
    {
        strncpy(value,m_reply->str,m_reply->len);
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::Del(const char *key)
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisCommand(m_context,"DEL %s",key);     if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::HMSet(const char *key,const char *format,...)
{
    va_list ap;
    va_start(ap,format);
    char buf[512]={0x00};
    snprintf(buf,512,"HMSet %s %s",key,format);
    // printf("%s\n",buf);
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisvCommand(m_context,buf,ap);
    va_end(ap);     if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::HMGet(const char *key,size_t *elements,char **element)
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisCommand(m_context,"HGETALL %s",key);
    if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     if(m_reply->type == REDIS_REPLY_ARRAY)
    {
        int i = 0;
        for(i=0;i<m_reply->elements;i++)
        {
            strncpy(element[i],m_reply->element[i]->str,m_reply->element[i]->len);
        }
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::HSetField(const char *key,const char *field,const char *format,...)
{
    va_list ap;
    va_start(ap,format);
    char buf[512]={0x00};
    snprintf(buf,512,"HSet %s %s %s",key,field,format);
    //printf("%s\n",buf);
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisvCommand(m_context,buf,ap);
    va_end(ap);     if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::HGetField(const char *key,const char *field,char *value)
{
    int ret = REDIS_OK;
#ifdef __USE_LOCK__
    pthread_mutex_lock(&m_mutex);
#endif
    m_reply = (redisReply *)redisCommand(m_context,"HGET %s %s",key,field);
    if( m_reply == NULL || m_reply->type == REDIS_REPLY_ERROR)
    {
        if(m_reply)
        {
            fprintf(stderr,"redis error: %s\n",m_reply->str);
            freeReplyObject(m_reply);
            m_reply = NULL;
        }
        else
        {
            fprintf(stderr,"redis error with null m_reply");
        }
#ifdef __USE_LOCK__
        pthread_mutex_unlock(&m_mutex);
#endif
        return REDIS_ERROR;
    }     if(m_reply->type == REDIS_REPLY_STRING)
    {
        strncpy(value,m_reply->str,m_reply->len);
    }     freeReplyObject(m_reply);
    m_reply = NULL;
#ifdef __USE_LOCK__
    pthread_mutex_unlock(&m_mutex);
#endif
    return ret;
} int CRedisCli::HDel(const char *key)
{
    return Del(key);
} ``
编译:

g++ -shared --fIPC suntelRedisCli.cpp -o lsuntelRedisCli -lhiredis

g++ -shared --fIPC suntelRedisCli.cpp -o lsuntelRedisCli -lhiredis -D__USE_LOCK__

最新文章

  1. vector容器删除某些元素且释放内存
  2. mybatis-缓存1
  3. yii2史上最简单式安装教程,没有之一
  4. 网页之间信息传递方式(Cookie,Session)
  5. [PaPaPa][需求说明书][V0.1]
  6. PowerDesigner15下载、安装以及破解
  7. Dom文档模型
  8. (转载)javascript转自世纪流年blog
  9. svn , github工作流
  10. LINUX更改时区和时间
  11. VS2013编译libcurl
  12. DevExpress中SearchLookUpEdit用法总结
  13. 第一次作业:基于Linux操作系统深入源码进程模型分析
  14. 【数据结构】B-Tree, B+Tree, B*树介绍
  15. node.js官方文档解析 01—assert 断言
  16. rem+media+jquery布局结局方案
  17. bzoj 3262 陌上花开 - CDQ分治 - 树状数组
  18. P4145 上帝造题的七分钟2 / 花神游历各国
  19. 在weblogic下部署找不到授权文件的解决方法
  20. Windows 中 .\ 和 ..\ 的区别

热门文章

  1. Docker 容器和镜像使用
  2. ActiveMQ使用例子
  3. java反序列化漏洞原理研习
  4. MySQL数据库安装与配置鸡汤
  5. swiper添加了自动滚动效果,然后用手指划过页面,发现自动滚动效果不生效了
  6. samba共享文件夹设置
  7. HTML(三)选择器--复杂选择器
  8. flask项目结构(五)使用数据库
  9. EF-获取自增ID值
  10. 四:FAQ附录(容器交互,镜像交互,镜像导出)