From: Padraig O'Sullivan Date: Sat, 19 Sep 2009 16:52:40 +0000 (-0400) Subject: Updated the C++ header file to correctly add memcached servers in the X-Git-Tag: 0.34~13^2~11 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=a715683d855fd0ee03d9557abb3c3f739e61a354;p=m6w6%2Flibmemcached Updated the C++ header file to correctly add memcached servers in the constructor. --- diff --git a/libmemcached/memcached.hpp b/libmemcached/memcached.hpp index a101da34..bb8a9732 100644 --- a/libmemcached/memcached.hpp +++ b/libmemcached/memcached.hpp @@ -55,6 +55,7 @@ public: { memcached_create(&memc); servers= memcached_servers_parse(servers_list.c_str()); + memcached_server_push(&memc, servers); } Memcache(const std::string &hostname, @@ -72,6 +73,7 @@ public: strsmt << port; servers_list.append(strsmt.str()); servers= memcached_servers_parse(servers_list.c_str()); + memcached_server_push(&memc, servers); } Memcache(memcached_st *clone) @@ -93,6 +95,7 @@ public: { memcached_clone(&memc, const_cast(&rhs.getImpl())); servers= memcached_servers_parse(servers_list.c_str()); + memcached_server_push(&memc, servers); } Memcache &operator=(const Memcache &rhs) @@ -101,6 +104,7 @@ public: { memcached_clone(&memc, const_cast(&rhs.getImpl())); servers= memcached_servers_parse(servers_list.c_str()); + memcached_server_push(&memc, servers); } return *this; } diff --git a/tests/cpp_example.cc b/tests/cpp_example.cc new file mode 100644 index 00000000..6feb26ab --- /dev/null +++ b/tests/cpp_example.cc @@ -0,0 +1,179 @@ +/* + * An example file showing the usage of the C++ libmemcached interface. + */ + +#include +#include +#include + +#include + +#include + +using namespace std; +using namespace memcache; + +class MyCache +{ +public: + + static const uint32_t num_of_clients= 10; + + static MyCache &singleton() + { + static MyCache instance; + return instance; + } + + void set(const string &key, + const vector &value) + { + time_t expiry= 0; + uint32_t flags= 0; + getCache()->set(key, value, expiry, flags); + } + + vector get(const string &key) + { + vector ret_value; + getCache()->get(key, ret_value); + return ret_value; + } + + void remove(const string &key) + { + getCache()->remove(key); + } + + Memcache *getCache() + { + /* pick a random element from the vector of clients */ + Memcache *first= clients[0]; + return first; + } + +private: + + /* + * A vector of clients. + */ + std::vector clients; + + MyCache() + : + clients() + { + /* create clients and add them to the vector */ + for (uint32_t i= 0; i < num_of_clients; i++) + { + Memcache *client= new Memcache("127.0.0.1:11211"); + clients.push_back(client); + } + } + + ~MyCache() + { + clients.clear(); + } + + MyCache(const MyCache&); + +}; + +class Product +{ +public: + + Product(int in_id, double in_price) + : + id(in_id), + price(in_price) + {} + + Product() + : + id(0), + price(0.0) + {} + + int getId() const + { + return id; + } + + double getPrice() const + { + return price; + } + +private: + + int id; + double price; + +}; + +void setAllProducts(vector &products) +{ + vector raw_products(products.size() * sizeof(Product)); + memcpy(&raw_products[0], &products[0], products.size() * sizeof(Product)); + MyCache::singleton().set("AllProducts", raw_products); +} + +vector getAllProducts() +{ + vector raw_products = MyCache::singleton().get("AllProducts"); + vector products(raw_products.size() / sizeof(Product)); + memcpy(&products[0], &raw_products[0], raw_products.size()); + return products; +} + +Product getProduct(const string &key) +{ + vector raw_product= MyCache::singleton().get(key); + Product ret; + if (! raw_product.empty()) + { + memcpy(&ret, &raw_product[0], sizeof(Product)); + } + else + { + /* retrieve it from the persistent store */ + } + return ret; +} + +void setProduct(const string &key, const Product &product) +{ + vector raw_product(sizeof(Product)); + memcpy(&raw_product[0], &product, sizeof(Product)); + MyCache::singleton().set(key, raw_product); +} + +int main() +{ + 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 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 got= getAllProducts(); + cout << "size of retrieved vector: " << got.size() << endl; + vector::iterator iter= got.begin(); + while (iter != got.end()) + { + cout << "product " << (*iter).getId() << " costs " << (*iter).getPrice() << endl; + ++iter; + } + return 0; +}