linux hiredis 编译安装

下载hiredis安装包地址如下:

链接:https://pan.baidu.com/s/1WCJIyj8ofCoCTC7hGWS-Rw 
提取码:b6fq

这里用到了回调所以需安装libevent下载如下:

链接:https://pan.baidu.com/s/1rWSgXQGNNnVFRebUazJxNg 
提取码:2aac

执行命令:

cd hiredis

make

make install

vi /etc/ld.so.conf

添加hiredis动态库路径

ldconfig

libevent安装自行百度

测试代码如下:

#include <stdio.h> 
#include <stdlib.h> 
#include <stddef.h> 
#include <stdarg.h> 
#include <string.h> 
#include <assert.h> 
#include <hiredis/hiredis.h> 
   
void doTest() 

    //redis默认监听端口为6387 可以再配置文件中修改 
    redisContext* c = redisConnect("127.0.0.1", 6379); 
    if ( c->err) 
    { 
        redisFree(c); 
        printf("Connect to redisServer faile\n"); 
        return ; 
    } 
    printf("Connect to redisServer Success\n"); 
    char user_password[]="123456";  //redis有密码需要进行身份验证
    redisReply* r=(redisReply*)redisCommand(c, "AUTH %s",user_password);       
    freeReplyObject(r); 
    const char* command1 = "set stest1 value1"; 
    r = (redisReply*)redisCommand(c, command1); 
       
    if( NULL == r) 
    { 
        printf("Execut command1 failure\n"); 
        redisFree(c); 
        return; 
    } 
    if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0)) 
    { 
        printf("Failed to execute command[%s]\n",command1); 
        freeReplyObject(r); 
        redisFree(c); 
        return; 
    }    
    freeReplyObject(r); 
    printf("Succeed to execute command[%s]\n", command1); 
       
    const char* command2 = "strlen stest1"; 
    r = (redisReply*)redisCommand(c, command2); 
    if ( r->type != REDIS_REPLY_INTEGER) 
    { 
        printf("Failed to execute command[%s]\n",command2); 
        freeReplyObject(r); 
        redisFree(c); 
        return; 
    } 
    int length =  r->integer; 
    freeReplyObject(r); 
    printf("The length of 'stest1' is %d.\n", length); 
    printf("Succeed to execute command[%s]\n", command2); 
       
       
    const char* command3 = "get stest1"; 
    r = (redisReply*)redisCommand(c, command3); 
    if ( r->type != REDIS_REPLY_STRING) 
    { 
        printf("Failed to execute command[%s]\n",command3); 
        freeReplyObject(r); 
        redisFree(c); 
        return; 
    } 
    printf("The value of 'stest1' is %s\n", r->str); 
    freeReplyObject(r); 
    printf("Succeed to execute command[%s]\n", command3); 
       
    const char* command4 = "get stest2"; 
    r = (redisReply*)redisCommand(c, command4); 
    if ( r->type != REDIS_REPLY_NIL) 
    { 
        printf("Failed to execute command[%s]\n",command4); 
        freeReplyObject(r); 
        redisFree(c); 
        return; 
    } 
    freeReplyObject(r); 
    printf("Succeed to execute command[%s]\n", command4);    
       
       
    redisFree(c); 
       

   
int main() 

    doTest(); 
    return 0; 

编译命令:

/usr/bin/gcc redis_connect.c -o redis_connect -lhiredis

执行结果如下:

Connect to redisServer Success
Succeed to execute command[set stest1 value1]
The length of 'stest1' is 6.
Succeed to execute command[strlen stest1]
The value of 'stest1' is value1
Succeed to execute command[get stest1]
Succeed to execute command[get stest2]
安装成功

api链接:

GitHub - 重新使用/雇用: 雷迪斯>= 1.2 的极简 C 客户端 hiredis的Readme就是很好的api介绍

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫回首�

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值