#include <hiredis/hiredis.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
struct Person{
int id;
char name[50];
int age;
};
int main() {
Person p = {11, "Alice", 25};
// 连接Redis服务
redisContext *context = redisConnect("127.0.0.1", 6379);
if (context == NULL || context->err) {
if (context) {
printf("%s\n", context->errstr);
} else {
printf("redisConnect error\n");
}
exit(EXIT_FAILURE);
}
//执行命令
// Set Key Value
char *key = "pkey";
/*SET key value */
redisReply * reply = (redisReply *)redisCommand(context, "SET %b %b",&key, 4 ,&p, sizeof(Person) );
std::cout<<"len:"<<sizeof(Person);
printf("type : %d\n", reply->type);
if (reply->type == REDIS_REPLY_STATUS) {
/*SET str Hello World*/
printf("SET %s \n", key);
}
freeReplyObject(reply);
// // GET Key
reply = (redisReply *)redisCommand(context, "GET %b", &key, 4);
Person *p2 = (Person *)(reply->str);
printf("out %d %s %d\n", p2->id, p2->name, p2->age);
printf("GET len %ld\n", reply->len);
freeReplyObject(reply);
//释放连接
redisFree(context);
return EXIT_SUCCESS;
}
reids 存取c语言结构体数据
最新推荐文章于 2025-05-20 18:02:11 发布