栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux hiredis 编译安装

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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  
#include  
#include  
#include  
#include  
#include  
#include  
   
void doTest() 

    //redis默认监听端口为6387 可以再配置文件中修改 
    redisContext* c = redisConnect("127.0.0.1", 6379); 
    if ( c->err) 
    { 
        redisFree(c); 
        printf("Connect to redisServer failen"); 
        return ; 
    } 
    printf("Connect to redisServer Successn"); 
    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 failuren"); 
        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 %sn", 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链接:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/460201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号