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