项目中需要用到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__

基于hiredis,redis C客户端封装的更多相关文章

  1. redis web 客户端工具 redis-admin

    redis-admin是基于java的redis web客户端(redis client),以方便广大程序员使用redis为宗旨,集五种数据结构增删改查于一身. https://github.com/ ...

  2. Redis C客户端API - God's blog - 博客频道 - CSDN.NET

    Redis C客户端API - God's blog - 博客频道 - CSDN.NET Redis安装步骤: 1.redis server安装 wget http://redis.googlecod ...

  3. 基于表单数据的封装,泛型,反射以及使用BeanUtils进行处理

    在Java Web开发过程中,会遇到很多的表单数据的提交和对表单数据的处理.而每次都需要对这些数据的字段进行一个一个的处理就显得尤为繁琐,在Java语言中,面向对象的存在目的便是为了消除重复代码,减少 ...

  4. Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例

    本文目录 1. 摘要 2. Redis配置 3. RedisHelper 4.使用实例 5. 总结 1.  摘要 由于內存存取速度远高于磁盘读取的特性,为了程序效率提高性能,通常会把常用的不常变动的数 ...

  5. 【原创】自己动手写一个能操作redis的客户端

    引言 redis大家在项目中经常会使用到.官网也提供了多语言的客户端供大家操作redis,如下图所示 但是,大家有思考过,这些语言操作redis背后的原理么?其实,某些大神会说 只要按照redis的协 ...

  6. 基于开源SuperSocket实现客户端和服务端通信项目实战

    一.课程介绍 本期带给大家分享的是基于SuperSocket的项目实战,阿笨在实际工作中遇到的真实业务场景,请跟随阿笨的视角去如何实现打通B/S与C/S网络通讯,如果您对本期的<基于开源Supe ...

  7. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  8. 自荐RedisViewer有情怀的跨平台Redis可视化客户端工具

    # **自荐一个有情怀的跨平台Redis可视化客户端工具——RedisViewer**[转载自 最美分享Coder 2019-09-17 06:31:00](https://www.toutiao.c ...

  9. 自荐RedisViewer一个有情怀的跨平台Redis可视化客户端工具

    自荐一个有情怀的跨平台Redis可视化客户端工具--RedisViewer 转载自 最美分享Coder 2019-09-17 06:31:00 介绍 在以往的文章中曾经介绍过几款Redis的可视化工具 ...

随机推荐

  1. 使用JQuery 合并两个 json 对象

    一,保存object1和2合并后产生新对象,若2中有与1相同的key,默认2将会覆盖1的值 var object = $.extend({}, object1, object2); 二,将2的值合并到 ...

  2. Android AES加密报错处理:javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH

    一.问题说明 今天写AES加/解密功能的apk,设想是四个控件(测试用的,界面丑这种东西请忽略) 一个编缉框----用于输入要加密的字符串 一个文本框----用于输出加密后的字符串,和加密后点击解密按 ...

  3. C#代码中如何比较两个日期的大小?

    C#中比较两个日期大小的方法 /// <summary> /// 比较两个日期大小 /// </summary> /// <param name="dateSt ...

  4. application Initialization设置导致处理程序ExtensionlessUrlHandler-Integrated-4.0在其模块列表中有一个错误模块问题的解决

    HTTP 错误 500.21 - Internal Server Error 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“Ma ...

  5. day037 行记录的操作

    1.库操作 2.表操作 3.行操作 1.库操作 1)创建数据库 语法: create database 数据库名 charset utf8; 数据库命名规则: 由数字,字母,下划线,@,#,$ 等组成 ...

  6. Linux系统管理常用命令用法总结(1)

    1.usermod可用来修改用户帐号的各项设定. usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数& ...

  7. calc()

    width:calc(): cale(a)计算出表达式a的值. e.g: height:cale(100vh-200px):vh,是指CSS中相对长度单位,表示相对视口高度,通常视口长度单位会被分成1 ...

  8. Web开发框架DevExtreme发布v18.2.5|附下载

    DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...

  9. 查看电脑安装的JDK版本

    1.输入java -d32 -version,若出现如下界面则是32位 2.java -d64 -version,因为是32位的,所以结果如下

  10. Unity3d代码及效率优化总结

    1.PC平台的话保持场景中显示的顶点数少于200K~3M,移动设备的话少于10W,一切取决于你的目标GPU与CPU. 2.如果你用U3D自带的SHADER,在表现不差的情况下选择Mobile或Unli ...