• 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
  • 相关阅读:
    ts重点学习37-可选属性和只读属性
    HTML+CSS+JavaScript仿京东购物商城网站 web前端制作服装购物商城 html电商购物网站
    vue项目打包时如何将静态文件打包到一个单独的文件夹
    华为云云耀云服务器L实例评测|云耀云服务器L实例部署Gogs服务器
    基于小程序实现的惠农小店系统设计与开发
    1999-2018年地级市经济增长数据
    【数据结构】&&【C++】红黑树RBTree的模拟实现(平衡搜索二叉树)
    【Spring Security】安全框架学习(一)
    SpringBoot整合SpringSession实现分布式登录
    Google Earth Engine(GEE)—— 各矿区时序NDVI变化图(包含具体的运行函数)
  • 原文地址:https://blog.csdn.net/techenliu/article/details/133635199