bb8a9732d9b8614614f2b501650bf67a570018df
[awesomized/libmemcached] / libmemcached / memcached.hpp
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 <osullivan.padraig@gmail.com>
7 * Patrick Galbraith <patg@patg.net>
8 */
9
10 /**
11 * @file memcached.hpp
12 * @brief Libmemcached C++ interface
13 */
14
15 #ifndef LIBMEMCACHEDPP_H
16 #define LIBMEMCACHEDPP_H
17
18 #include <libmemcached/memcached.h>
19 #include <libmemcached/exception.hpp>
20
21 #include <string.h>
22
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 #include <map>
27
28 namespace memcache
29 {
30
31 /**
32 * This is the core memcached library (if later, other objects
33 * are needed, they will be created from this class).
34 */
35 class Memcache
36 {
37 public:
38
39 Memcache()
40 :
41 servers_list(),
42 memc(),
43 servers(NULL),
44 result()
45 {
46 memcached_create(&memc);
47 }
48
49 Memcache(const std::string &in_servers_list)
50 :
51 servers_list(in_servers_list),
52 memc(),
53 servers(NULL),
54 result()
55 {
56 memcached_create(&memc);
57 servers= memcached_servers_parse(servers_list.c_str());
58 memcached_server_push(&memc, servers);
59 }
60
61 Memcache(const std::string &hostname,
62 unsigned int port)
63 :
64 servers_list(),
65 memc(),
66 servers(NULL),
67 result()
68 {
69 memcached_create(&memc);
70 servers_list.append(hostname);
71 servers_list.append(":");
72 std::ostringstream strsmt;
73 strsmt << port;
74 servers_list.append(strsmt.str());
75 servers= memcached_servers_parse(servers_list.c_str());
76 memcached_server_push(&memc, servers);
77 }
78
79 Memcache(memcached_st *clone)
80 :
81 servers_list(),
82 memc(),
83 servers(NULL),
84 result()
85 {
86 memcached_clone(&memc, clone);
87 }
88
89 Memcache(const Memcache &rhs)
90 :
91 servers_list(rhs.servers_list),
92 memc(),
93 servers(NULL),
94 result()
95 {
96 memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
97 servers= memcached_servers_parse(servers_list.c_str());
98 memcached_server_push(&memc, servers);
99 }
100
101 Memcache &operator=(const Memcache &rhs)
102 {
103 if (this != &rhs)
104 {
105 memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
106 servers= memcached_servers_parse(servers_list.c_str());
107 memcached_server_push(&memc, servers);
108 }
109 return *this;
110 }
111
112 ~Memcache()
113 {
114 memcached_free(&memc);
115 memcached_server_list_free(servers);
116 }
117
118 /**
119 * Get the internal memcached_st *
120 */
121 memcached_st &getImpl()
122 {
123 return memc;
124 }
125
126 /**
127 * Get the internal memcached_st *
128 */
129 const memcached_st &getImpl() const
130 {
131 return memc;
132 }
133
134 /**
135 * Return an error string for the given return structure.
136 *
137 * @param[in] rc a memcached_return structure
138 * @return error string corresponding to given return code in the library.
139 */
140 const std::string getError(memcached_return rc) const
141 {
142 /* first parameter to strerror is unused */
143 return memcached_strerror(NULL, rc);
144 }
145
146 /**
147 * Return the string which contains the list of memcached servers being
148 * used.
149 *
150 * @return a std::string containing the list of memcached servers
151 */
152 const std::string getServersList() const
153 {
154 return servers_list;
155 }
156
157 /**
158 * Set the list of memcached servers to use.
159 *
160 * @param[in] in_servers_list list of servers
161 * @return true on success; false otherwise
162 */
163 bool setServers(const std::string &in_servers_list)
164 {
165 servers_list.assign(in_servers_list);
166 servers= memcached_servers_parse(in_servers_list.c_str());
167 return (servers == NULL);
168 }
169
170 /**
171 * Add a server to the list of memcached servers to use.
172 *
173 * @param[in] server_name name of the server to add
174 * @param[in[ port port number of server to add
175 * @return true on success; false otherwise
176 */
177 bool addServer(const std::string &server_name, unsigned int port)
178 {
179 memcached_return rc;
180 std::ostringstream strstm;
181 servers_list.append(",");
182 servers_list.append(server_name);
183 servers_list.append(":");
184 strstm << port;
185 servers_list.append(strstm.str());
186 servers= memcached_server_list_append(servers,
187 server_name.c_str(),
188 port,
189 &rc);
190 return (rc == MEMCACHED_SUCCESS);
191 }
192
193 /**
194 * Fetches an individual value from the server. mget() must always
195 * be called before using this method.
196 *
197 * @param[in] key key of object to fetch
198 * @param[out] ret_val store returned object in this vector
199 * @return a memcached return structure
200 */
201 memcached_return fetch(std::string &key,
202 std::vector<char> &ret_val)
203 {
204 char ret_key[MEMCACHED_MAX_KEY];
205 size_t value_length= 0;
206 size_t key_length= 0;
207 memcached_return rc;
208 uint32_t flags= 0;
209 char *value= memcached_fetch(&memc, ret_key, &key_length,
210 &value_length, &flags, &rc);
211 if (value && ret_val.empty())
212 {
213 ret_val.reserve(value_length);
214 ret_val.assign(value, value + value_length);
215 key.assign(ret_key);
216 }
217 return rc;
218 }
219
220 /**
221 * Fetches an individual value from the server.
222 *
223 * @param[in] key key of object whose value to get
224 * @param[out] ret_val object that is retrieved is stored in
225 * this vector
226 * @return true on success; false otherwise
227 */
228 bool get(const std::string &key,
229 std::vector<char> &ret_val) throw (Error)
230 {
231 uint32_t flags= 0;
232 memcached_return rc;
233 size_t value_length= 0;
234
235 if (key.empty())
236 {
237 throw(Error("the key supplied is empty!", false));
238 }
239 char *value= memcached_get(&memc, key.c_str(), key.length(),
240 &value_length, &flags, &rc);
241 if (value != NULL && ret_val.empty())
242 {
243 ret_val.reserve(value_length);
244 ret_val.assign(value, value + value_length);
245 return true;
246 }
247 return false;
248 }
249
250 /**
251 * Fetches an individual from a server which is specified by
252 * the master_key parameter that is used for determining which
253 * server an object was stored in if key partitioning was
254 * used for storage.
255 *
256 * @param[in] master_key key that specifies server object is stored on
257 * @param[in] key key of object whose value to get
258 * @param[out] ret_val object that is retrieved is stored in
259 * this vector
260 * @return true on success; false otherwise
261 */
262 bool getByKey(const std::string &master_key,
263 const std::string &key,
264 std::vector<char> &ret_val) throw(Error)
265 {
266 uint32_t flags= 0;
267 memcached_return rc;
268 size_t value_length= 0;
269
270 if (master_key.empty() || key.empty())
271 {
272 throw(Error("the master key or key supplied is empty!", false));
273 }
274 char *value= memcached_get_by_key(&memc,
275 master_key.c_str(), master_key.length(),
276 key.c_str(), key.length(),
277 &value_length, &flags, &rc);
278 if (value)
279 {
280 ret_val.reserve(value_length);
281 ret_val.assign(value, value + value_length);
282 return true;
283 }
284 return false;
285 }
286
287 /**
288 * Selects multiple keys at once. This method always
289 * works asynchronously.
290 *
291 * @param[in] keys vector of keys to select
292 * @return true if all keys are found
293 */
294 bool mget(std::vector<std::string> &keys)
295 {
296 std::vector<const char *> real_keys;
297 std::vector<size_t> key_len;
298 /*
299 * Construct an array which will contain the length
300 * of each of the strings in the input vector. Also, to
301 * interface with the memcached C API, we need to convert
302 * the vector of std::string's to a vector of char *.
303 */
304 real_keys.reserve(keys.size());
305 key_len.reserve(keys.size());
306
307 std::vector<std::string>::iterator it= keys.begin();
308
309 while (it != keys.end())
310 {
311 real_keys.push_back(const_cast<char *>((*it).c_str()));
312 key_len.push_back((*it).length());
313 ++it;
314 }
315
316 /*
317 * If the std::vector of keys is empty then we cannot
318 * call memcached_mget as we will get undefined behavior.
319 */
320 if (! real_keys.empty())
321 {
322 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
323 real_keys.size());
324 return (rc == MEMCACHED_SUCCESS);
325 }
326
327 return false;
328 }
329
330 /**
331 * Writes an object to the server. If the object already exists, it will
332 * overwrite the existing object. This method always returns true
333 * when using non-blocking mode unless a network error occurs.
334 *
335 * @param[in] key key of object to write to server
336 * @param[in] value value of object to write to server
337 * @param[in] expiration time to keep the object stored in the server for
338 * @param[in] flags flags to store with the object
339 * @return true on succcess; false otherwise
340 */
341 bool set(const std::string &key,
342 const std::vector<char> &value,
343 time_t expiration,
344 uint32_t flags) throw(Error)
345 {
346 if (key.empty() || value.empty())
347 {
348 throw(Error("the key or value supplied is empty!", false));
349 }
350 memcached_return rc= memcached_set(&memc,
351 key.c_str(), key.length(),
352 &value[0], value.size(),
353 expiration, flags);
354 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
355 }
356
357 /**
358 * Writes an object to a server specified by the master_key parameter.
359 * If the object already exists, it will overwrite the existing object.
360 *
361 * @param[in] master_key key that specifies server to write to
362 * @param[in] key key of object to write to server
363 * @param[in] value value of object to write to server
364 * @param[in] expiration time to keep the object stored in the server for
365 * @param[in] flags flags to store with the object
366 * @return true on succcess; false otherwise
367 */
368 bool setByKey(const std::string &master_key,
369 const std::string &key,
370 const std::vector<char> &value,
371 time_t expiration,
372 uint32_t flags) throw(Error)
373 {
374 if (master_key.empty() ||
375 key.empty() ||
376 value.empty())
377 {
378 throw(Error("the key or value supplied is empty!", false));
379 }
380 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
381 master_key.length(),
382 key.c_str(), key.length(),
383 &value[0], value.size(),
384 expiration,
385 flags);
386 return (rc == MEMCACHED_SUCCESS);
387 }
388
389 /**
390 * Writes a list of objects to the server. Objects are specified by
391 * 2 vectors - 1 vector of keys and 1 vector of values.
392 *
393 * @param[in] keys vector of keys of objects to write to server
394 * @param[in] values vector of values of objects to write to server
395 * @param[in] expiration time to keep the objects stored in server for
396 * @param[in] flags flags to store with the objects
397 * @return true on success; false otherwise
398 */
399 bool setAll(std::vector<std::string> &keys,
400 std::vector< std::vector<char> *> &values,
401 time_t expiration,
402 uint32_t flags) throw(Error)
403 {
404 if (keys.size() != values.size())
405 {
406 throw(Error("The number of keys and values do not match!", false));
407 }
408 bool retval= true;
409 std::vector<std::string>::iterator key_it= keys.begin();
410 std::vector< std::vector<char> *>::iterator val_it= values.begin();
411 while (key_it != keys.end())
412 {
413 retval= set((*key_it), *(*val_it), expiration, flags);
414 if (retval == false)
415 {
416 return retval;
417 }
418 ++key_it;
419 ++val_it;
420 }
421 return retval;
422 }
423
424 /**
425 * Writes a list of objects to the server. Objects are specified by
426 * a map of keys to values.
427 *
428 * @param[in] key_value_map map of keys and values to store in server
429 * @param[in] expiration time to keep the objects stored in server for
430 * @param[in] flags flags to store with the objects
431 * @return true on success; false otherwise
432 */
433 bool setAll(std::map<const std::string, std::vector<char> > &key_value_map,
434 time_t expiration,
435 uint32_t flags) throw(Error)
436 {
437 if (key_value_map.empty())
438 {
439 throw(Error("The key/values are not properly set!", false));
440 }
441 bool retval= true;
442 std::map<const std::string, std::vector<char> >::iterator it=
443 key_value_map.begin();
444 while (it != key_value_map.end())
445 {
446 retval= set(it->first, it->second, expiration, flags);
447 if (retval == false)
448 {
449 char err_buff[64];
450 sprintf(err_buff, "There was an error setting the key %s",
451 it->first.c_str());
452 throw(Error(err_buff, false));
453 }
454 ++it;
455 }
456 return true;
457 }
458
459 /**
460 * Increment the value of the object associated with the specified
461 * key by the offset given. The resulting value is saved in the value
462 * parameter.
463 *
464 * @param[in] key key of object in server whose value to increment
465 * @param[in] offset amount to increment object's value by
466 * @param[out] value store the result of the increment here
467 * @return true on success; false otherwise
468 */
469 bool increment(const std::string &key, uint32_t offset, uint64_t *value) throw(Error)
470 {
471 if (key.empty())
472 {
473 throw(Error("the key supplied is empty!", false));
474 }
475 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
476 offset, value);
477 return (rc == MEMCACHED_SUCCESS);
478 }
479
480 /**
481 * Decrement the value of the object associated with the specified
482 * key by the offset given. The resulting value is saved in the value
483 * parameter.
484 *
485 * @param[in] key key of object in server whose value to decrement
486 * @param[in] offset amount to increment object's value by
487 * @param[out] value store the result of the decrement here
488 * @return true on success; false otherwise
489 */
490 bool decrement(const std::string &key, uint32_t offset, uint64_t *value)
491 throw(Error)
492 {
493 if (key.empty())
494 {
495 throw(Error("the key supplied is empty!", false));
496 }
497 memcached_return rc= memcached_decrement(&memc, key.c_str(),
498 key.length(),
499 offset, value);
500 return (rc == MEMCACHED_SUCCESS);
501 }
502
503
504 /**
505 * Add an object with the specified key and value to the server. This
506 * function returns false if the object already exists on the server.
507 *
508 * @param[in] key key of object to add
509 * @param[in] value of object to add
510 * @return true on success; false otherwise
511 */
512 bool add(const std::string &key, const std::vector<char> &value)
513 throw(Error)
514 {
515 if (key.empty() || value.empty())
516 {
517 throw(Error("the key or value supplied is empty!", false));
518 }
519 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
520 &value[0], value.size(), 0, 0);
521 return (rc == MEMCACHED_SUCCESS);
522 }
523
524 /**
525 * Add an object with the specified key and value to the server. This
526 * function returns false if the object already exists on the server. The
527 * server to add the object to is specified by the master_key parameter.
528 *
529 * @param[in[ master_key key of server to add object to
530 * @param[in] key key of object to add
531 * @param[in] value of object to add
532 * @return true on success; false otherwise
533 */
534 bool addByKey(const std::string &master_key,
535 const std::string &key,
536 const std::vector<char> &value) throw(Error)
537 {
538 if (master_key.empty() ||
539 key.empty() ||
540 value.empty())
541 {
542 throw(Error("the master key or key supplied is empty!", false));
543 }
544 memcached_return rc= memcached_add_by_key(&memc,
545 master_key.c_str(),
546 master_key.length(),
547 key.c_str(),
548 key.length(),
549 &value[0],
550 value.size(),
551 0, 0);
552 return (rc == MEMCACHED_SUCCESS);
553 }
554
555 /**
556 * Replaces an object on the server. This method only succeeds
557 * if the object is already present on the server.
558 *
559 * @param[in] key key of object to replace
560 * @param[in[ value value to replace object with
561 * @return true on success; false otherwise
562 */
563 bool replace(const std::string &key, const std::vector<char> &value) throw(Error)
564 {
565 if (key.empty() ||
566 value.empty())
567 {
568 throw(Error("the key or value supplied is empty!", false));
569 }
570 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
571 &value[0], value.size(),
572 0, 0);
573 return (rc == MEMCACHED_SUCCESS);
574 }
575
576 /**
577 * Replaces an object on the server. This method only succeeds
578 * if the object is already present on the server. The server
579 * to replace the object on is specified by the master_key param.
580 *
581 * @param[in] master_key key of server to replace object on
582 * @param[in] key key of object to replace
583 * @param[in[ value value to replace object with
584 * @return true on success; false otherwise
585 */
586 bool replaceByKey(const std::string &master_key,
587 const std::string &key,
588 const std::vector<char> &value)
589 {
590 if (master_key.empty() ||
591 key.empty() ||
592 value.empty())
593 {
594 throw(Error("the master key or key supplied is empty!", false));
595 }
596 memcached_return rc= memcached_replace_by_key(&memc,
597 master_key.c_str(),
598 master_key.length(),
599 key.c_str(),
600 key.length(),
601 &value[0],
602 value.size(),
603 0, 0);
604 return (rc == MEMCACHED_SUCCESS);
605 }
606
607 /**
608 * Places a segment of data before the last piece of data stored.
609 *
610 * @param[in] key key of object whose value we will prepend data to
611 * @param[in] value data to prepend to object's value
612 * @return true on success; false otherwise
613 */
614 bool prepend(const std::string &key, const std::vector<char> &value)
615 throw(Error)
616 {
617 if (key.empty() || value.empty())
618 {
619 throw(Error("the key or value supplied is empty!", false));
620 }
621 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
622 &value[0], value.size(), 0, 0);
623 return (rc == MEMCACHED_SUCCESS);
624 }
625
626 /**
627 * Places a segment of data before the last piece of data stored. The
628 * server on which the object where we will be prepending data is stored
629 * on is specified by the master_key parameter.
630 *
631 * @param[in] master_key key of server where object is stored
632 * @param[in] key key of object whose value we will prepend data to
633 * @param[in] value data to prepend to object's value
634 * @return true on success; false otherwise
635 */
636 bool prependByKey(const std::string &master_key,
637 const std::string &key,
638 const std::vector<char> &value)
639 throw(Error)
640 {
641 if (master_key.empty() ||
642 key.empty() ||
643 value.empty())
644 {
645 throw(Error("the master key or key supplied is empty!", false));
646 }
647 memcached_return rc= memcached_prepend_by_key(&memc,
648 master_key.c_str(),
649 master_key.length(),
650 key.c_str(),
651 key.length(),
652 &value[0],
653 value.size(),
654 0,
655 0);
656 return (rc == MEMCACHED_SUCCESS);
657 }
658
659 /**
660 * Places a segment of data at the end of the last piece of data stored.
661 *
662 * @param[in] key key of object whose value we will append data to
663 * @param[in] value data to append to object's value
664 * @return true on success; false otherwise
665 */
666 bool append(const std::string &key, const std::vector<char> &value)
667 throw(Error)
668 {
669 if (key.empty() || value.empty())
670 {
671 throw(Error("the key or value supplied is empty!", false));
672 }
673 memcached_return rc= memcached_append(&memc,
674 key.c_str(),
675 key.length(),
676 &value[0],
677 value.size(),
678 0, 0);
679 return (rc == MEMCACHED_SUCCESS);
680 }
681
682 /**
683 * Places a segment of data at the end of the last piece of data stored. The
684 * server on which the object where we will be appending data is stored
685 * on is specified by the master_key parameter.
686 *
687 * @param[in] master_key key of server where object is stored
688 * @param[in] key key of object whose value we will append data to
689 * @param[in] value data to append to object's value
690 * @return true on success; false otherwise
691 */
692 bool appendByKey(const std::string &master_key,
693 const std::string &key,
694 const std::vector<char> &value)
695 throw(Error)
696 {
697 if (master_key.empty() ||
698 key.empty() ||
699 value.empty())
700 {
701 throw(Error("the master key or key supplied is empty!", false));
702 }
703 memcached_return rc= memcached_append_by_key(&memc,
704 master_key.c_str(),
705 master_key.length(),
706 key.c_str(),
707 key.length(),
708 &value[0],
709 value.size(),
710 0, 0);
711 return (rc == MEMCACHED_SUCCESS);
712 }
713
714 /**
715 * Overwrite data in the server as long as the cas_arg value
716 * is still the same in the server.
717 *
718 * @param[in] key key of object in server
719 * @param[in] value value to store for object in server
720 * @param[in] cas_arg "cas" value
721 */
722 bool cas(const std::string &key,
723 const std::vector<char> &value,
724 uint64_t cas_arg) throw(Error)
725 {
726 if (key.empty() || value.empty())
727 {
728 throw(Error("the key or value supplied is empty!", false));
729 }
730 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
731 &value[0], value.size(),
732 0, 0, cas_arg);
733 return (rc == MEMCACHED_SUCCESS);
734 }
735
736 /**
737 * Overwrite data in the server as long as the cas_arg value
738 * is still the same in the server. The server to use is
739 * specified by the master_key parameter.
740 *
741 * @param[in] master_key specifies server to operate on
742 * @param[in] key key of object in server
743 * @param[in] value value to store for object in server
744 * @param[in] cas_arg "cas" value
745 */
746 bool casByKey(const std::string &master_key,
747 const std::string &key,
748 const std::vector<char> &value,
749 uint64_t cas_arg) throw(Error)
750 {
751 if (master_key.empty() ||
752 key.empty() ||
753 value.empty())
754 {
755 throw(Error("the master key, key or value supplied is empty!", false));
756 }
757 memcached_return rc= memcached_cas_by_key(&memc,
758 master_key.c_str(),
759 master_key.length(),
760 key.c_str(),
761 key.length(),
762 &value[0],
763 value.size(),
764 0, 0, cas_arg);
765 return (rc == MEMCACHED_SUCCESS);
766 }
767
768 /**
769 * Delete an object from the server specified by the key given.
770 *
771 * @param[in] key key of object to delete
772 * @return true on success; false otherwise
773 */
774 bool remove(const std::string &key) throw(Error)
775 {
776 if (key.empty())
777 {
778 throw(Error("the key supplied is empty!", false));
779 }
780 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
781 return (rc == MEMCACHED_SUCCESS);
782 }
783
784 /**
785 * Delete an object from the server specified by the key given.
786 *
787 * @param[in] key key of object to delete
788 * @param[in] expiration time to delete the object after
789 * @return true on success; false otherwise
790 */
791 bool remove(const std::string &key,
792 time_t expiration) throw(Error)
793 {
794 if (key.empty())
795 {
796 throw(Error("the key supplied is empty!", false));
797 }
798 memcached_return rc= memcached_delete(&memc,
799 key.c_str(),
800 key.length(),
801 expiration);
802 return (rc == MEMCACHED_SUCCESS);
803 }
804
805 /**
806 * Delete an object from the server specified by the key given.
807 *
808 * @param[in] master_key specifies server to remove object from
809 * @param[in] key key of object to delete
810 * @return true on success; false otherwise
811 */
812 bool removeByKey(const std::string &master_key,
813 const std::string &key) throw(Error)
814 {
815 if (master_key.empty() || key.empty())
816 {
817 throw(Error("the master key or key supplied is empty!", false));
818 }
819 memcached_return rc= memcached_delete_by_key(&memc,
820 master_key.c_str(),
821 master_key.length(),
822 key.c_str(),
823 key.length(),
824 0);
825 return (rc == MEMCACHED_SUCCESS);
826 }
827
828 /**
829 * Delete an object from the server specified by the key given.
830 *
831 * @param[in] master_key specifies server to remove object from
832 * @param[in] key key of object to delete
833 * @param[in] expiration time to delete the object after
834 * @return true on success; false otherwise
835 */
836 bool removeByKey(const std::string &master_key,
837 const std::string &key,
838 time_t expiration) throw(Error)
839 {
840 if (master_key.empty() || key.empty())
841 {
842 throw(Error("the master key or key supplied is empty!", false));
843 }
844 memcached_return rc= memcached_delete_by_key(&memc,
845 master_key.c_str(),
846 master_key.length(),
847 key.c_str(),
848 key.length(),
849 expiration);
850 return (rc == MEMCACHED_SUCCESS);
851 }
852
853 /**
854 * Wipe the contents of memcached servers.
855 *
856 * @param[in] expiration time to wait until wiping contents of
857 * memcached servers
858 * @return true on success; false otherwise
859 */
860 bool flush(time_t expiration)
861 {
862 memcached_return rc= memcached_flush(&memc, expiration);
863 return (rc == MEMCACHED_SUCCESS);
864 }
865
866 /**
867 * Callback function for result sets. It passes the result
868 * sets to the list of functions provided.
869 *
870 * @param[in] callback list of callback functions
871 * @param[in] context pointer to memory reference that is
872 * supplied to the calling function
873 * @param[in] num_of_callbacks number of callback functions
874 * @return true on success; false otherwise
875 */
876 bool fetchExecute(memcached_execute_function *callback,
877 void *context,
878 unsigned int num_of_callbacks)
879 {
880 memcached_return rc= memcached_fetch_execute(&memc,
881 callback,
882 context,
883 num_of_callbacks);
884 return (rc == MEMCACHED_SUCCESS);
885 }
886
887 /**
888 * Get the library version string.
889 * @return std::string containing a copy of the library version string.
890 */
891 const std::string libVersion() const
892 {
893 const char *ver= memcached_lib_version();
894 const std::string version(ver);
895 return version;
896 }
897
898 private:
899
900 std::string servers_list;
901 memcached_st memc;
902 memcached_server_st *servers;
903 memcached_result_st result;
904 };
905
906 }
907
908 #endif /* LIBMEMCACHEDPP_H */