Merging bug fix for wrong retry counter.
[m6w6/libmemcached] / tests / cpp_example.cc
1 /*
2 * An example file showing the usage of the C++ libmemcached interface.
3 */
4
5 #include <vector>
6 #include <string>
7 #include <iostream>
8 #include <algorithm>
9
10 #include <string.h>
11
12 #include <libmemcached/memcached.hpp>
13
14 using namespace std;
15 using namespace memcache;
16
17 class DeletePtrs
18 {
19 public:
20 template<typename T>
21 inline void operator()(const T *ptr) const
22 {
23 delete ptr;
24 }
25 };
26
27 class MyCache
28 {
29 public:
30
31 static const uint32_t num_of_clients= 10;
32
33 static MyCache &singleton()
34 {
35 static MyCache instance;
36 return instance;
37 }
38
39 void set(const string &key,
40 const vector<char> &value)
41 {
42 time_t expiry= 0;
43 uint32_t flags= 0;
44 getCache()->set(key, value, expiry, flags);
45 }
46
47 vector<char> get(const string &key)
48 {
49 vector<char> ret_value;
50 getCache()->get(key, ret_value);
51 return ret_value;
52 }
53
54 void remove(const string &key)
55 {
56 getCache()->remove(key);
57 }
58
59 Memcache *getCache()
60 {
61 /*
62 * pick a random element from the vector of clients. Obviously, this is
63 * not very random but suffices as an example!
64 */
65 uint32_t index= rand() % num_of_clients;
66 return clients[index];
67 }
68
69 private:
70
71 /*
72 * A vector of clients.
73 */
74 std::vector<Memcache *> clients;
75
76 MyCache()
77 :
78 clients()
79 {
80 /* create clients and add them to the vector */
81 for (uint32_t i= 0; i < num_of_clients; i++)
82 {
83 Memcache *client= new Memcache("127.0.0.1:11211");
84 clients.push_back(client);
85 }
86 }
87
88 ~MyCache()
89 {
90 for_each(clients.begin(), clients.end(), DeletePtrs());
91 clients.clear();
92 }
93
94 MyCache(const MyCache&);
95
96 };
97
98 class Product
99 {
100 public:
101
102 Product(int in_id, double in_price)
103 :
104 id(in_id),
105 price(in_price)
106 {}
107
108 Product()
109 :
110 id(0),
111 price(0.0)
112 {}
113
114 int getId() const
115 {
116 return id;
117 }
118
119 double getPrice() const
120 {
121 return price;
122 }
123
124 private:
125
126 int id;
127 double price;
128
129 };
130
131 void setAllProducts(vector<Product> &products)
132 {
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);
136 }
137
138 vector<Product> getAllProducts()
139 {
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());
143 return products;
144 }
145
146 Product getProduct(const string &key)
147 {
148 vector<char> raw_product= MyCache::singleton().get(key);
149 Product ret;
150 if (! raw_product.empty())
151 {
152 memcpy(&ret, &raw_product[0], sizeof(Product));
153 }
154 else
155 {
156 /* retrieve it from the persistent store */
157 }
158 return ret;
159 }
160
161 void setProduct(const string &key, const Product &product)
162 {
163 vector<char> raw_product(sizeof(Product));
164 memcpy(&raw_product[0], &product, sizeof(Product));
165 MyCache::singleton().set(key, raw_product);
166 }
167
168 int main()
169 {
170 #if 0
171 Product pad(1, 5.0);
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())
190 {
191 cout << "product " << (*iter).getId() << " costs " << (*iter).getPrice() << endl;
192 ++iter;
193 }
194 #endif
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);
201 return 0;
202 }