redis存取二进制数据

redis提供的API中,redisCommandArgv接口是二进制安全的,我们可以利用此接口实现对二进制的存取。下面是测试代码


#include 
#include 
#include 
#include 
#include 

#include 
#include 

#include "hiredis.h"

struct st
{
	int a;
	int b;
	char c;
	int d;
};

void set(void)
{
	struct st t;
	t.a = 1;
	t.b = 2;
	t.c = 'a';
	t.d = 4;

	redisContext *c = redisConnect("127.0.0.1", 6379);
	if (!c) {
		printf("connect to redis fail\n");
		exit(1);
	}

	const char *v[3];
	size_t vlen[3];
	v[0] = "set";
	vlen[0] = strlen("set");

	v[1] = "c";
	vlen[1] = strlen("c");

	v[2] = (const char *)&t;
	vlen[2] = sizeof(struct st);

	redisReply *r = (redisReply *)redisCommandArgv(c, sizeof(v) / sizeof(v[0]), v, vlen);
	if (!r) {
		printf("empty reply\n");
		exit(1);
	}

	printf("reply: %s\n", r->str);

	freeReplyObject(r);
	redisFree(c);
}

void get(void)
{
	redisContext *c = redisConnect("127.0.0.1", 6379);
	if (!c) {
		printf("connect to redis fail\n");
		exit(1);
	}

	redisReply *r = (redisReply *)redisCommand(c, "get c");
	if (!r) {
		printf("empty reply\n");
		exit(1);
	}

	printf("reply length: %d\n", r->len);
	if (r->len != sizeof(struct st)) {
		printf("reply len error\n");
		exit(-1);
	}

	struct st *p = (struct st *)malloc(sizeof(struct st));
	if (!p) {
		printf("malloc fail\n");
		exit(-1);
	}
	memcpy(p, r->str, r->len);
	std::cout << "a:" << p->a
		<< ", b:" << p->b
		<< ", c:" << p->c
		<< ", d:" << p->d
		<< std::endl;
		;

	free(p);
	freeReplyObject(r);
	redisFree(c);
}

void zset(void)
{
	struct st t;
	t.a = 1;
	t.b = 2;
	t.c = 'a';
	t.d = 4;

	redisContext *c = redisConnect("127.0.0.1", 6379);
	if (!c) {
		printf("connect to redis fail\n");
		exit(1);
	}

	const char *v[4];
	size_t vlen[4];
	v[0] = "zadd";
	vlen[0] = strlen("zadd");

	v[1] = "d";
	vlen[1] = strlen("d");

	std::stringstream ss;
	ss << time(0);
	v[2] = ss.str().c_str();
	vlen[2] = ss.str().size();

	v[3] = (const char *)&t;
	vlen[3] = sizeof(struct st);

	redisReply *r = (redisReply *)redisCommandArgv(c, sizeof(v) / sizeof(v[0]), v, vlen);
	if (!r) {
		printf("empty reply\n");
		exit(1);
	}

	printf("reply: %s\n", r->str);

	freeReplyObject(r);
	redisFree(c);
}

void zget(void)
{
	redisContext *c = redisConnect("127.0.0.1", 6379);
	if (!c) {
		printf("connect to redis fail\n");
		exit(1);
	}

	redisReply *r = (redisReply *)redisCommand(c, "zrange d 0 -1");
	if (!r) {
		printf("empty reply\n");
		exit(1);
	}

	printf("reply length: %lu\n", r->elements);

	struct st *p = (struct st *)malloc(sizeof(struct st));
	if (!p) {
		printf("malloc fail\n");
		exit(-1);
	}

	for (size_t i = 0; i < r->elements; i++) {
		printf("element length: %u\n", r->element[i]->len);
		if (r->element[i]->len != sizeof(struct st)) {
			printf("reply len error\n");
			exit(-1);
		}

		memcpy(p, r->element[i]->str, r->element[i]->len);
		std::cout << "a:" << p->a
			<< ", b:" << p->b
			<< ", c:" << p->c
			<< ", d:" << p->d
			<< std::endl;
			;
	}

	free(p);
	freeReplyObject(r);
	redisFree(c);
}

int main(int argc, char **argv)
{
	zset();
	zget();
}

你可能感兴趣的:(redis存取二进制数据)