From 6232db852b3e682cf186b3d0f8fdfc3083b5a50f Mon Sep 17 00:00:00 2001 From: Tony Han Date: Fri, 10 May 2019 16:42:39 +0800 Subject: [PATCH] support mget for Redis --- protocol.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/protocol.cpp b/protocol.cpp index ca3c04b9..9d49be33 100644 --- a/protocol.cpp +++ b/protocol.cpp @@ -289,8 +289,37 @@ int redis_protocol::write_command_set(const char *key, int key_len, const char * int redis_protocol::write_command_multi_get(const keylist *keylist) { - fprintf(stderr, "error: multi get not implemented for redis yet!\n"); - assert(0); + assert(keylist != NULL); + assert(keylist->get_keys_count() > 0); + + int n = 0; + int size = 0; + + size = evbuffer_add_printf(m_write_buf, + "*%u\r\n" + "$4\r\n" + "MGET\r\n", keylist->get_keys_count() + 1); + + for (unsigned int i = 0; i < keylist->get_keys_count(); i++) { + const char *key; + unsigned int key_len; + + key = keylist->get_key(i, &key_len); + assert(key != NULL); + + size += evbuffer_add_printf(m_write_buf, + "$%u\r\n", + key_len); + + n = evbuffer_add(m_write_buf, key, key_len); + assert(n != -1); + n = evbuffer_add(m_write_buf, "\r\n", 2); + assert(n != -1); + + size += key_len + 2; + } + + return size; } int redis_protocol::write_command_get(const char *key, int key_len, unsigned int offset)