6feb26ab5e0b00baa3e7f25bfe8447dc0323e6fb
[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
9 #include <string.h>
10
11 #include <libmemcached/memcached.hpp>
12
13 using namespace std;
14 using namespace memcache;
15
16 class MyCache
17 {
18 public:
19
20 static const uint32_t num_of_clients= 10;
21
22 static MyCache &singleton()
23 {
24 static MyCache instance;
25 return instance;
26 }
27
28 void set(const string &key,
29 const vector<char> &value)
30 {
31 time_t expiry= 0;
32 uint32_t flags= 0;
33 getCache()->set(key, value, expiry, flags);
34 }
35
36 vector<char> get(const string &key)
37 {
38 vector<char> ret_value;
39 getCache()->get(key, ret_value);
40 return ret_value;
41 }
42
43 void remove(const string &key)
44 {
45 getCache()->remove(key);
46 }
47
48 Memcache *getCache()
49 {
50 /* pick a random element from the vector of clients */
51 Memcache *first= clients[0];
52 return first;
53 }
54
55 private:
56
57 /*
58 * A vector of clients.
59 */
60 std::vector<Memcache *> clients;
61
62 MyCache()
63 :
64 clients()
65 {
66 /* create clients and add them to the vector */
67 for (uint32_t i= 0; i < num_of_clients; i++)
68 {
69 Memcache *client= new Memcache("127.0.0.1:11211");
70 clients.push_back(client);
71 }
72 }
73
74 ~MyCache()
75 {
76 clients.clear();
77 }
78
79 MyCache(const MyCache&);
80
81 };
82
83 class Product
84 {
85 public:
86
87 Product(int in_id, double in_price)
88 :
89 id(in_id),
90 price(in_price)
91 {}
92
93 Product()
94 :
95 id(0),
96 price(0.0)
97 {}
98
99 int getId() const
100 {
101 return id;
102 }
103
104 double getPrice() const
105 {
106 return price;
107 }
108
109 private:
110
111 int id;
112 double price;
113
114 };
115
116 void setAllProducts(vector<Product> &products)
117 {
118 vector<char> raw_products(products.size() * sizeof(Product));
119 memcpy(&raw_products[0], &products[0], products.size() * sizeof(Product));
120 MyCache::singleton().set("AllProducts", raw_products);
121 }
122
123 vector<Product> getAllProducts()
124 {
125 vector<char> raw_products = MyCache::singleton().get("AllProducts");
126 vector<Product> products(raw_products.size() / sizeof(Product));
127 memcpy(&products[0], &raw_products[0], raw_products.size());
128 return products;
129 }
130
131 Product getProduct(const string &key)
132 {
133 vector<char> raw_product= MyCache::singleton().get(key);
134 Product ret;
135 if (! raw_product.empty())
136 {
137 memcpy(&ret, &raw_product[0], sizeof(Product));
138 }
139 else
140 {
141 /* retrieve it from the persistent store */
142 }
143 return ret;
144 }
145
146 void setProduct(const string &key, const Product &product)
147 {
148 vector<char> raw_product(sizeof(Product));
149 memcpy(&raw_product[0], &product, sizeof(Product));
150 MyCache::singleton().set(key, raw_product);
151 }
152
153 int main()
154 {
155 Product pad(1, 5.0);
156 const string key("padraig");
157 cout << "Going to set an object in the cache..." << endl;
158 setProduct(key, pad);
159 cout << "Now retrieve that key..." << endl;
160 Product test= getProduct(key);
161 double price= test.getPrice();
162 cout << "Price of retrieve object: " << price << endl;
163 Product next(2, 10.0);
164 vector<Product> products;
165 products.push_back(pad);
166 products.push_back(next);
167 cout << "going to set a vector of products..." << endl;
168 setAllProducts(products);
169 cout << "now retrieve those products..." << endl;
170 vector<Product> got= getAllProducts();
171 cout << "size of retrieved vector: " << got.size() << endl;
172 vector<Product>::iterator iter= got.begin();
173 while (iter != got.end())
174 {
175 cout << "product " << (*iter).getId() << " costs " << (*iter).getPrice() << endl;
176 ++iter;
177 }
178 return 0;
179 }