2 * An example file showing the usage of the C++ libmemcached interface.
12 #include <libmemcached/memcached.hpp>
15 using namespace memcache
;
21 inline void operator()(const T
*ptr
) const
31 static const uint32_t num_of_clients
= 10;
33 static MyCache
&singleton()
35 static MyCache instance
;
39 void set(const string
&key
,
40 const vector
<char> &value
)
44 getCache()->set(key
, value
, expiry
, flags
);
47 vector
<char> get(const string
&key
)
49 vector
<char> ret_value
;
50 getCache()->get(key
, ret_value
);
54 void remove(const string
&key
)
56 getCache()->remove(key
);
62 * pick a random element from the vector of clients. Obviously, this is
63 * not very random but suffices as an example!
65 uint32_t index
= rand() % num_of_clients
;
66 return clients
[index
];
72 * A vector of clients.
74 std::vector
<Memcache
*> clients
;
80 /* create clients and add them to the vector */
81 for (uint32_t i
= 0; i
< num_of_clients
; i
++)
83 Memcache
*client
= new Memcache("127.0.0.1:11211");
84 clients
.push_back(client
);
90 for_each(clients
.begin(), clients
.end(), DeletePtrs());
94 MyCache(const MyCache
&);
102 Product(int in_id
, double in_price
)
119 double getPrice() const
131 void setAllProducts(vector
<Product
> &products
)
133 vector
<char> raw_products(products
.size() * sizeof(Product
));
134 memcpy(&raw_products
[0], &products
[0], products
.size() * sizeof(Product
));
135 MyCache::singleton().set("AllProducts", raw_products
);
138 vector
<Product
> getAllProducts()
140 vector
<char> raw_products
= MyCache::singleton().get("AllProducts");
141 vector
<Product
> products(raw_products
.size() / sizeof(Product
));
142 memcpy(&products
[0], &raw_products
[0], raw_products
.size());
146 Product
getProduct(const string
&key
)
148 vector
<char> raw_product
= MyCache::singleton().get(key
);
150 if (! raw_product
.empty())
152 memcpy(&ret
, &raw_product
[0], sizeof(Product
));
156 /* retrieve it from the persistent store */
161 void setProduct(const string
&key
, const Product
&product
)
163 vector
<char> raw_product(sizeof(Product
));
164 memcpy(&raw_product
[0], &product
, sizeof(Product
));
165 MyCache::singleton().set(key
, raw_product
);
172 const string
key("padraig");
173 cout
<< "Going to set an object in the cache..." << endl
;
174 setProduct(key
, pad
);
175 cout
<< "Now retrieve that key..." << endl
;
176 Product test
= getProduct(key
);
177 double price
= test
.getPrice();
178 cout
<< "Price of retrieve object: " << price
<< endl
;
179 Product
next(2, 10.0);
180 vector
<Product
> products
;
181 products
.push_back(pad
);
182 products
.push_back(next
);
183 cout
<< "going to set a vector of products..." << endl
;
184 setAllProducts(products
);
185 cout
<< "now retrieve those products..." << endl
;
186 vector
<Product
> got
= getAllProducts();
187 cout
<< "size of retrieved vector: " << got
.size() << endl
;
188 vector
<Product
>::iterator iter
= got
.begin();
189 while (iter
!= got
.end())
191 cout
<< "product " << (*iter
).getId() << " costs " << (*iter
).getPrice() << endl
;
195 Memcache
first_client("127.0.0.1:11211");
196 Memcache
second_client("127.0.0.1", 11211);
197 //first_client.set("key", some_vector_of_chars, expiry, flags);
198 //first_client.get("key", vector_to_fill_with_data);
199 //first_client.remove("key");
200 first_client
.addServer("192.168.1.1", 11211);