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