2 * Summary: C++ interface for memcached server
4 * Copy: See Copyright for the status of this software.
6 * Authors: Padraig O'Sullivan <osullivan.padraig@gmail.com>
7 * Patrick Galbraith <patg@patg.net>
12 * @brief Libmemcached C++ interface
15 #ifndef LIBMEMCACHEDPP_H
16 #define LIBMEMCACHEDPP_H
18 #include <libmemcached/memcached.h>
30 * This is the core memcached library (if later, other objects
31 * are needed, they will be created from this class).
42 memcached_create(&memc);
45 Memcache(memcached_st *clone)
50 memcached_clone(&memc, clone);
53 Memcache(const Memcache &rhs)
58 memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
63 memcached_free(&memc);
67 * Get the internal memcached_st *
69 memcached_st &getImpl()
75 * Get the internal memcached_st *
77 const memcached_st &getImpl() const
83 * Return an error string for the given return structure.
85 * @param[in] rc a memcached_return structure
86 * @return error string corresponding to given return code in the library.
88 const std::string getError(memcached_return rc) const
90 /* first parameter to strerror is unused */
91 return memcached_strerror(NULL, rc);
95 * Fetches an individual value from the server. mget() must always
96 * be called before using this method.
98 * @param[in] key key of object to fetch
99 * @param[out] ret_val store returned object in this vector
100 * @return a memcached return structure
102 memcached_return fetch(std::string &key,
103 std::vector<char> &ret_val)
105 char ret_key[MEMCACHED_MAX_KEY];
106 size_t value_length= 0;
107 size_t key_length= 0;
110 char *value= memcached_fetch(&memc, ret_key, &key_length,
111 &value_length, &flags, &rc);
112 if (value && ret_val.empty())
114 ret_val.reserve(value_length);
115 ret_val.assign(value, value + value_length);
122 * Fetches an individual value from the server.
124 * @param[in] key key of object whose value to get
125 * @param[out] ret_val object that is retrieved is stored in
127 * @return true on success; false otherwise
129 bool get(const std::string &key,
130 std::vector<char> &ret_val)
134 size_t value_length= 0;
140 char *value= memcached_get(&memc, key.c_str(), key.length(),
141 &value_length, &flags, &rc);
142 if (value != NULL && ret_val.empty())
144 ret_val.reserve(value_length);
145 ret_val.assign(value, value + value_length);
152 * Fetches an individual from a server which is specified by
153 * the master_key parameter that is used for determining which
154 * server an object was stored in if key partitioning was
157 * @param[in] master_key key that specifies server object is stored on
158 * @param[in] key key of object whose value to get
159 * @param[out] ret_val object that is retrieved is stored in
161 * @return true on success; false otherwise
163 bool getByKey(const std::string &master_key,
164 const std::string &key,
165 std::vector<char> &ret_val)
169 size_t value_length= 0;
171 if (master_key.empty() || key.empty())
175 char *value= memcached_get_by_key(&memc,
176 master_key.c_str(), master_key.length(),
177 key.c_str(), key.length(),
178 &value_length, &flags, &rc);
181 ret_val.reserve(value_length);
182 ret_val.assign(value, value + value_length);
189 * Selects multiple keys at once. This method always
190 * works asynchronously.
192 * @param[in] keys vector of keys to select
193 * @return true if all keys are found
195 bool mget(std::vector<std::string> &keys)
197 std::vector<const char *> real_keys;
198 std::vector<size_t> key_len;
200 * Construct an array which will contain the length
201 * of each of the strings in the input vector. Also, to
202 * interface with the memcached C API, we need to convert
203 * the vector of std::string's to a vector of char *.
205 real_keys.reserve(keys.size());
206 key_len.reserve(keys.size());
208 std::vector<std::string>::iterator it= keys.begin();
210 while (it != keys.end())
212 real_keys.push_back(const_cast<char *>((*it).c_str()));
213 key_len.push_back((*it).length());
218 * If the std::vector of keys is empty then we cannot
219 * call memcached_mget as we will get undefined behavior.
221 if (! real_keys.empty())
223 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
225 return (rc == MEMCACHED_SUCCESS);
232 * Writes an object to the server. If the object already exists, it will
233 * overwrite the existing object. This method always returns true
234 * when using non-blocking mode unless a network error occurs.
236 * @param[in] key key of object to write to server
237 * @param[in] value value of object to write to server
238 * @param[in] expiration time to keep the object stored in the server for
239 * @param[in] flags flags to store with the object
240 * @return true on succcess; false otherwise
242 bool set(const std::string &key,
243 const std::vector<char> &value,
247 if (key.empty() || value.empty())
251 memcached_return rc= memcached_set(&memc,
252 key.c_str(), key.length(),
253 &value[0], value.size(),
255 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
259 * Writes an object to a server specified by the master_key parameter.
260 * If the object already exists, it will overwrite the existing object.
262 * @param[in] master_key key that specifies server to write to
263 * @param[in] key key of object to write to server
264 * @param[in] value value of object to write to server
265 * @param[in] expiration time to keep the object stored in the server for
266 * @param[in] flags flags to store with the object
267 * @return true on succcess; false otherwise
269 bool setByKey(const std::string &master_key,
270 const std::string &key,
271 const std::vector<char> &value,
275 if (master_key.empty() ||
281 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
283 key.c_str(), key.length(),
284 &value[0], value.size(),
287 return (rc == MEMCACHED_SUCCESS);
291 * Writes a list of objects to the server. Objects are specified by
292 * 2 vectors - 1 vector of keys and 1 vector of values.
294 * @param[in] keys vector of keys of objects to write to server
295 * @param[in] values vector of values of objects to write to server
296 * @param[in] expiration time to keep the objects stored in server for
297 * @param[in] flags flags to store with the objects
298 * @return true on success; false otherwise
300 bool setAll(std::vector<std::string> &keys,
301 std::vector< std::vector<char> *> &values,
305 if (keys.size() != values.size())
310 std::vector<std::string>::iterator key_it= keys.begin();
311 std::vector< std::vector<char> *>::iterator val_it= values.begin();
312 while (key_it != keys.end())
314 retval= set((*key_it), *(*val_it), expiration, flags);
326 * Writes a list of objects to the server. Objects are specified by
327 * a map of keys to values.
329 * @param[in] key_value_map map of keys and values to store in server
330 * @param[in] expiration time to keep the objects stored in server for
331 * @param[in] flags flags to store with the objects
332 * @return true on success; false otherwise
334 bool setAll(std::map<const std::string, std::vector<char> > &key_value_map,
338 if (key_value_map.empty())
343 std::map<const std::string, std::vector<char> >::iterator it=
344 key_value_map.begin();
345 while (it != key_value_map.end())
347 retval= set(it->first, it->second, expiration, flags);
358 * Increment the value of the object associated with the specified
359 * key by the offset given. The resulting value is saved in the value
362 * @param[in] key key of object in server whose value to increment
363 * @param[in] offset amount to increment object's value by
364 * @param[out] value store the result of the increment here
365 * @return true on success; false otherwise
367 bool increment(const std::string &key, uint32_t offset, uint64_t *value)
373 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
375 return (rc == MEMCACHED_SUCCESS);
379 * Decrement the value of the object associated with the specified
380 * key by the offset given. The resulting value is saved in the value
383 * @param[in] key key of object in server whose value to decrement
384 * @param[in] offset amount to increment object's value by
385 * @param[out] value store the result of the decrement here
386 * @return true on success; false otherwise
388 bool decrement(const std::string &key, uint32_t offset, uint64_t *value)
394 memcached_return rc= memcached_decrement(&memc, key.c_str(),
397 return (rc == MEMCACHED_SUCCESS);
402 * Add an object with the specified key and value to the server. This
403 * function returns false if the object already exists on the server.
405 * @param[in] key key of object to add
406 * @param[in] value of object to add
407 * @return true on success; false otherwise
409 bool add(const std::string &key, const std::vector<char> &value)
411 if (key.empty() || value.empty())
415 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
416 &value[0], value.size(), 0, 0);
417 return (rc == MEMCACHED_SUCCESS);
421 * Add an object with the specified key and value to the server. This
422 * function returns false if the object already exists on the server. The
423 * server to add the object to is specified by the master_key parameter.
425 * @param[in[ master_key key of server to add object to
426 * @param[in] key key of object to add
427 * @param[in] value of object to add
428 * @return true on success; false otherwise
430 bool addByKey(const std::string &master_key,
431 const std::string &key,
432 const std::vector<char> &value)
434 if (master_key.empty() ||
440 memcached_return rc= memcached_add_by_key(&memc,
448 return (rc == MEMCACHED_SUCCESS);
452 * Replaces an object on the server. This method only succeeds
453 * if the object is already present on the server.
455 * @param[in] key key of object to replace
456 * @param[in[ value value to replace object with
457 * @return true on success; false otherwise
459 bool replace(const std::string &key, const std::vector<char> &value)
466 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
467 &value[0], value.size(),
469 return (rc == MEMCACHED_SUCCESS);
473 * Replaces an object on the server. This method only succeeds
474 * if the object is already present on the server. The server
475 * to replace the object on is specified by the master_key param.
477 * @param[in] master_key key of server to replace object on
478 * @param[in] key key of object to replace
479 * @param[in[ value value to replace object with
480 * @return true on success; false otherwise
482 bool replaceByKey(const std::string &master_key,
483 const std::string &key,
484 const std::vector<char> &value)
486 if (master_key.empty() ||
492 memcached_return rc= memcached_replace_by_key(&memc,
500 return (rc == MEMCACHED_SUCCESS);
504 * Places a segment of data before the last piece of data stored.
506 * @param[in] key key of object whose value we will prepend data to
507 * @param[in] value data to prepend to object's value
508 * @return true on success; false otherwise
510 bool prepend(const std::string &key, const std::vector<char> &value)
512 if (key.empty() || value.empty())
516 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
517 &value[0], value.size(), 0, 0);
518 return (rc == MEMCACHED_SUCCESS);
522 * Places a segment of data before the last piece of data stored. The
523 * server on which the object where we will be prepending data is stored
524 * on is specified by the master_key parameter.
526 * @param[in] master_key key of server where object is stored
527 * @param[in] key key of object whose value we will prepend data to
528 * @param[in] value data to prepend to object's value
529 * @return true on success; false otherwise
531 bool prependByKey(const std::string &master_key,
532 const std::string &key,
533 const std::vector<char> &value)
535 if (master_key.empty() ||
541 memcached_return rc= memcached_prepend_by_key(&memc,
550 return (rc == MEMCACHED_SUCCESS);
554 * Places a segment of data at the end of the last piece of data stored.
556 * @param[in] key key of object whose value we will append data to
557 * @param[in] value data to append to object's value
558 * @return true on success; false otherwise
560 bool append(const std::string &key, const std::vector<char> &value)
562 if (key.empty() || value.empty())
566 memcached_return rc= memcached_append(&memc,
572 return (rc == MEMCACHED_SUCCESS);
576 * Places a segment of data at the end of the last piece of data stored. The
577 * server on which the object where we will be appending data is stored
578 * on is specified by the master_key parameter.
580 * @param[in] master_key key of server where object is stored
581 * @param[in] key key of object whose value we will append data to
582 * @param[in] value data to append to object's value
583 * @return true on success; false otherwise
585 bool appendByKey(const std::string &master_key,
586 const std::string &key,
587 const std::vector<char> &value)
589 if (master_key.empty() ||
595 memcached_return rc= memcached_append_by_key(&memc,
603 return (rc == MEMCACHED_SUCCESS);
607 * Overwrite data in the server as long as the cas_arg value
608 * is still the same in the server.
610 * @param[in] key key of object in server
611 * @param[in] value value to store for object in server
612 * @param[in] cas_arg "cas" value
614 bool cas(const std::string &key,
615 const std::vector<char> &value,
618 if (key.empty() || value.empty())
622 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
623 &value[0], value.size(),
625 return (rc == MEMCACHED_SUCCESS);
629 * Overwrite data in the server as long as the cas_arg value
630 * is still the same in the server. The server to use is
631 * specified by the master_key parameter.
633 * @param[in] master_key specifies server to operate on
634 * @param[in] key key of object in server
635 * @param[in] value value to store for object in server
636 * @param[in] cas_arg "cas" value
638 bool casByKey(const std::string &master_key,
639 const std::string &key,
640 const std::vector<char> &value,
643 if (master_key.empty() ||
649 memcached_return rc= memcached_cas_by_key(&memc,
657 return (rc == MEMCACHED_SUCCESS);
661 * Delete an object from the server specified by the key given.
663 * @param[in] key key of object to delete
664 * @return true on success; false otherwise
666 bool remove(const std::string &key)
672 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
673 return (rc == MEMCACHED_SUCCESS);
677 * Delete an object from the server specified by the key given.
679 * @param[in] key key of object to delete
680 * @param[in] expiration time to delete the object after
681 * @return true on success; false otherwise
683 bool remove(const std::string &key,
690 memcached_return rc= memcached_delete(&memc,
694 return (rc == MEMCACHED_SUCCESS);
698 * Delete an object from the server specified by the key given.
700 * @param[in] master_key specifies server to remove object from
701 * @param[in] key key of object to delete
702 * @return true on success; false otherwise
704 bool removeByKey(const std::string &master_key,
705 const std::string &key)
707 if (master_key.empty() || key.empty())
711 memcached_return rc= memcached_delete_by_key(&memc,
717 return (rc == MEMCACHED_SUCCESS);
721 * Delete an object from the server specified by the key given.
723 * @param[in] master_key specifies server to remove object from
724 * @param[in] key key of object to delete
725 * @param[in] expiration time to delete the object after
726 * @return true on success; false otherwise
728 bool removeByKey(const std::string &master_key,
729 const std::string &key,
732 if (master_key.empty() || key.empty())
736 memcached_return rc= memcached_delete_by_key(&memc,
742 return (rc == MEMCACHED_SUCCESS);
746 * Wipe the contents of memcached servers.
748 * @param[in] expiration time to wait until wiping contents of
750 * @return true on success; false otherwise
752 bool flush(time_t expiration)
754 memcached_return rc= memcached_flush(&memc, expiration);
755 return (rc == MEMCACHED_SUCCESS);
759 * Callback function for result sets. It passes the result
760 * sets to the list of functions provided.
762 * @param[in] callback list of callback functions
763 * @param[in] context pointer to memory reference that is
764 * supplied to the calling function
765 * @param[in] num_of_callbacks number of callback functions
766 * @return true on success; false otherwise
768 bool fetchExecute(memcached_execute_function *callback,
770 unsigned int num_of_callbacks)
772 memcached_return rc= memcached_fetch_execute(&memc,
776 return (rc == MEMCACHED_SUCCESS);
780 * Get the library version string.
781 * @return std::string containing a copy of the library version string.
783 const std::string libVersion() const
785 const char *ver= memcached_lib_version();
786 const std::string version(ver);
793 memcached_result_st result;
798 #endif /* LIBMEMCACHEDPP_H */