2017-03-09 8 views
0
나는 이런 식으로 뭔가 수행 할 hiredis 라이브러리의 redisCommand를 사용하고

사용 레디 스 목록에 빈 문자열을 삽입하는 방법 :HiRedis :: LPUSH

LPUSH list1 a b "" c d "" e 

""나는에 빈 요소를 삽입 할 뜻을 명부. 그것은 redis 명령 줄에서 할 때 잘 작동하지만 hiredis에 명령으로 이것을 전달하면 작동하지 않고 요소가 비어있는 대신 ""이됩니다. 해결 되셨습니까?

reply = (redisReply *) redisCommand(c,"LPUSH list1 a b c "" c d "" e); 

나는 또한

+0

redisCommandArgv가 어떻게 redisComand 전화 않았다 사용할 수 있습니까? 어떤예요? –

+0

예제를 추가했습니다 –

답변

0

당신은 레디 스에서 바이너리 안전 문자열을 사용할 수있다 백 슬래시 등, 작은 따옴표를 넣어 시도 : 여기

내가 redisCommand라는 방법입니다. 아래와 같이 LPUSH 명령을 사용하여 이진 문자열을 목록에 추가하십시오.

redisReply * reply = redisCommand ("LPUSH list1 % b % b % b", "a", strlen ("a" "", 0, "b", strlen ("b"));

출력은 다음과 같습니다

127.0.0.1:6379> lrange list1 0 -1 
1) "b" 
2) "" 
3) "a" 

HTH, Swanand

+0

문제는 목록이 크고 redis에 한 번만 연결하려고합니다. –

0

만약 당신이 고정되어 목록에 밀어하려는 요소의 수 : 형식화 된 매개 변수와 함께 redisCommand를 사용

const char *list = "list-name"; 
const char *non_empty_val = "value"; 
const char *empty_val = ""; 
/* or use %b to push binary element, as the other answer mentioned. */ 
redisReply *reply = (redisReply*)redisCommand(redis, 
          "lpush %s %s %s", list, non_empty_val, empty_val); 

요소의 수 동적 인 경우 :

int argc = 4; /* number of arguments including command name. */ 

const char **argv = (const char**)malloc(sizeof(const char**) * argc); 
argv[0] = strdup("lpush"); 
argv[1] = strdup(list); 
argv[2] = strdup(non_empty_val); 
argv[3] = strdup(empty_val); 

/* specify the length of each argument. */ 
size_t *argv_len = (size_t*)malloc(sizeof(size_t) * argc); 
for (int i = 0; i < argc; ++i) 
    argv_len[i] = strlen(argv[i]); 

redisReply *reply = (redisReply*)redisCommandArgv(redis, argc, argv, argv_len);