From 45ba8fd665b4d6453a6175111edddc6a0e503b43 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Fri, 17 Jun 2011 17:08:20 -0700 Subject: [PATCH] Cleaned up silly usage of memory in C++ fetch method. --- libmemcached/memcached.hpp | 44 ++++++++++++++++++++++++-------------- tests/plus.cpp | 1 - 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/libmemcached/memcached.hpp b/libmemcached/memcached.hpp index 70d3df07..0b27291b 100644 --- a/libmemcached/memcached.hpp +++ b/libmemcached/memcached.hpp @@ -163,30 +163,41 @@ public: * @return a memcached return structure */ memcached_return_t fetch(std::string &key, - std::vector &ret_val) + std::vector &ret_val, + uint32_t &flags, + uint64_t &cas_value) { - char ret_key[MEMCACHED_MAX_KEY]; - size_t value_length= 0; - size_t key_length= 0; memcached_return_t rc; - uint32_t flags= 0; - char *value= memcached_fetch(memc, ret_key, &key_length, - &value_length, &flags, &rc); - if (value && ret_val.empty()) - { - ret_val.reserve(value_length); - ret_val.assign(value, value + value_length); - key.assign(ret_key, key_length); - free(value); - } - else if (value) + + memcached_result_st *result; + if ((result= memcached_fetch_result(memc, NULL, &rc))) { - free(value); + // Key + key.assign(memcached_result_key_value(result), memcached_result_key_length(result)); + + // Actual value, null terminated + ret_val.reserve(memcached_result_length(result) +1); + ret_val.assign(memcached_result_value(result), + memcached_result_value(result) +memcached_result_length(result)); + + // Misc + flags= memcached_result_flags(result); + cas_value= memcached_result_cas(result); } + memcached_result_free(result); return rc; } + memcached_return_t fetch(std::string &key, + std::vector &ret_val) + { + uint32_t flags= 0; + uint64_t cas_value= 0; + + return fetch(key, ret_val, flags, cas_value); + } + /** * Fetches an individual value from the server. * @@ -210,6 +221,7 @@ public: free(value); return true; } + return false; } diff --git a/tests/plus.cpp b/tests/plus.cpp index 818c908a..59ebf555 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -174,7 +174,6 @@ test_return_t mget_test(memcached_st *original) test_true(memc.setAll(keys, values, 50, 9)); test_true(memc.mget(keys)); - size_t count= 0; while ((mc_rc= memc.fetch(return_key, return_value)) == MEMCACHED_SUCCESS) { -- 2.30.2