0

下面是我的示例代码,我通过它尝试使用 IgniteServer 对hiredis 客户端进行基准测试,以在序列化后存储和检索 C++ 类对象。

#include <cassert>
#include <sstream>
#include <string>
#include <unordered_map>
#include <time.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <sstream>
#include <chrono>
#include <time.h>


#include <hiredis/hiredis.h>
#include <cereal/archives/binary.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/types/unordered_map.hpp>

using namespace std::chrono;
struct Person
{
    friend class cereal::access;

    template <typename Archive>
        void serialize(Archive& archive) {
        //  archive(orgId,firstName, lastName,resume,salary,other);
        archive & orgId & firstName & lastName & resume & salary & other;
        }
    Person() {}

    Person(int32_t orgId, const std::string& firstName,
            const std::string& lastName, const std::string& resume, double sal, const std::string& other) :
        orgId(orgId),
        firstName(firstName),
        lastName(lastName),
        resume(resume),
        salary(sal),
        other(other)
    {
        // No-op.
    }
    int64_t orgId;
    std::string firstName;
    std::string lastName;
    std::string resume;
    double salary;
    std::string other;
};

auto redis_context = redisConnect("127.0.0.1", 6379);
int _set(int32_t key, Person p){
        std::ostringstream oss;
    cereal::BinaryOutputArchive archive{oss};
    archive(p.orgId,p.firstName, p.lastName,p.resume,p.salary,p.other);
    const auto set_reply =
        redisCommand(redis_context, "SET %ld %b",(long)key,oss.str().c_str(), oss.str().length());
    freeReplyObject(set_reply);

}
int _get(int32_t key){
    const auto get_reply = 
        static_cast<redisReply*>(redisCommand(redis_context, "GET %ld",(long)key));
    std::string repr{get_reply->str, static_cast<size_t>(get_reply->len)};
    if(static_cast<size_t>(get_reply->len) <= 0) {
        std::cout << "No key is matching" << std::endl;
        return -1;
    }   
    std::istringstream iss{repr};
    cereal::BinaryInputArchive input(iss);
    Person g;
    input(g);
        std::cout << g.orgId << " Name: " << g.firstName << " Last: " << g.lastName << "Resume: " << g.resume<< " Salary: " << g.salary << " Other: " << g.other <<std::endl;
    freeReplyObject(get_reply);
    iss.clear();
    //        redisFree(redis_context);

}
int _remove(int32_t key){
    const auto del_reply =
        static_cast<redisReply*>(redisCommand(redis_context, "DEL %ld",(long)key));
    freeReplyObject(del_reply);

    _get(key);
}

int main(int argc, char **argv) {


    int start =1 ; 
    int range =3;
    std::cout << "Starting Redis Testing " << std::endl;
    clock_t begin_time = clock();
    high_resolution_clock::time_point t1 = high_resolution_clock::now();

**//Person P{10,"John","Cena","Analyst",450000,"Summa"};// Works Fine when object is created in this way**
    **Person P;
    for (int32_t i = start ; i < range ; ++i)
    {
        P.orgId = i;
        P.firstName = "Gibbi";
        P.lastName = "Prakash";
        P.resume = "Analyst";
        P.salary = 45000;
        P.other = "Summa";
        _set(i,P);

    }
    for (int32_t i = start; i < range; ++i)
    {

            try{
                _get(i);
                //break;
            }catch(cereal::Exception e){
                std::cout <<  "Caught Exception " << std::endl;
            }
    }
    for (int32_t i = start; i < range; ++i)
    {
        _remove(i);
} 
}

Person P{10,"John","Cena","Analyst",450000,"Summa"};当以这种方式创建对象时,程序按预期工作。

OUTPUT:
Starting Redis Testing 
1 Name: John Last: CenaResume: Analyst Salary: 450000 Other: Summa
2 Name: John Last: CenaResume: Analyst Salary: 450000 Other: Summa
No key is matching
No key is matching

当我创建对象首先然后分配值(

Person P; //when object is created first then values are assigned to it.
            P.orgId = i;
            P.firstName = "Gibbi";
            P.lastName = "Prakash";
            P.resume = "Analyst";
            P.salary = 45000;
            P.other = "Summa";

),输出为空或客户端挂起。

我不确定幕后发生了什么,因为我对使用谷物库很陌生。我觉得问题出在谷物上,但我想不通。

4

0 回答 0