• C++用hiredis访问redis


    C++用hiredis访问redis

    常用函数

    1)redisContext* redisConnect(const char *ip, int port)
    用于建立与Redis数据库的连接。

    2)void redisFree(redisContext *c)
    用于释放与Redis数据库的连接。

    3)redisReply* redisCommand(redisContext *c, const char *cmd)
    用于发送命令到Redis服务器并等待服务器响应

    4)redisReply* redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen)
    用于发送带有参数的命令到Redis服务器并等待服务器响应。

    5)void freeReplyObject(redisReply r)
    用于释放Redis服务器的响应结果。

    6)redisReply* redisGetReply(redisContext *c, void **replyValue)
    用于从Redis服务器获取单个响应结果。

    7)redisReply* redisGetReplyFromReader(redisContext *c)
    用于从Redis读取器中获取下一个响应结果。

    8)int redisGetReplyStatus(redisContext *c, const redisReply *r)
    用于获取Redis响应的状态码。

    9)const char* redisGetReplyString(redisContext *c, const redisReply *r)
    用于获取Redis响应的字符串值。

    10)int redisGetReplyInt(redisContext *c, const redisReply *r)
    用于获取Redis响应的整数值。

    11)double redisGetReplyDouble(redisContext *c, const redisReply *r)
    用于获取Redis响应的浮点数值。

    12)void* redisGetReplyData(redisContext *c, const redisReply *r, size_t *len)
    用于获取Redis响应的数据和数据长度。

    应用例子

    #include   
    #include   
      
    int main() {  
        // 创建连接  
        redisContext *c = redisConnect("127.0.0.1", 6379);  
        if (c == NULL || c->err) {  
            std::cout << "Connection error: " << c->errstr << std::endl;  
            return 1;  
        }  
      
        // 设置数据库名称  
        const char *dbName = "mydb";  
      
        // 创建数据库  
        if (redisDbCreate(c, dbName) != REDIS_OK) {  
            std::cout << "Failed to create database: " << c->errstr << std::endl;  
            return 1;  
        }  
      
        // 增加数据  
        const char *key = "key1";  
        const char *value = "value1";  
        if (redisDbKeySet(c, dbName, key, value) != REDIS_OK) {  
            std::cout << "Failed to set key-value pair: " << c->errstr << std::endl;  
            return 1;  
        }  
      
        // 获取数据  
        redisReply *reply = redisDbKeyGet(c, dbName, key);  
        if (reply == NULL) {  
            std::cout << "Failed to get key-value pair: " << c->errstr << std::endl;  
            return 1;  
        }  
        std::cout << "Value for key '" << key << "': " << reply->str << std::endl;  
        freeReplyObject(reply);  
      
        // 删除数据  
        if (redisDbKeyDel(c, dbName, key) != REDIS_OK) {  
            std::cout << "Failed to delete key-value pair: " << c->errstr << std::endl;  
            return 1;  
        }  
      
        // 修改数据  
        const char *new_value = "value2";  
        if (redisDbKeySet(c, dbName, key, new_value) != REDIS_OK) {  
            std::cout << "Failed to set key-value pair: " << c->errstr << std::endl;  
            return 1;  
        }  
      
        // 查询数据  
        reply = redisDbKeysGet(c, dbName, NULL, 0);  
        if (reply == NULL || reply->type == REDIS_REPLY_NIL) {  
            std::cout << "No keys found in database." << std::endl;  
            freeReplyObject(reply);  
        } else {  
            std::cout << "Found keys in database:" << std::endl;  
            while (reply != NULL && reply->type != REDIS_REPLY_NIL) {  
                std::cout << reply->str << std::endl;  
                freeReplyObject(reply);  
                reply = reply->next;  
            }  
        }  
      
        // 关闭连接  
        redisFree(c);  
      
        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
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
  • 相关阅读:
    Cheat Engine.exe修改植物大战僵尸阳光与冷却
    PNAS:睡眠的fMRI频谱特征
    网络技术-Cisco路由器
    计算机毕业设计Java校园面包超市系统(源码+系统+mysql数据库+Lw文档)
    【力扣练习】找一个字符串中不含有重复字符的最长字串的长度
    Tomcat 源码解析一类加载器-狂狮吟
    【无标题】
    LeetCode-1106. 解析布尔表达式【栈,递归,字符串】
    基于PaddleOCR的集装箱箱号检测识别
    IP代理|一文看懂IPv4与IPv6
  • 原文地址:https://blog.csdn.net/techenliu/article/details/133635199