Linux下C++访问Redis

Linux下C++访问Redis

    • 编译redis
    • 例子
    • gcc编译

编译redis

开发包的下载和编译在其他文章中已经介绍,不熟悉的可以转至[Linux下redis环境搭建]学习。(https://blog.csdn.net/bajianxiaofendui/article/details/87642244)
编译完成后进入redis-2.8.3/deps/hiredis目录,可以看到下面已经编译出了libhiredis.a静态库:
Linux下C++访问Redis_第1张图片

例子

编写了一个访问redis,进行简单的string读写操作


#include  
#include  
#include  
#include  
#include  
#include  
#include  
   
   
int main() 
{ 
    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"); 

    redisReply *reply = (redisReply *)redisCommand(c, "AUTH %s", "password"); //password为redis服务的密码
    if (reply->type == REDIS_REPLY_ERROR)
    {
        printf("Redis认证失败!\n");
    } 
    else
    {
        printf("Redis认证成功!\n");
    }
    freeReplyObject(reply);
       
    const char* command1 = "set stest1 value1"; 
    redisReply* 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); 
    
    return 0; 
} 



在redis-2.8.3同级目录新建一个src目录:
在这里插入图片描述
将RedisTest.cpp放入src目录,有两种方式:
1,通过xftp传输
2,输入命令vim RedisTest.cpp,将代码拷贝进去,再输入:wq!保存退出。

gcc编译

输入命令开始编译RedisTest.cpp:

g++   RedisTest.cpp -o RedisTest -I../redis-2.8.3/deps  -L../redis-2.8.3/deps/hiredis -lhiredis

g++:表示编译的是c++文件
RedisTest.cpp:源文件
RedisTest:输出文件
-I…/redis-2.8.3/deps:Include目录为当前目录的上级目录下的redis-2.8.3/deps
-L…/redis-2.8.3/deps/hiredis:表示链接库目录为当前目录的上级目录下的redis-2.8.3/deps/hiredis
-lhiredis:表示静态库为libhiredis,(分开看:-l是链接,后面的才是库名,二者拼接成libhiredis.o)
执行上述编译命令,再同级目录下生成RedisTest,输入执行命令:

./RedisTest

运行结果为:
Linux下C++访问Redis_第2张图片
上述gcc命令可以用makefile来代替,具体见另一篇博客makefile通用模板

你可能感兴趣的:(Redis)