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;
}
{
ret_val.reserve(value_length);
ret_val.assign(value, value + value_length);
+ free(value);
return true;
}
return false;
{
ret_val.reserve(value_length);
ret_val.assign(value, value + value_length);
+ free(value);
return true;
}
return false;
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<std::string, std::string> >
+ &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<std::string, std::string> 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;
#include <string>
#include <iostream>
#include <algorithm>
+#include <map>
#include <string.h>
int main()
{
-#if 0
- Product pad(1, 5.0);
- const string key("padraig");
- cout << "Going to set an object in the cache..." << endl;
- setProduct(key, pad);
- cout << "Now retrieve that key..." << endl;
- Product test= getProduct(key);
- double price= test.getPrice();
- cout << "Price of retrieve object: " << price << endl;
- Product next(2, 10.0);
- vector<Product> products;
- products.push_back(pad);
- products.push_back(next);
- cout << "going to set a vector of products..." << endl;
- setAllProducts(products);
- cout << "now retrieve those products..." << endl;
- vector<Product> got= getAllProducts();
- cout << "size of retrieved vector: " << got.size() << endl;
- vector<Product>::iterator iter= got.begin();
- while (iter != got.end())
+ Memcache first_client("127.0.0.1:19191");
+ map< string, map<string, string> > my_stats;
+ first_client.getStats(my_stats);
+
+ /*
+ * Iterate through the retrieved stats.
+ */
+ map< string, map<string, string> >::iterator it=
+ my_stats.begin();
+ while (it != my_stats.end())
{
- cout << "product " << (*iter).getId() << " costs " << (*iter).getPrice() << endl;
- ++iter;
+ cout << "working with server: " << (*it).first << endl;
+ map<string, string> serv_stats= (*it).second;
+ map<string, string>::iterator iter= serv_stats.begin();
+ while (iter != serv_stats.end())
+ {
+ cout << (*iter).first << ":" << (*iter).second << endl;
+ ++iter;
+ }
+ ++it;
}
-#endif
- Memcache first_client("127.0.0.1:11211");
- Memcache second_client("127.0.0.1", 11211);
- //first_client.set("key", some_vector_of_chars, expiry, flags);
- //first_client.get("key", vector_to_fill_with_data);
- //first_client.remove("key");
- first_client.addServer("192.168.1.1", 11211);
+
return 0;
}