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;  
}

你可能感兴趣的:(c++,redis,开发语言)