マーシャリングを一切行わずにバイナリ データをリモート ストアに直接格納すると、災害が発生する可能性があります。バイナリ データをプラットフォームから独立させるために使用できるシリアライゼーション プロトコルはたくさんあります。
つまり、あなたの質問に答えるには:
// This is the key
int k[3] = {11,22,33};
// This is the value
int v[4] = {0,1,2,3};
redisReply *reply = 0;
// Store the key/value: note the usage of sizeof to get the size of the arrays (in bytes)
reply = redisCommand(context, "SET %b %b", k, (size_t) sizeof(k), v, (size_t) sizeof(v) );
if (!reply)
return REDIS_ERR;
freeReplyObject(reply);
// Now, get the value back, corresponding to the same key
reply = redisCommand(context, "GET %b", k, (size_t) sizeof(k) );
if ( !reply )
return REDIS_ERR;
if ( reply->type != REDIS_REPLY_STRING ) {
printf("ERROR: %s", reply->str);
} else {
// Here, it is safer to make a copy to be sure memory is properly aligned
int *val = (int *) malloc( reply->len );
memcpy( val, reply->str, reply->len);
for (int i=0; i<reply->len/sizeof(int); ++i )
printf("%d\n",val[i]);
free( val );
}
freeReplyObject(reply);
この種のコードは、すべての Redis クライアントが同じエンディアンと同じ sizeof(int) を持つシステムで実行されていることが確実な場合にのみ機能することに注意してください。