Updated C++ interface to have include guards. Also modified the naming
[awesomized/libmemcached] / libmemcached / memcached.hh
1 /*
2 * Summary: C++ interface for memcached server
3 *
4 * Copy: See Copyright for the status of this software.
5 *
6 * Authors: Padraig O'Sullivan, Patrick Galbraith
7 */
8
9 #ifndef LIBMEMCACHEDPP_H
10 #define LIBMEMCACHEDPP_H
11
12 #include <libmemcached/memcached.h>
13
14 #include <string>
15 #include <vector>
16
17 class Memcached
18 {
19 public:
20
21 Memcached()
22 :
23 memc(),
24 result()
25 {
26 memcached_create(&memc);
27 }
28
29 Memcached(memcached_st *clone)
30 :
31 memc(),
32 result()
33 {
34 memcached_clone(&memc, clone);
35 }
36
37 ~Memcached()
38 {
39 memcached_free(&memc);
40 }
41
42 bool fetch(std::string &key,
43 std::string &ret_val,
44 size_t *key_length,
45 size_t *value_length,
46 uint32_t *flags,
47 memcached_return *rc)
48 {
49 char ret_key[MEMCACHED_MAX_KEY];
50 char *value= memcached_fetch(&memc, ret_key, key_length,
51 value_length, flags, rc);
52 if (value)
53 {
54 ret_val.assign(value);
55 key.assign(ret_key);
56 return true;
57 }
58 return false;
59 }
60
61 std::string get(const std::string &key, size_t *value_length)
62 {
63 uint32_t flags;
64 memcached_return rc;
65 std::string ret_val;
66
67 char *value= memcached_get(&memc, key.c_str(), key.length(),
68 value_length, &flags, &rc);
69 if (value)
70 {
71 ret_val.assign(value);
72 }
73 return ret_val;
74 }
75
76 std::string getByKey(const std::string &master_key,
77 const std::string &key,
78 size_t *value_length)
79 {
80 uint32_t flags;
81 memcached_return rc;
82 std::string ret_val;
83
84 char *value= memcached_get_by_key(&memc,
85 master_key.c_str(), master_key.length(),
86 key.c_str(), key.length(),
87 value_length, &flags, &rc);
88 if (value)
89 {
90 ret_val.assign(value);
91 }
92 return ret_val;
93 }
94
95 bool mget(std::vector<std::string> &keys)
96 {
97 std::vector<const char *> real_keys;
98 std::vector<size_t> key_len;
99 /*
100 * Construct an array which will contain the length
101 * of each of the strings in the input vector. Also, to
102 * interface with the memcached C API, we need to convert
103 * the vector of std::string's to a vector of char *.
104 */
105 real_keys.reserve(keys.size());
106 key_len.reserve(keys.size());
107
108 std::vector<std::string>::iterator it= keys.begin();
109
110 while (it != keys.end())
111 {
112 real_keys.push_back(const_cast<char *>((*it).c_str()));
113 key_len.push_back((*it).length());
114 ++it;
115 }
116
117 /*
118 * If the std::vector of keys is empty then we cannot
119 * call memcached_mget as we will get undefined behavior.
120 */
121 if (!real_keys.empty())
122 {
123 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
124 real_keys.size());
125 return (rc == MEMCACHED_SUCCESS);
126 }
127
128 return false;
129 }
130
131 bool set(const std::string &key,
132 const std::string &value,
133 time_t expiration,
134 uint32_t flags)
135 {
136 memcached_return rc= memcached_set(&memc,
137 key.c_str(), key.length(),
138 value.c_str(), value.length(),
139 expiration, flags);
140 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
141 }
142
143 bool setAll(std::vector<std::string> &keys,
144 std::vector<std::string> &values,
145 time_t expiration,
146 uint32_t flags)
147 {
148 if (keys.size() != values.size())
149 {
150 return false;
151 }
152 bool retval= true;
153 std::vector<std::string>::iterator key_it= keys.begin();
154 std::vector<std::string>::iterator val_it= values.begin();
155 while (key_it != keys.end())
156 {
157 retval= set((*key_it), (*val_it), expiration, flags);
158 if (retval == false)
159 {
160 return retval;
161 }
162 ++key_it;
163 ++val_it;
164 }
165 return retval;
166 }
167
168 bool setByKey(const std::string &master_key,
169 const std::string &key,
170 const std::string &value,
171 time_t expiration,
172 uint32_t flags)
173 {
174 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
175 master_key.length(),
176 key.c_str(), key.length(),
177 value.c_str(), value.length(),
178 expiration,
179 flags);
180 return (rc == MEMCACHED_SUCCESS);
181 }
182
183 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
184 {
185 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
186 offset, value);
187 return (rc == MEMCACHED_SUCCESS);
188 }
189
190 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
191 {
192 memcached_return rc= memcached_decrement(&memc, key.c_str(),
193 key.length(),
194 offset, value);
195 return (rc == MEMCACHED_SUCCESS);
196 }
197
198
199 bool add(const std::string &key, const std::string &value)
200 {
201 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
202 value.c_str(), value.length(), 0, 0);
203 return (rc == MEMCACHED_SUCCESS);
204 }
205
206 bool addByKey(const std::string &master_key,
207 const std::string &key,
208 const std::string &value)
209 {
210 memcached_return rc= memcached_add_by_key(&memc,
211 master_key.c_str(),
212 master_key.length(),
213 key.c_str(),
214 key.length(),
215 value.c_str(),
216 value.length(),
217 0, 0);
218 return (rc == MEMCACHED_SUCCESS);
219 }
220
221 bool replace(const std::string &key, const std::string &value)
222 {
223 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
224 value.c_str(), value.length(),
225 0, 0);
226 return (rc == MEMCACHED_SUCCESS);
227 }
228
229 bool replaceByKey(const std::string &master_key,
230 const std::string &key,
231 const std::string &value)
232 {
233 memcached_return rc= memcached_replace_by_key(&memc,
234 master_key.c_str(),
235 master_key.length(),
236 key.c_str(),
237 key.length(),
238 value.c_str(),
239 value.length(),
240 0, 0);
241 return (rc == MEMCACHED_SUCCESS);
242 }
243
244 bool prepend(const std::string &key, const std::string &value)
245 {
246 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
247 value.c_str(), value.length(), 0, 0);
248 return (rc == MEMCACHED_SUCCESS);
249 }
250
251 bool prependByKey(const std::string &master_key,
252 const std::string &key,
253 const std::string &value)
254 {
255 memcached_return rc= memcached_prepend_by_key(&memc,
256 master_key.c_str(),
257 master_key.length(),
258 key.c_str(),
259 key.length(),
260 value.c_str(),
261 value.length(),
262 0,
263 0);
264 return (rc == MEMCACHED_SUCCESS);
265 }
266
267 bool append(const std::string &key, const std::string &value)
268 {
269 memcached_return rc= memcached_append(&memc,
270 key.c_str(),
271 key.length(),
272 value.c_str(),
273 value.length(),
274 0, 0);
275 return (rc == MEMCACHED_SUCCESS);
276 }
277
278 bool appendByKey(const std::string &master_key,
279 const std::string &key,
280 const std::string &value)
281 {
282 memcached_return rc= memcached_append_by_key(&memc,
283 master_key.c_str(),
284 master_key.length(),
285 key.c_str(),
286 key.length(),
287 value.c_str(),
288 value.length(),
289 0, 0);
290 return (rc == MEMCACHED_SUCCESS);
291 }
292
293 bool cas(const std::string &key,
294 const std::string &value,
295 uint64_t cas_arg)
296 {
297 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
298 value.c_str(), value.length(),
299 0, 0, cas_arg);
300 return (rc == MEMCACHED_SUCCESS);
301 }
302
303 bool casByKey(const std::string &master_key,
304 const std::string &key,
305 const std::string &value,
306 uint64_t cas_arg)
307 {
308 memcached_return rc= memcached_cas_by_key(&memc,
309 master_key.c_str(),
310 master_key.length(),
311 key.c_str(),
312 key.length(),
313 value.c_str(),
314 value.length(),
315 0, 0, cas_arg);
316 return (rc == MEMCACHED_SUCCESS);
317 }
318
319 // using 'remove' vs. 'delete' since 'delete' is a keyword
320 bool remove(const std::string &key)
321 {
322 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
323 return (rc == MEMCACHED_SUCCESS);
324 }
325
326 bool removeByKey(const std::string &master_key,
327 const std::string &key)
328 {
329 memcached_return rc= memcached_delete_by_key(&memc,
330 master_key.c_str(),
331 master_key.length(),
332 key.c_str(),
333 key.length(),
334 0);
335 return (rc == MEMCACHED_SUCCESS);
336 }
337
338 bool flush(time_t expiration)
339 {
340 memcached_return rc= memcached_flush(&memc, expiration);
341 return (rc == MEMCACHED_SUCCESS);
342 }
343
344 bool fetchExecute(memcached_execute_function *callback,
345 void *context,
346 unsigned int num_of_callbacks)
347 {
348 memcached_return rc= memcached_fetch_execute(&memc,
349 callback,
350 context,
351 num_of_callbacks);
352 return (rc == MEMCACHED_SUCCESS);
353 }
354
355 const std::string libVersion() const
356 {
357 const char *ver= memcached_lib_version();
358 const std::string version(ver);
359 return version;
360 }
361
362 private:
363 memcached_st memc;
364 memcached_result_st result;
365 };
366
367 #endif /* LIBMEMCACHEDPP_H */