Merge from Trond
[m6w6/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 memcached_server_push(&memc, servers);
168 return (servers == NULL);
169 }
170
171 /**
172 * Add a server to the list of memcached servers to use.
173 *
174 * @param[in] server_name name of the server to add
175 * @param[in] port port number of server to add
176 * @return true on success; false otherwise
177 */
178 bool addServer(const std::string &server_name, unsigned int port)
179 {
180 memcached_return rc;
181 std::ostringstream strstm;
182 servers_list.append(",");
183 servers_list.append(server_name);
184 servers_list.append(":");
185 strstm << port;
186 servers_list.append(strstm.str());
187 servers= memcached_server_list_append(servers,
188 server_name.c_str(),
189 port,
190 &rc);
191 memcached_server_push(&memc, servers);
192 return (rc == MEMCACHED_SUCCESS);
193 }
194
195 /**
196 * Remove a server from the list of memcached servers to use.
197 *
198 * @param[in] server_name name of the server to remove
199 * @param[in] port port number of server to remove
200 * @return true on success; false otherwise
201 */
202 bool removeServer(const std::string &server_name, size_t port)
203 {
204 std::string tmp_str;
205 std::ostringstream strstm;
206 tmp_str.append(",");
207 tmp_str.append(server_name);
208 tmp_str.append(":");
209 strstm << port;
210 tmp_str.append(strstm.str());
211 memcached_server_st *server= memcached_servers_parse(tmp_str.c_str());
212 memcached_return rc= memcached_server_remove(server);
213 return (rc == MEMCACHED_SUCCESS);
214 }
215
216 /**
217 * Fetches an individual value from the server. mget() must always
218 * be called before using this method.
219 *
220 * @param[in] key key of object to fetch
221 * @param[out] ret_val store returned object in this vector
222 * @return a memcached return structure
223 */
224 memcached_return fetch(std::string &key,
225 std::vector<char> &ret_val)
226 {
227 char ret_key[MEMCACHED_MAX_KEY];
228 size_t value_length= 0;
229 size_t key_length= 0;
230 memcached_return rc;
231 uint32_t flags= 0;
232 char *value= memcached_fetch(&memc, ret_key, &key_length,
233 &value_length, &flags, &rc);
234 if (value && ret_val.empty())
235 {
236 ret_val.reserve(value_length);
237 ret_val.assign(value, value + value_length);
238 key.assign(ret_key);
239 }
240 return rc;
241 }
242
243 /**
244 * Fetches an individual value from the server.
245 *
246 * @param[in] key key of object whose value to get
247 * @param[out] ret_val object that is retrieved is stored in
248 * this vector
249 * @return true on success; false otherwise
250 */
251 bool get(const std::string &key,
252 std::vector<char> &ret_val) throw (Error)
253 {
254 uint32_t flags= 0;
255 memcached_return rc;
256 size_t value_length= 0;
257
258 if (key.empty())
259 {
260 throw(Error("the key supplied is empty!", false));
261 }
262 char *value= memcached_get(&memc, key.c_str(), key.length(),
263 &value_length, &flags, &rc);
264 if (value != NULL && ret_val.empty())
265 {
266 ret_val.reserve(value_length);
267 ret_val.assign(value, value + value_length);
268 return true;
269 }
270 return false;
271 }
272
273 /**
274 * Fetches an individual from a server which is specified by
275 * the master_key parameter that is used for determining which
276 * server an object was stored in if key partitioning was
277 * used for storage.
278 *
279 * @param[in] master_key key that specifies server object is stored on
280 * @param[in] key key of object whose value to get
281 * @param[out] ret_val object that is retrieved is stored in
282 * this vector
283 * @return true on success; false otherwise
284 */
285 bool getByKey(const std::string &master_key,
286 const std::string &key,
287 std::vector<char> &ret_val) throw(Error)
288 {
289 uint32_t flags= 0;
290 memcached_return rc;
291 size_t value_length= 0;
292
293 if (master_key.empty() || key.empty())
294 {
295 throw(Error("the master key or key supplied is empty!", false));
296 }
297 char *value= memcached_get_by_key(&memc,
298 master_key.c_str(), master_key.length(),
299 key.c_str(), key.length(),
300 &value_length, &flags, &rc);
301 if (value)
302 {
303 ret_val.reserve(value_length);
304 ret_val.assign(value, value + value_length);
305 return true;
306 }
307 return false;
308 }
309
310 /**
311 * Selects multiple keys at once. This method always
312 * works asynchronously.
313 *
314 * @param[in] keys vector of keys to select
315 * @return true if all keys are found
316 */
317 bool mget(std::vector<std::string> &keys)
318 {
319 std::vector<const char *> real_keys;
320 std::vector<size_t> key_len;
321 /*
322 * Construct an array which will contain the length
323 * of each of the strings in the input vector. Also, to
324 * interface with the memcached C API, we need to convert
325 * the vector of std::string's to a vector of char *.
326 */
327 real_keys.reserve(keys.size());
328 key_len.reserve(keys.size());
329
330 std::vector<std::string>::iterator it= keys.begin();
331
332 while (it != keys.end())
333 {
334 real_keys.push_back(const_cast<char *>((*it).c_str()));
335 key_len.push_back((*it).length());
336 ++it;
337 }
338
339 /*
340 * If the std::vector of keys is empty then we cannot
341 * call memcached_mget as we will get undefined behavior.
342 */
343 if (! real_keys.empty())
344 {
345 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
346 real_keys.size());
347 return (rc == MEMCACHED_SUCCESS);
348 }
349
350 return false;
351 }
352
353 /**
354 * Writes an object to the server. If the object already exists, it will
355 * overwrite the existing object. This method always returns true
356 * when using non-blocking mode unless a network error occurs.
357 *
358 * @param[in] key key of object to write to server
359 * @param[in] value value of object to write to server
360 * @param[in] expiration time to keep the object stored in the server for
361 * @param[in] flags flags to store with the object
362 * @return true on succcess; false otherwise
363 */
364 bool set(const std::string &key,
365 const std::vector<char> &value,
366 time_t expiration,
367 uint32_t flags) throw(Error)
368 {
369 if (key.empty() || value.empty())
370 {
371 throw(Error("the key or value supplied is empty!", false));
372 }
373 memcached_return rc= memcached_set(&memc,
374 key.c_str(), key.length(),
375 &value[0], value.size(),
376 expiration, flags);
377 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
378 }
379
380 /**
381 * Writes an object to a server specified by the master_key parameter.
382 * If the object already exists, it will overwrite the existing object.
383 *
384 * @param[in] master_key key that specifies server to write to
385 * @param[in] key key of object to write to server
386 * @param[in] value value of object to write to server
387 * @param[in] expiration time to keep the object stored in the server for
388 * @param[in] flags flags to store with the object
389 * @return true on succcess; false otherwise
390 */
391 bool setByKey(const std::string &master_key,
392 const std::string &key,
393 const std::vector<char> &value,
394 time_t expiration,
395 uint32_t flags) throw(Error)
396 {
397 if (master_key.empty() ||
398 key.empty() ||
399 value.empty())
400 {
401 throw(Error("the key or value supplied is empty!", false));
402 }
403 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
404 master_key.length(),
405 key.c_str(), key.length(),
406 &value[0], value.size(),
407 expiration,
408 flags);
409 return (rc == MEMCACHED_SUCCESS);
410 }
411
412 /**
413 * Writes a list of objects to the server. Objects are specified by
414 * 2 vectors - 1 vector of keys and 1 vector of values.
415 *
416 * @param[in] keys vector of keys of objects to write to server
417 * @param[in] values vector of values of objects to write to server
418 * @param[in] expiration time to keep the objects stored in server for
419 * @param[in] flags flags to store with the objects
420 * @return true on success; false otherwise
421 */
422 bool setAll(std::vector<std::string> &keys,
423 std::vector< std::vector<char> *> &values,
424 time_t expiration,
425 uint32_t flags) throw(Error)
426 {
427 if (keys.size() != values.size())
428 {
429 throw(Error("The number of keys and values do not match!", false));
430 }
431 bool retval= true;
432 std::vector<std::string>::iterator key_it= keys.begin();
433 std::vector< std::vector<char> *>::iterator val_it= values.begin();
434 while (key_it != keys.end())
435 {
436 retval= set((*key_it), *(*val_it), expiration, flags);
437 if (retval == false)
438 {
439 return retval;
440 }
441 ++key_it;
442 ++val_it;
443 }
444 return retval;
445 }
446
447 /**
448 * Writes a list of objects to the server. Objects are specified by
449 * a map of keys to values.
450 *
451 * @param[in] key_value_map map of keys and values to store in server
452 * @param[in] expiration time to keep the objects stored in server for
453 * @param[in] flags flags to store with the objects
454 * @return true on success; false otherwise
455 */
456 bool setAll(std::map<const std::string, std::vector<char> > &key_value_map,
457 time_t expiration,
458 uint32_t flags) throw(Error)
459 {
460 if (key_value_map.empty())
461 {
462 throw(Error("The key/values are not properly set!", false));
463 }
464 bool retval= true;
465 std::map<const std::string, std::vector<char> >::iterator it=
466 key_value_map.begin();
467 while (it != key_value_map.end())
468 {
469 retval= set(it->first, it->second, expiration, flags);
470 if (retval == false)
471 {
472 std::string err_buff("There was an error setting the key ");
473 err_buff.append(it->first);
474 throw(Error(err_buff, false));
475 }
476 ++it;
477 }
478 return true;
479 }
480
481 /**
482 * Increment the value of the object associated with the specified
483 * key by the offset given. The resulting value is saved in the value
484 * parameter.
485 *
486 * @param[in] key key of object in server whose value to increment
487 * @param[in] offset amount to increment object's value by
488 * @param[out] value store the result of the increment here
489 * @return true on success; false otherwise
490 */
491 bool increment(const std::string &key, uint32_t offset, uint64_t *value) throw(Error)
492 {
493 if (key.empty())
494 {
495 throw(Error("the key supplied is empty!", false));
496 }
497 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
498 offset, value);
499 return (rc == MEMCACHED_SUCCESS);
500 }
501
502 /**
503 * Decrement the value of the object associated with the specified
504 * key by the offset given. The resulting value is saved in the value
505 * parameter.
506 *
507 * @param[in] key key of object in server whose value to decrement
508 * @param[in] offset amount to increment object's value by
509 * @param[out] value store the result of the decrement here
510 * @return true on success; false otherwise
511 */
512 bool decrement(const std::string &key, uint32_t offset, uint64_t *value)
513 throw(Error)
514 {
515 if (key.empty())
516 {
517 throw(Error("the key supplied is empty!", false));
518 }
519 memcached_return rc= memcached_decrement(&memc, key.c_str(),
520 key.length(),
521 offset, value);
522 return (rc == MEMCACHED_SUCCESS);
523 }
524
525
526 /**
527 * Add an object with the specified key and value to the server. This
528 * function returns false if the object already exists on the server.
529 *
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 add(const std::string &key, const std::vector<char> &value)
535 throw(Error)
536 {
537 if (key.empty() || value.empty())
538 {
539 throw(Error("the key or value supplied is empty!", false));
540 }
541 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
542 &value[0], value.size(), 0, 0);
543 return (rc == MEMCACHED_SUCCESS);
544 }
545
546 /**
547 * Add an object with the specified key and value to the server. This
548 * function returns false if the object already exists on the server. The
549 * server to add the object to is specified by the master_key parameter.
550 *
551 * @param[in[ master_key key of server to add object to
552 * @param[in] key key of object to add
553 * @param[in] value of object to add
554 * @return true on success; false otherwise
555 */
556 bool addByKey(const std::string &master_key,
557 const std::string &key,
558 const std::vector<char> &value) throw(Error)
559 {
560 if (master_key.empty() ||
561 key.empty() ||
562 value.empty())
563 {
564 throw(Error("the master key or key supplied is empty!", false));
565 }
566 memcached_return rc= memcached_add_by_key(&memc,
567 master_key.c_str(),
568 master_key.length(),
569 key.c_str(),
570 key.length(),
571 &value[0],
572 value.size(),
573 0, 0);
574 return (rc == MEMCACHED_SUCCESS);
575 }
576
577 /**
578 * Replaces an object on the server. This method only succeeds
579 * if the object is already present on the server.
580 *
581 * @param[in] key key of object to replace
582 * @param[in[ value value to replace object with
583 * @return true on success; false otherwise
584 */
585 bool replace(const std::string &key, const std::vector<char> &value) throw(Error)
586 {
587 if (key.empty() ||
588 value.empty())
589 {
590 throw(Error("the key or value supplied is empty!", false));
591 }
592 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
593 &value[0], value.size(),
594 0, 0);
595 return (rc == MEMCACHED_SUCCESS);
596 }
597
598 /**
599 * Replaces an object on the server. This method only succeeds
600 * if the object is already present on the server. The server
601 * to replace the object on is specified by the master_key param.
602 *
603 * @param[in] master_key key of server to replace object on
604 * @param[in] key key of object to replace
605 * @param[in[ value value to replace object with
606 * @return true on success; false otherwise
607 */
608 bool replaceByKey(const std::string &master_key,
609 const std::string &key,
610 const std::vector<char> &value)
611 {
612 if (master_key.empty() ||
613 key.empty() ||
614 value.empty())
615 {
616 throw(Error("the master key or key supplied is empty!", false));
617 }
618 memcached_return rc= memcached_replace_by_key(&memc,
619 master_key.c_str(),
620 master_key.length(),
621 key.c_str(),
622 key.length(),
623 &value[0],
624 value.size(),
625 0, 0);
626 return (rc == MEMCACHED_SUCCESS);
627 }
628
629 /**
630 * Places a segment of data before the last piece of data stored.
631 *
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 prepend(const std::string &key, const std::vector<char> &value)
637 throw(Error)
638 {
639 if (key.empty() || value.empty())
640 {
641 throw(Error("the key or value supplied is empty!", false));
642 }
643 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
644 &value[0], value.size(), 0, 0);
645 return (rc == MEMCACHED_SUCCESS);
646 }
647
648 /**
649 * Places a segment of data before the last piece of data stored. The
650 * server on which the object where we will be prepending data is stored
651 * on is specified by the master_key parameter.
652 *
653 * @param[in] master_key key of server where object is stored
654 * @param[in] key key of object whose value we will prepend data to
655 * @param[in] value data to prepend to object's value
656 * @return true on success; false otherwise
657 */
658 bool prependByKey(const std::string &master_key,
659 const std::string &key,
660 const std::vector<char> &value)
661 throw(Error)
662 {
663 if (master_key.empty() ||
664 key.empty() ||
665 value.empty())
666 {
667 throw(Error("the master key or key supplied is empty!", false));
668 }
669 memcached_return rc= memcached_prepend_by_key(&memc,
670 master_key.c_str(),
671 master_key.length(),
672 key.c_str(),
673 key.length(),
674 &value[0],
675 value.size(),
676 0,
677 0);
678 return (rc == MEMCACHED_SUCCESS);
679 }
680
681 /**
682 * Places a segment of data at the end of the last piece of data stored.
683 *
684 * @param[in] key key of object whose value we will append data to
685 * @param[in] value data to append to object's value
686 * @return true on success; false otherwise
687 */
688 bool append(const std::string &key, const std::vector<char> &value)
689 throw(Error)
690 {
691 if (key.empty() || value.empty())
692 {
693 throw(Error("the key or value supplied is empty!", false));
694 }
695 memcached_return rc= memcached_append(&memc,
696 key.c_str(),
697 key.length(),
698 &value[0],
699 value.size(),
700 0, 0);
701 return (rc == MEMCACHED_SUCCESS);
702 }
703
704 /**
705 * Places a segment of data at the end of the last piece of data stored. The
706 * server on which the object where we will be appending data is stored
707 * on is specified by the master_key parameter.
708 *
709 * @param[in] master_key key of server where object is stored
710 * @param[in] key key of object whose value we will append data to
711 * @param[in] value data to append to object's value
712 * @return true on success; false otherwise
713 */
714 bool appendByKey(const std::string &master_key,
715 const std::string &key,
716 const std::vector<char> &value)
717 throw(Error)
718 {
719 if (master_key.empty() ||
720 key.empty() ||
721 value.empty())
722 {
723 throw(Error("the master key or key supplied is empty!", false));
724 }
725 memcached_return rc= memcached_append_by_key(&memc,
726 master_key.c_str(),
727 master_key.length(),
728 key.c_str(),
729 key.length(),
730 &value[0],
731 value.size(),
732 0, 0);
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.
739 *
740 * @param[in] key key of object in server
741 * @param[in] value value to store for object in server
742 * @param[in] cas_arg "cas" value
743 */
744 bool cas(const std::string &key,
745 const std::vector<char> &value,
746 uint64_t cas_arg) throw(Error)
747 {
748 if (key.empty() || value.empty())
749 {
750 throw(Error("the key or value supplied is empty!", false));
751 }
752 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
753 &value[0], value.size(),
754 0, 0, cas_arg);
755 return (rc == MEMCACHED_SUCCESS);
756 }
757
758 /**
759 * Overwrite data in the server as long as the cas_arg value
760 * is still the same in the server. The server to use is
761 * specified by the master_key parameter.
762 *
763 * @param[in] master_key specifies server to operate on
764 * @param[in] key key of object in server
765 * @param[in] value value to store for object in server
766 * @param[in] cas_arg "cas" value
767 */
768 bool casByKey(const std::string &master_key,
769 const std::string &key,
770 const std::vector<char> &value,
771 uint64_t cas_arg) throw(Error)
772 {
773 if (master_key.empty() ||
774 key.empty() ||
775 value.empty())
776 {
777 throw(Error("the master key, key or value supplied is empty!", false));
778 }
779 memcached_return rc= memcached_cas_by_key(&memc,
780 master_key.c_str(),
781 master_key.length(),
782 key.c_str(),
783 key.length(),
784 &value[0],
785 value.size(),
786 0, 0, cas_arg);
787 return (rc == MEMCACHED_SUCCESS);
788 }
789
790 /**
791 * Delete an object from the server specified by the key given.
792 *
793 * @param[in] key key of object to delete
794 * @return true on success; false otherwise
795 */
796 bool remove(const std::string &key) throw(Error)
797 {
798 if (key.empty())
799 {
800 throw(Error("the key supplied is empty!", false));
801 }
802 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
803 return (rc == MEMCACHED_SUCCESS);
804 }
805
806 /**
807 * Delete an object from the server specified by the key given.
808 *
809 * @param[in] key key of object to delete
810 * @param[in] expiration time to delete the object after
811 * @return true on success; false otherwise
812 */
813 bool remove(const std::string &key,
814 time_t expiration) throw(Error)
815 {
816 if (key.empty())
817 {
818 throw(Error("the key supplied is empty!", false));
819 }
820 memcached_return rc= memcached_delete(&memc,
821 key.c_str(),
822 key.length(),
823 expiration);
824 return (rc == MEMCACHED_SUCCESS);
825 }
826
827 /**
828 * Delete an object from the server specified by the key given.
829 *
830 * @param[in] master_key specifies server to remove object from
831 * @param[in] key key of object to delete
832 * @return true on success; false otherwise
833 */
834 bool removeByKey(const std::string &master_key,
835 const std::string &key) throw(Error)
836 {
837 if (master_key.empty() || key.empty())
838 {
839 throw(Error("the master key or key supplied is empty!", false));
840 }
841 memcached_return rc= memcached_delete_by_key(&memc,
842 master_key.c_str(),
843 master_key.length(),
844 key.c_str(),
845 key.length(),
846 0);
847 return (rc == MEMCACHED_SUCCESS);
848 }
849
850 /**
851 * Delete an object from the server specified by the key given.
852 *
853 * @param[in] master_key specifies server to remove object from
854 * @param[in] key key of object to delete
855 * @param[in] expiration time to delete the object after
856 * @return true on success; false otherwise
857 */
858 bool removeByKey(const std::string &master_key,
859 const std::string &key,
860 time_t expiration) throw(Error)
861 {
862 if (master_key.empty() || key.empty())
863 {
864 throw(Error("the master key or key supplied is empty!", false));
865 }
866 memcached_return rc= memcached_delete_by_key(&memc,
867 master_key.c_str(),
868 master_key.length(),
869 key.c_str(),
870 key.length(),
871 expiration);
872 return (rc == MEMCACHED_SUCCESS);
873 }
874
875 /**
876 * Wipe the contents of memcached servers.
877 *
878 * @param[in] expiration time to wait until wiping contents of
879 * memcached servers
880 * @return true on success; false otherwise
881 */
882 bool flush(time_t expiration)
883 {
884 memcached_return rc= memcached_flush(&memc, expiration);
885 return (rc == MEMCACHED_SUCCESS);
886 }
887
888 /**
889 * Callback function for result sets. It passes the result
890 * sets to the list of functions provided.
891 *
892 * @param[in] callback list of callback functions
893 * @param[in] context pointer to memory reference that is
894 * supplied to the calling function
895 * @param[in] num_of_callbacks number of callback functions
896 * @return true on success; false otherwise
897 */
898 bool fetchExecute(memcached_execute_function *callback,
899 void *context,
900 unsigned int num_of_callbacks)
901 {
902 memcached_return rc= memcached_fetch_execute(&memc,
903 callback,
904 context,
905 num_of_callbacks);
906 return (rc == MEMCACHED_SUCCESS);
907 }
908
909 /**
910 * Get the library version string.
911 * @return std::string containing a copy of the library version string.
912 */
913 const std::string libVersion() const
914 {
915 const char *ver= memcached_lib_version();
916 const std::string version(ver);
917 return version;
918 }
919
920 private:
921
922 std::string servers_list;
923 memcached_st memc;
924 memcached_server_st *servers;
925 memcached_result_st result;
926 };
927
928 }
929
930 #endif /* LIBMEMCACHEDPP_H */