• Sword redis C语言接口介绍


    hiredis安装
    hiredis是redis官方推荐的基于C接口的客户端组件,它提供接口,供c语言调用以操作数据库。
    在redis的源码包的deps/hiredis下就有它的源码
    安装方法,进入deps/hiredis目录,执行命令:
    make
    make install
    主要函数接口
        
    函数原型:redisContext *redisConnect(const char *ip, int port);
    说明:该函数用来连接redis数据库,参数为数据库的ip地址和端口,通常默认端口为6379。该函数返回一个redisContext对象。
    
    函数原型:void *redisCommand(redisContext *c, const char *format, …);
    说明:该函数执行redis命令,当然也包括由lua脚本组成的命令,返回redisReply对象。
    
    函数原型void freeReplyObject(void *reply);
    说明:释放redisCommand执行后返回的redisReply所占用的内存。
    
    函数原型:void redisFree(redisContext *c);
    说明:释放redisConnect()所产生的连接。
    //redisReply对象
    typedef struct redisReply {
         int type;                       // 返回结果类型
         long long integer;              // 返回类型为整型的时候的返回值
         size_t len;                     // 字符串长度
         char *str;                      // 返回错误类型或者字符类型的字符串 
         size_t elements;                // 返回数组类型时,元素的数量
         struct redisReply **element;    // 元素结果集合,redisReply对象 
     } redisReply;
    //返回结果类型
    REDIS_REPLY_STRING 1   //字符串
    REDIS_REPLY_ARRAY 2    //数组,多个reply,通过element数组以及elements数组大小访问
    REDIS_REPLY_INTEGER 3  //整型
    REDIS_REPLY_NIL 4      //空,没有数据
    REDIS_REPLY_STATUS 5   //状态,str字符串以及len
    REDIS_REPLY_ERROR 6    //错误,同STATUS
  • 相关阅读:
    一次网络IO优化的讨论
    服务器框架回顾
    一个小工具:DebugFile
    TPO-23 C2 Advice on choosing courses
    TPO-23 C1 Post a student announcement
    TPO-22 C2 Revise a music history paper
    TPO-22 C1 Complain about a biased article
    TPO-21 C2 Which elective courses to take
    TPO-20-Apply for the undergraduate research fund
    TPO-21 C1 Find a building for orientation
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9866082.html
Copyright © 2020-2023  润新知