【问题标题】:Redis: Can I store a c / c ++ structure type containing pointers to array in Redis?Redis:我可以在 Redis 中存储包含指向数组的指针的 c/c++ 结构类型吗?
【发布时间】:2020-06-14 10:11:12
【问题描述】:

我有一个这样的结构:

struct father {
    int child_n; //the number of children
    int *child_age; //childrens' age
}

我可以将这种具有可变长度数组的结构存储在Redis中吗?

【问题讨论】:

  • 我认为将 variable-length 数组嵌套为 Redis 列表可能会有所帮助
  • 我想我解决了。如果有人有同样的问题,请随时问我。

标签: c++ c database redis key-value


【解决方案1】:

我用嵌套的 *Redis 列表解决了这个问题,这里是一个例子:

#include <iostream>
#include <string>
#include <cstring>
#include <hiredis/hiredis.h>

typedef struct stu {
    int stu_num;
    int* stu_age;
} stu;

using namespace std;


void put_stus(string key, stu students) {
    // connect to Redis server
    redisContext *connect = redisConnect("127.0.0.1",6379);
    if(!connect) {
        cout << "connect to redis fail" << endl;
        exit(-1);
    }

    // upload students' age
    redisReply *r;
    for(int i = 0;i < students.stu_num;i++) {
        r = (redisReply *)redisCommand(connect, "RPUSH %s:age %b",key,&students.stu_age[i],sizeof(students.stu_age[i]));
        if(r->type == REDIS_REPLY_ERROR) {
            cout << "put_stus error" << endl;
            exit(-1);
        }
        freeReplyObject(r);
    }
    // upload struct
    r = (redisReply *)redisCommand(connect, "SET %s %b",key,&students,sizeof(students));
    if(r->type == REDIS_REPLY_ERROR) {
        cout << "put_stus error" <<endl;
        exit(-1);
    }
    freeReplyObject(r);
    redisFree(connect);
}

void get_stus(string key, stu *r_students) {
    // connect to Redis Server
    redisContext *connect = redisConnect("127.0.0.1",6379);
    if(!connect) {
        cout << "connect to redis fail" << endl;
        exit(-1);
    }

    // Get sturct
    redisReply *r;
    r = (redisReply *)redisCommand(connect, "GET %s",key);
    if(r->type == REDIS_REPLY_ERROR) {
        cout << "get_stus error" <<endl;
        exit(-1);
    }
    memcpy(r_students,r->str,r->len);
    // Get students' age
    r_students->stu_age = (int *)malloc(r_students->stu_num * sizeof(int));
    if(!r_students->stu_age) {
        cout << "malloc error" << endl;
        exit(-1);
    }
    r = (redisReply *)redisCommand(connect, "LRANGE %s:age 0 -1",key);
    if(r->type == REDIS_REPLY_ERROR) {
        cout << "get_stus error" <<endl;
        exit(-1);
    }
    else if(r->type == REDIS_REPLY_ARRAY) {
        for(int i = 0;i < r_students->stu_num;i++) {
            memcpy(r_students->stu_age + i, r->element[i]->str, r->element[i]->len);
        }
    }
    freeReplyObject(r);
    redisFree(connect);
}

int main() {
    stu stu_a,stu_b;
    stu_a.stu_num = 3;
    int ages[3] = {1,2,3};
    stu_a.stu_age = ages;

    put_stus("test1",stu_a);
    get_stus("test1",&stu_b);

    cout << "stu_b.stu_num is " << stu_b.stu_num << endl;
    for(int i = 0;i < stu_b.stu_num;i++) {
        cout << "stu_b.stu_age[" << i << "] is " << stu_b.stu_age[i] << endl;
    }

}

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2016-08-28
    • 2012-07-16
    相关资源
    最近更新 更多