• redis(4)-hiredis-API函数的调用


    1.下载安装编译
     

    windowslinux
    redis下载

    redis-plus-plus-master.zip文件的下载

    http://download.redis.io/releases/redis-7.2.0.tar.gz
    tar -zxvf  redis-7.2.0.tar.gz 
    //-z:gzip属性 ;-x:解压;-v:显示;-f:文件
    make
    make install  //make distclean
    hiredis下载 hiredis-master.zip文件的下载
    https://github.com/redis/hiredis.git
     
    安装与编译


    详见:redis(1)-hiredis-Windows下的编译-CSDN博客

    1.hiredis cmake
    2.redis-plus-plus cmake

     

    编译后生成的文件





    redis++.dll
    redis++.lib
    redis++_static.dll
    redis++_static.lib

    hiredisd.lib
    hiredisd.dll


     

    需要添加的头文件和源文件1.工程->属性->C++->附加包含目录:
    原始下载的hiredis-master下的D:\IPS3000\tangfiles\linux\centos-src-9-74\hiredis\hiredis-master
    2.工程->属性->链接器->输入
    ..\Debug\hiredisd.lib
    3.
     
    GitHub - redis/hiredis: Minimalistic C client for Redis >= 1.2
    重要的函数重要的函数就3个:
    redisContext *redisConnect(const char *ip, int port);
    void *redisCommand(redisContext *c, const char *format, ...);
    void freeReplyObject(void *reply);


     


        1.1 hiredis
        GitHub - sewenew/redis-plus-plus: Redis client written in C++icon-default.png?t=N7T8https://github.com/sewenew/redis-plus-plus
         1.2 redis官网下载+安装+编译
               如果下的是7的版本,不需要下hiredis.直接编译即可。
              

    1. $ wget http://download.redis.io/releases/redis-7.2.0.tar.gz
    2. $ tar -zxvf redis-7.2.0.tar.gz
    3. # -z:gzip x:解压 v:显示所有过程 f:文件
    4. $ cd redis-7.2.0
    5. $ make
    6. $ make install
    7. $ redis-server
    8. $ redis-cli
    9. 默认的安装目录:/usr/local/redis
    10. /usr/
    11. /usr/local/include
    12. /usr/local/lib
    13. /usr/local/lib/pkgconfig

      错误提示:
     1./bin/sh: 1: pkg-config: not found
       apt-get install pkg-config
        make distclean && make


    srv/ftp/

    1. #1.ubuntu 下安装tftp
    2. apt-get install vsftpd
    3. apt-get install ftp
    4. #2.修改配置文件
    5. vim /etc/vsftpd.conf
    6. local_enables=YES
    7. write_enable=YES
    8. anonymous_enable=YES
    9. anon_mkdir_write_enable=YES //允许匿名用户在FTP上创建目录
    10. anon_upload_enable=YES //允许匿名用户在FTP服务器上上传文件
    11. anon_other_write_enable=YES //开启匿名用户的其他写权限
    12. #不要用root 或者 anonymous访问


     

    1.1make 之前 

    5.0.147.2.0
    make
    1.deps/jemalloc 多了lib文件夹
    2.deps/lua/ 多了.o文件
    3.src 下多了.o文件
     
    make  test
    两个版本的对比:1.deps/fpconv
    2.deps/hdr_histogram
    3.deps/hiredis :ssl
                    sockcompat
    4.src/commands
    5.src/ 多了好多函数

     
    make install 

    之前bin,lib,inlcude3个文件夹下什么都没有。
    安装后:
    1.redis-5.2.0/src下有6个应用程序redis-server
    2./usr/local/bin 下有6个应用程序redis-server
    3./usr/local/include  无 
    4. /usr/local/lib        无
    /usr/local/lib/pkgconfig

    1.redis-7.2.0/src下有6个应用程序redis-server 只保留一个版本。
    2./usr/local/bin下有6个应用程序redis-server
    3./usr/local/include  无 
    4. /usr/local/lib       


    /usr/local/lib/pkgconfig

    所有文件在目录下:
    /home/tang/redis-5.0.14
    配置文件:/home/tang/redis-5.0.14/redis.conf
    日志文件:/var/log/redis/redis-server.log

    1.make 前后的对比

        

    2.5版本和7版本的对比


     


     
         


    2.启动服务器和客户端


        2.1 src下启动
        cd   redis7/src
               redis-server
               redis-cli
             

    1. ./src/redis-cli
    2. ​​​​​​​#带Host、Port、Auth连接: ./src/redis-cli -h 127.0.0.1 -p 6379 -a 123456


       2.2 修改环境变量
          /etc/profile



    3.redis 自带的hiredis基本用法
     

    https://github.com/redis/hiredis

    1. #1.创建一个放工程文件的文件夹 与redis安装包同级别
    2. mkdir /home/tang/redisprj
    3. #2.修改环境变量
    4. vim ~/.bashrc
    5. source .bashrc
    6. vim /etc/profile
    7. #2.1 动态库+静态库链接库文件
    8. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/tang/redis-7.2.0/deps/hiredis
    9. export LIBRARY_PATH=$LIBRARY_PATH
    10. #2.2 头文件
    11. #gcc头文件
    12. export C_INCLUDE_PATH=/usr/include
    13. #g++找到头文件的路径
    14. export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/include
    15. #2.3 可执行文件
    16. export PATH =$PATH:/home/tang/redis-7.2.0/src
    17. # 安装软件放在 /usr/local/include下

    1. 编写一个最基本的c文件。
           1.1编写test_main00.c,只输出一个字符串。

    1. #include
    2. int main()
    3. {
    4. printf("my test_main.c\n");
    5. return 0;
    6. }

          1.2编译运行,看是否正常输出。

    1. 编译运行
    2. gcc test_main00.c -o testmain # -o 编译后的应用程序名,可以有后缀也可以没有后缀
    3. ./testmain # 运行程序 ./表示当前文件夹

    2.编写1个调用hiredis set和get 的c文件
       2.1 编写文件

    1. //1.连接
    2. //2.发送
    3. //3.处理返回
    4. //4.清空
    5. #include
    6. #include
    7. #include
    8. #include
    9. void main()
    10. {
    11. redisContext *pc = redisConnect("127.0.0.1", 6379); //1.连接
    12. if (pc == NULL || pc->err)
    13. {
    14. if (pc)
    15. {
    16. printf("Error: %s\n", pc->errstr);
    17. }
    18. else
    19. {
    20. printf("Can't allocate redis context\n");
    21. }
    22. }
    23. redisReply *psetreply;
    24. psetreply= redisCommand(pc, "PING"); //2.发送PING
    25. printf("PING: %s\n", psetreply->str);
    26. freeReplyObject(psetreply);
    27. psetreply= redisCommand(pc, "SET %s %s", "foo", "hello world"); //2.发送SET
    28. printf("SET: %s\n", psetreply->str);
    29. freeReplyObject(psetreply);
    30. psetreply= redisCommand(pc, "SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5); //2.发送
    31. printf("SET (binary API): %s\n", psetreply->str);
    32. freeReplyObject( psetreply);
    33. // reply = redisCommand(context, "SET foo %s", value);
    34. // reply = redisCommand(context, "SET foo %b", value, (size_t) valuelen);
    35. // reply = redisCommand(context, "SET key:%s %s", myid, value);
    36. //4.清空
    37. redisFree(pc);
    38. //5.get();
    39. }

    1. //1.连接
    2. //2.发送
    3. //3.处理返回
    4. //4.清空
    5. void get()
    6. {
    7. redisContext *c = redisConnect("127.0.0.1", 6379); //1.连接
    8. if (c == NULL || c->err)
    9. {
    10. if (c)
    11. {
    12. printf("Error: %s\n", c->errstr);
    13. }
    14. else
    15. {
    16. printf("Can't allocate redis context\n");
    17. }
    18. }
    19. redisReply *pgetreply;
    20. pgetreply= redisCommand(c, "GET foo"); //2.GET foo
    21. printf("GET foo: %s\n", pgetreply->str);
    22. freeReplyObject(pgetreply);
    23. pgetreply= redisCommand(c, "INCR counter"); //2.GET
    24. printf("INCR counter: %lld\n", pgetreply->integer);
    25. freeReplyObject(pgetreply);
    26. pgetreply= redisCommand(c,"INCR counter");
    27. printf("INCR counter: %lld\n", pgetreply->integer);
    28. freeReplyObject(pgetreply);
    29. redisReply *reply ;
    30. /* Create a list of numbers, from 0 to 9 */
    31. reply = redisCommand(c,"DEL mylist");
    32. freeReplyObject(reply);
    33. for (int j = 0; j < 10; j++) {
    34. char buf[64];
    35. snprintf(buf,64,"%u",j);
    36. reply = redisCommand(c,"LPUSH mylist element-%s", buf);
    37. freeReplyObject(reply);
    38. }
    39. /* Let's check what we have inside the list */
    40. reply = redisCommand(c,"LRANGE mylist 0 -1");
    41. if (reply->type == REDIS_REPLY_ARRAY)
    42. {
    43. for ( int j = 0; j < reply->elements; j++) {
    44. printf("%u) %s\n", j, reply->element[j]->str);
    45. }
    46. }
    47. //4.清空
    48. redisFree(c);
    49. }

    2.2 编译输出
     

    gcc redis_mainsetget01.c -o  redis_mainsetget01 -L/home/tang/redis-7.2.0/deps/hiredis -lhiredis

      

    2.3 远程连接
     

    redis-cli -h [IP地址] -p [PORT端口号] -a [密码password]


     




     











       

  • 相关阅读:
    探究js数据类型和底层原理
    web前端期末大作业——基于html+css+javascript+jquery+bootstrap响应式户外旅游网站
    云游戏发行是什么?云游戏发行的演进历程
    MySQL 的索引和事务
    Svg Flow Editor 原生svg流程图编辑器(一)
    移动WEB开发之rem布局--rem适配方案
    解决vulhub漏洞环境下载慢卡死问题即解决docker-valhub漏洞环境下载慢的问题
    Eclipse配置tomcat
    企业园区办公室无线覆盖部署案例
    嵌入式软件工程师面试题——2025校招专题(三)
  • 原文地址:https://blog.csdn.net/aggie4628/article/details/133267838