• Hiredis快速入门


    Hiredis

    一、简介

    Hiredis库封装了Redis的C语言接口,专门用来操作Redis数据库

    Hiredis库下载

    库下载完成之后,利用makemake install将库安装到服务器,之后就可以在gcc编译时通过-lhiredis选项将库链接到可执行文件了。

    注:如果报出"…Not Found…"的错误,可以在root下编辑/etc/ld.so.conf,加上一行/usr/local/lib,再使用执行ldconfig命令刷新一下即可。

    二、API介绍

    1、建立连接

    redisContext *redisConnect(const char *ip, int port);
    
    • 1

    两个参数分别对应服务器的ip与端口。

    示例

    redisContext* redis = redisConnect("127.0.0.1", 6379);
    if (redis->err || redis == nullptr)
    {
        cout << "连接失败" << endl;
        return -1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注:如果是云服务器,需要在防火墙开放端口。


    2、写数据库

    void *redisCommand(redisContext *c, const char *format, ...);
    void freeReplyObject(void *reply);
    
    • 1
    • 2

    c是redisConnect返回的Redis实例,后面的可变参数是需要执行的命令,使用起来类似于printf()函数。

    注:该函数的返回值可以被redisReply*指针接收,用来获取命令执行结果,在使用结束后必须调用freeReplyObject()进行释放!

    示例

    char value[] = "hello redis";
    redisReply *reply = (redisReply *)redisCommand(redis, "ping");
    redisReply *reply = (redisReply *)redisCommand(redis, "set key %s", value);
    
    • 1
    • 2
    • 3
    redisReply结构体
    typedef struct redisReply
    {
        int type;                    // 返回的信息类型
        long long integer;           // 当type为REDIS_REPLY_INTEGER时有效
        double dval;                 // 当type为REDIS_REPLY_DOUBLE时有效
        size_t len;                  // 字符串长度
        char *str;                   // 当字段为REDIS_REPLY_ERROR, REDIS_REPLY_STRING
                                     // REDIS_REPLY_VERB, REDIS_REPLY_DOUBLE (in additional to dval),
                                     // 和 REDIS_REPLY_BIGNUM时有效
        char vtype[4];               // 返回值为REDIS_REPLY_VERB时有效
        size_t elements;             // element数组的元素个数
        struct redisReply **element; // 当type为REDIS_REPLY_ARRAY时有效
    } redisReply;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    其中,信息类型有:

    #define REDIS_REPLY_STRING 1
    #define REDIS_REPLY_ARRAY 2
    #define REDIS_REPLY_INTEGER 3
    #define REDIS_REPLY_NIL 4
    #define REDIS_REPLY_STATUS 5
    #define REDIS_REPLY_ERROR 6
    #define REDIS_REPLY_DOUBLE 7
    #define REDIS_REPLY_BOOL 8
    #define REDIS_REPLY_MAP 9
    #define REDIS_REPLY_SET 10
    #define REDIS_REPLY_ATTR 11
    #define REDIS_REPLY_PUSH 12
    #define REDIS_REPLY_BIGNUM 13
    #define REDIS_REPLY_VERB 14
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3、释放连接

    void redisFree(redisContext *c);
    
    • 1

    释放redisConnect()创建的redis实例。

    三、应用示例

    #include 
    #include 
    #include "hiredis/hiredis.h"
    
    using namespace std;
    
    int main()
    {
        redisContext *redis = redisConnect("101.35.158.44", 6379);
        if (redis->err || redis == nullptr)
        {
            cout << "连接失败" << endl;
            return -1;
        }
        else
        {
            cout << "连接成功" << endl;
        }
    
        string key = "testkey";
        string value = "hello redis!";
        // 密码验证
        redisCommand(redis, "auth redis123456");
        // 设置键值对
        redisReply *reply = (redisReply *)redisCommand(redis, "set %s %s", key.c_str(), value.c_str());
        // 获取键值对
        reply = (redisReply *)redisCommand(redis, "get %s", key.c_str());
        cout << reply->str << endl;
        // 利用 keys * 获取所有键值对
        reply = (redisReply *)redisCommand(redis, "keys *");
        size_t n = reply->elements;
        for (int i = 0; i < n; ++i)
        {
            cout << reply->element[i]->str << endl;
        }
    
        freeReplyObject(reply);
        redisFree(redis);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    Linux进程内核栈
    【蜂鸟E203的FPGA验证】Chap.7 Vivado综合与性能分析-建立Vivado工程
    2.13 集成学习
    [CISCN2019 华北赛区 Day2 Web1]Hack World1
    巨细!Python爬虫详解
    图形学学习笔记
    Kubernetes IPVS和IPTABLES
    【开发记录】利用QT读取Excel并写入数据
    4. 执行引擎
    理解渲染,吃透渲染,你应该知道的Android渲染优化小技巧
  • 原文地址:https://blog.csdn.net/Wyf_Fj/article/details/126389909