首页 > 代码库 > Redis学习之路(003)- hiredis安装及测试

Redis学习之路(003)- hiredis安装及测试

一. hiredis下载地址及C API  github下载:https://github.com/redis/hiredis

安装脚本:

1 #!/bin/zsh
2 git clone https://github.com/redis/hiredis.git
3 cd hiredis
4 make
5 make install

二. 文件pc,我是把libhiredis.so放到/usr/local/lib/中,把hiredis.h放到/usr/local/inlcude/hiredis/中的。

三. redis启动关闭

    服务启动:redis-server   

    客户端连接:redis-cli

    客户端关闭:redis-cli shutdown
四. hiredis测试:
 1 #include <stdio.h> 
 2 #include <hiredis/hiredis.h> 
 3 int main() 
 4 { 
 5      redisContext *conn  = redisConnect("127.0.0.1",6379); 
 6      if(conn != NULL && conn->err) 
 7      {   
 8          printf("connection error: %s\n",conn->errstr); 
 9          return 0; 
10      }   
11      redisReply *reply = (redisReply*)redisCommand(conn,"set foo 1234"); 
12      freeReplyObject(reply); 
13              
14      reply = redisCommand(conn,"get foo"); 
15      printf("%s\n",reply->str); 
16      freeReplyObject(reply); 
17              
18      redisFree(conn); 
19      return 0; 
20 }

编译:gcc -o  redis_test redis_test.c -L/usr/local/lib/ -lhiredis

 

Redis学习之路(003)- hiredis安装及测试