Record a hiredis demo that uses a struct for key and a struct for value

#include <stdio.h> #include <string.h> #include <hiredis/hiredis.h> typedef struct { char name[10]; char myStr[20]; } mytest; Void test(void) {redisContext *context = redisConnect("127.0.0.1", 6379); If (context->err) {// Connection failed redisFree(context); printf("connect redisServer err:%s\n", context->errstr); return ; } printf("connect success\n"); mytest test={0}; memset(test.name, '\0', 10); strcpy(test.myStr, "llllll"); mytest key={0}; //strcpy(key.name, "123"); strcpy(key.myStr, "strstr"); char *key2 = strdup("test_key"); printf("start set key value\n"); redisReply *reply = (redisReply*)redisCommand(context, "SET %b %b",&key,sizeof(key),&test, sizeof(test)); freeReplyObject(reply); reply = redisCommand(context, "GET %b", &key, sizeof(key)); mytest *result = (mytest *)reply->str; printf("%s\n",result->name); printf("%s\n",result->myStr); freeReplyObject(reply); } int main(void) { test(); return 0;; }Copy the code

If the key is a string, change the key to a char array, delete the address character, and leave the value unchanged