Updated the destructor for the C++ example to correctly release memory.
[awesomized/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 /* pick a random element from the vector of clients */
62 Memcache *first= clients[0];
63 return first;
64 }
65
66 private:
67
68 /*
69 * A vector of clients.
70 */
71 std::vector<Memcache *> clients;
72
73 MyCache()
74 :
75 clients()
76 {
77 /* create clients and add them to the vector */
78 for (uint32_t i= 0; i < num_of_clients; i++)
79 {
80 Memcache *client= new Memcache("127.0.0.1:11211");
81 clients.push_back(client);
82 }
83 }
84
85 ~MyCache()
86 {
87 for_each(clients.begin(), clients.end(), DeletePtrs());
88 clients.clear();
89 }
90
91 MyCache(const MyCache&);
92
93 };
94
95 class Product
96 {
97 public:
98
99 Product(int in_id, double in_price)
100 :
101 id(in_id),
102 price(in_price)
103 {}
104
105 Product()
106 :
107 id(0),
108 price(0.0)
109 {}
110
111 int getId() const
112 {
113 return id;
114 }
115
116 double getPrice() const
117 {
118 return price;
119 }
120
121 private:
122
123 int id;
124 double price;
125
126 };
127
128 void setAllProducts(vector<Product> &products)
129 {
130 vector<char> raw_products(products.size() * sizeof(Product));
131 memcpy(&raw_products[0], &products[0], products.size() * sizeof(Product));
132 MyCache::singleton().set("AllProducts", raw_products);
133 }
134
135 vector<Product> getAllProducts()
136 {
137 vector<char> raw_products = MyCache::singleton().get("AllProducts");
138 vector<Product> products(raw_products.size() / sizeof(Product));
139 memcpy(&products[0], &raw_products[0], raw_products.size());
140 return products;
141 }
142
143 Product getProduct(const string &key)
144 {
145 vector<char> raw_product= MyCache::singleton().get(key);
146 Product ret;
147 if (! raw_product.empty())
148 {
149 memcpy(&ret, &raw_product[0], sizeof(Product));
150 }
151 else
152 {
153 /* retrieve it from the persistent store */
154 }
155 return ret;
156 }
157
158 void setProduct(const string &key, const Product &product)
159 {
160 vector<char> raw_product(sizeof(Product));
161 memcpy(&raw_product[0], &product, sizeof(Product));
162 MyCache::singleton().set(key, raw_product);
163 }
164
165 int main()
166 {
167 Product pad(1, 5.0);
168 const string key("padraig");
169 cout << "Going to set an object in the cache..." << endl;
170 setProduct(key, pad);
171 cout << "Now retrieve that key..." << endl;
172 Product test= getProduct(key);
173 double price= test.getPrice();
174 cout << "Price of retrieve object: " << price << endl;
175 Product next(2, 10.0);
176 vector<Product> products;
177 products.push_back(pad);
178 products.push_back(next);
179 cout << "going to set a vector of products..." << endl;
180 setAllProducts(products);
181 cout << "now retrieve those products..." << endl;
182 vector<Product> got= getAllProducts();
183 cout << "size of retrieved vector: " << got.size() << endl;
184 vector<Product>::iterator iter= got.begin();
185 while (iter != got.end())
186 {
187 cout << "product " << (*iter).getId() << " costs " << (*iter).getPrice() << endl;
188 ++iter;
189 }
190 return 0;
191 }