X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fmemcached.hpp;h=107b01c58a17ed002c4b4ff93e5a2b57125e2e0f;hb=75bbebb7976acf3cbfdf1525599180cf49e67f56;hp=46573f4959b2aab0a874da0eae407965fb2360a3;hpb=9141e90dccb732d6e7ef10ea248ce2fd7d1ef46b;p=m6w6%2Flibmemcached diff --git a/libmemcached/memcached.hpp b/libmemcached/memcached.hpp index 46573f49..107b01c5 100644 --- a/libmemcached/memcached.hpp +++ b/libmemcached/memcached.hpp @@ -143,6 +143,17 @@ public: return memcached_strerror(NULL, rc); } + + bool setBehavior(memcached_behavior flag, uint64_t data) { + memcached_return rc; + rc= memcached_behavior_set(&memc, flag, data); + return (rc == MEMCACHED_SUCCESS); + } + + uint64_t getBehavior(memcached_behavior flag) { + return memcached_behavior_get(&memc, flag); + } + /** * Return the string which contains the list of memcached servers being * used. @@ -236,7 +247,13 @@ public: ret_val.reserve(value_length); ret_val.assign(value, value + value_length); key.assign(ret_key); + free(value); + } + else if (value) + { + free(value); } + return rc; } @@ -265,6 +282,7 @@ public: { ret_val.reserve(value_length); ret_val.assign(value, value + value_length); + free(value); return true; } return false; @@ -302,6 +320,7 @@ public: { ret_val.reserve(value_length); ret_val.assign(value, value + value_length); + free(value); return true; } return false; @@ -917,6 +936,60 @@ public: return version; } + /** + * Retrieve memcached statistics. Populate a std::map with the retrieved + * stats. Each server will map to another std::map of the key:value stats. + * + * @param[out] stats_map a std::map to be populated with the memcached + * stats + * @return true on success; false otherwise + */ + bool getStats(std::map< std::string, std::map > + &stats_map) + { + memcached_return rc; + memcached_stat_st *stats= memcached_stat(&memc, NULL, &rc); + + if (rc != MEMCACHED_SUCCESS && + rc != MEMCACHED_SOME_ERRORS) + { + return false; + } + + uint32_t server_count= memcached_server_count(&memc); + + /* + * For each memcached server, construct a std::map for its stats and add + * it to the std::map of overall stats. + */ + for (uint32_t x= 0; x < server_count; x++) + { + std::ostringstream strstm; + std::string server_name(memcached_server_name(&memc, servers[x])); + server_name.append(":"); + strstm << memcached_server_port(&memc, servers[x]); + server_name.append(strstm.str()); + + std::map server_stats; + char **list= NULL; + char **ptr= NULL; + + list= memcached_stat_get_keys(&memc, &stats[x], &rc); + for (ptr= list; *ptr; ptr++) + { + char *value= memcached_stat_get_value(&memc, &stats[x], *ptr, &rc); + server_stats[*ptr]= value; + free(value); + } + + stats_map[server_name]= server_stats; + free(list); + } + + memcached_stat_free(&memc, stats); + return true; + } + private: std::string servers_list;