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>
31 * This is the core memcached library (if later, other objects
32 * are needed, they will be created from this class).
45 memcached_create(&memc);
48 Memcache(const std::string &in_servers_list)
50 servers_list(in_servers_list),
55 memcached_create(&memc);
56 servers= memcached_servers_parse(servers_list.c_str());
59 Memcache(const std::string &hostname,
67 memcached_create(&memc);
68 servers_list.append(hostname);
69 servers_list.append(":");
70 std::ostringstream strsmt;
72 servers_list.append(strsmt.str());
73 servers= memcached_servers_parse(servers_list.c_str());
76 Memcache(memcached_st *clone)
83 memcached_clone(&memc, clone);
86 Memcache(const Memcache &rhs)
88 servers_list(rhs.servers_list),
93 memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
94 servers= memcached_servers_parse(servers_list.c_str());
97 Memcache &operator=(const Memcache &rhs)
101 memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
102 servers= memcached_servers_parse(servers_list.c_str());
109 memcached_free(&memc);
110 memcached_server_list_free(servers);
114 * Get the internal memcached_st *
116 memcached_st &getImpl()
122 * Get the internal memcached_st *
124 const memcached_st &getImpl() const
130 * Return an error string for the given return structure.
132 * @param[in] rc a memcached_return structure
133 * @return error string corresponding to given return code in the library.
135 const std::string getError(memcached_return rc) const
137 /* first parameter to strerror is unused */
138 return memcached_strerror(NULL, rc);
142 * Return the string which contains the list of memcached servers being
145 * @return a std::string containing the list of memcached servers
147 const std::string getServersList() const
153 * Set the list of memcached servers to use.
155 * @param[in] in_servers_list list of servers
156 * @return true on success; false otherwise
158 bool setServers(const std::string &in_servers_list)
160 servers_list.assign(in_servers_list);
161 servers= memcached_servers_parse(in_servers_list.c_str());
162 return (servers == NULL);
166 * Add a server to the list of memcached servers to use.
168 * @param[in] server_name name of the server to add
169 * @param[in[ port port number of server to add
170 * @return true on success; false otherwise
172 bool addServer(const std::string &server_name, unsigned int port)
175 std::ostringstream strstm;
176 servers_list.append(",");
177 servers_list.append(server_name);
178 servers_list.append(":");
180 servers_list.append(strstm.str());
181 servers= memcached_server_list_append(servers,
185 return (rc == MEMCACHED_SUCCESS);
189 * Fetches an individual value from the server. mget() must always
190 * be called before using this method.
192 * @param[in] key key of object to fetch
193 * @param[out] ret_val store returned object in this vector
194 * @return a memcached return structure
196 memcached_return fetch(std::string &key,
197 std::vector<char> &ret_val)
199 char ret_key[MEMCACHED_MAX_KEY];
200 size_t value_length= 0;
201 size_t key_length= 0;
204 char *value= memcached_fetch(&memc, ret_key, &key_length,
205 &value_length, &flags, &rc);
206 if (value && ret_val.empty())
208 ret_val.reserve(value_length);
209 ret_val.assign(value, value + value_length);
216 * Fetches an individual value from the server.
218 * @param[in] key key of object whose value to get
219 * @param[out] ret_val object that is retrieved is stored in
221 * @return true on success; false otherwise
223 bool get(const std::string &key,
224 std::vector<char> &ret_val)
228 size_t value_length= 0;
234 char *value= memcached_get(&memc, key.c_str(), key.length(),
235 &value_length, &flags, &rc);
236 if (value != NULL && ret_val.empty())
238 ret_val.reserve(value_length);
239 ret_val.assign(value, value + value_length);
246 * Fetches an individual from a server which is specified by
247 * the master_key parameter that is used for determining which
248 * server an object was stored in if key partitioning was
251 * @param[in] master_key key that specifies server object is stored on
252 * @param[in] key key of object whose value to get
253 * @param[out] ret_val object that is retrieved is stored in
255 * @return true on success; false otherwise
257 bool getByKey(const std::string &master_key,
258 const std::string &key,
259 std::vector<char> &ret_val)
263 size_t value_length= 0;
265 if (master_key.empty() || key.empty())
269 char *value= memcached_get_by_key(&memc,
270 master_key.c_str(), master_key.length(),
271 key.c_str(), key.length(),
272 &value_length, &flags, &rc);
275 ret_val.reserve(value_length);
276 ret_val.assign(value, value + value_length);
283 * Selects multiple keys at once. This method always
284 * works asynchronously.
286 * @param[in] keys vector of keys to select
287 * @return true if all keys are found
289 bool mget(std::vector<std::string> &keys)
291 std::vector<const char *> real_keys;
292 std::vector<size_t> key_len;
294 * Construct an array which will contain the length
295 * of each of the strings in the input vector. Also, to
296 * interface with the memcached C API, we need to convert
297 * the vector of std::string's to a vector of char *.
299 real_keys.reserve(keys.size());
300 key_len.reserve(keys.size());
302 std::vector<std::string>::iterator it= keys.begin();
304 while (it != keys.end())
306 real_keys.push_back(const_cast<char *>((*it).c_str()));
307 key_len.push_back((*it).length());
312 * If the std::vector of keys is empty then we cannot
313 * call memcached_mget as we will get undefined behavior.
315 if (! real_keys.empty())
317 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
319 return (rc == MEMCACHED_SUCCESS);
326 * Writes an object to the server. If the object already exists, it will
327 * overwrite the existing object. This method always returns true
328 * when using non-blocking mode unless a network error occurs.
330 * @param[in] key key of object to write to server
331 * @param[in] value value of object to write to server
332 * @param[in] expiration time to keep the object stored in the server for
333 * @param[in] flags flags to store with the object
334 * @return true on succcess; false otherwise
336 bool set(const std::string &key,
337 const std::vector<char> &value,
341 if (key.empty() || value.empty())
345 memcached_return rc= memcached_set(&memc,
346 key.c_str(), key.length(),
347 &value[0], value.size(),
349 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
353 * Writes an object to a server specified by the master_key parameter.
354 * If the object already exists, it will overwrite the existing object.
356 * @param[in] master_key key that specifies server to write to
357 * @param[in] key key of object to write to server
358 * @param[in] value value of object to write to server
359 * @param[in] expiration time to keep the object stored in the server for
360 * @param[in] flags flags to store with the object
361 * @return true on succcess; false otherwise
363 bool setByKey(const std::string &master_key,
364 const std::string &key,
365 const std::vector<char> &value,
369 if (master_key.empty() ||
375 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
377 key.c_str(), key.length(),
378 &value[0], value.size(),
381 return (rc == MEMCACHED_SUCCESS);
385 * Writes a list of objects to the server. Objects are specified by
386 * 2 vectors - 1 vector of keys and 1 vector of values.
388 * @param[in] keys vector of keys of objects to write to server
389 * @param[in] values vector of values of objects to write to server
390 * @param[in] expiration time to keep the objects stored in server for
391 * @param[in] flags flags to store with the objects
392 * @return true on success; false otherwise
394 bool setAll(std::vector<std::string> &keys,
395 std::vector< std::vector<char> *> &values,
399 if (keys.size() != values.size())
404 std::vector<std::string>::iterator key_it= keys.begin();
405 std::vector< std::vector<char> *>::iterator val_it= values.begin();
406 while (key_it != keys.end())
408 retval= set((*key_it), *(*val_it), expiration, flags);
420 * Writes a list of objects to the server. Objects are specified by
421 * a map of keys to values.
423 * @param[in] key_value_map map of keys and values to store in server
424 * @param[in] expiration time to keep the objects stored in server for
425 * @param[in] flags flags to store with the objects
426 * @return true on success; false otherwise
428 bool setAll(std::map<const std::string, std::vector<char> > &key_value_map,
432 if (key_value_map.empty())
437 std::map<const std::string, std::vector<char> >::iterator it=
438 key_value_map.begin();
439 while (it != key_value_map.end())
441 retval= set(it->first, it->second, expiration, flags);
452 * Increment the value of the object associated with the specified
453 * key by the offset given. The resulting value is saved in the value
456 * @param[in] key key of object in server whose value to increment
457 * @param[in] offset amount to increment object's value by
458 * @param[out] value store the result of the increment here
459 * @return true on success; false otherwise
461 bool increment(const std::string &key, uint32_t offset, uint64_t *value)
467 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
469 return (rc == MEMCACHED_SUCCESS);
473 * Decrement the value of the object associated with the specified
474 * key by the offset given. The resulting value is saved in the value
477 * @param[in] key key of object in server whose value to decrement
478 * @param[in] offset amount to increment object's value by
479 * @param[out] value store the result of the decrement here
480 * @return true on success; false otherwise
482 bool decrement(const std::string &key, uint32_t offset, uint64_t *value)
488 memcached_return rc= memcached_decrement(&memc, key.c_str(),
491 return (rc == MEMCACHED_SUCCESS);
496 * Add an object with the specified key and value to the server. This
497 * function returns false if the object already exists on the server.
499 * @param[in] key key of object to add
500 * @param[in] value of object to add
501 * @return true on success; false otherwise
503 bool add(const std::string &key, const std::vector<char> &value)
505 if (key.empty() || value.empty())
509 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
510 &value[0], value.size(), 0, 0);
511 return (rc == MEMCACHED_SUCCESS);
515 * Add an object with the specified key and value to the server. This
516 * function returns false if the object already exists on the server. The
517 * server to add the object to is specified by the master_key parameter.
519 * @param[in[ master_key key of server to add object to
520 * @param[in] key key of object to add
521 * @param[in] value of object to add
522 * @return true on success; false otherwise
524 bool addByKey(const std::string &master_key,
525 const std::string &key,
526 const std::vector<char> &value)
528 if (master_key.empty() ||
534 memcached_return rc= memcached_add_by_key(&memc,
542 return (rc == MEMCACHED_SUCCESS);
546 * Replaces an object on the server. This method only succeeds
547 * if the object is already present on the server.
549 * @param[in] key key of object to replace
550 * @param[in[ value value to replace object with
551 * @return true on success; false otherwise
553 bool replace(const std::string &key, const std::vector<char> &value)
560 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
561 &value[0], value.size(),
563 return (rc == MEMCACHED_SUCCESS);
567 * Replaces an object on the server. This method only succeeds
568 * if the object is already present on the server. The server
569 * to replace the object on is specified by the master_key param.
571 * @param[in] master_key key of server to replace object on
572 * @param[in] key key of object to replace
573 * @param[in[ value value to replace object with
574 * @return true on success; false otherwise
576 bool replaceByKey(const std::string &master_key,
577 const std::string &key,
578 const std::vector<char> &value)
580 if (master_key.empty() ||
586 memcached_return rc= memcached_replace_by_key(&memc,
594 return (rc == MEMCACHED_SUCCESS);
598 * Places a segment of data before the last piece of data stored.
600 * @param[in] key key of object whose value we will prepend data to
601 * @param[in] value data to prepend to object's value
602 * @return true on success; false otherwise
604 bool prepend(const std::string &key, const std::vector<char> &value)
606 if (key.empty() || value.empty())
610 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
611 &value[0], value.size(), 0, 0);
612 return (rc == MEMCACHED_SUCCESS);
616 * Places a segment of data before the last piece of data stored. The
617 * server on which the object where we will be prepending data is stored
618 * on is specified by the master_key parameter.
620 * @param[in] master_key key of server where object is stored
621 * @param[in] key key of object whose value we will prepend data to
622 * @param[in] value data to prepend to object's value
623 * @return true on success; false otherwise
625 bool prependByKey(const std::string &master_key,
626 const std::string &key,
627 const std::vector<char> &value)
629 if (master_key.empty() ||
635 memcached_return rc= memcached_prepend_by_key(&memc,
644 return (rc == MEMCACHED_SUCCESS);
648 * Places a segment of data at the end of the last piece of data stored.
650 * @param[in] key key of object whose value we will append data to
651 * @param[in] value data to append to object's value
652 * @return true on success; false otherwise
654 bool append(const std::string &key, const std::vector<char> &value)
656 if (key.empty() || value.empty())
660 memcached_return rc= memcached_append(&memc,
666 return (rc == MEMCACHED_SUCCESS);
670 * Places a segment of data at the end of the last piece of data stored. The
671 * server on which the object where we will be appending data is stored
672 * on is specified by the master_key parameter.
674 * @param[in] master_key key of server where object is stored
675 * @param[in] key key of object whose value we will append data to
676 * @param[in] value data to append to object's value
677 * @return true on success; false otherwise
679 bool appendByKey(const std::string &master_key,
680 const std::string &key,
681 const std::vector<char> &value)
683 if (master_key.empty() ||
689 memcached_return rc= memcached_append_by_key(&memc,
697 return (rc == MEMCACHED_SUCCESS);
701 * Overwrite data in the server as long as the cas_arg value
702 * is still the same in the server.
704 * @param[in] key key of object in server
705 * @param[in] value value to store for object in server
706 * @param[in] cas_arg "cas" value
708 bool cas(const std::string &key,
709 const std::vector<char> &value,
712 if (key.empty() || value.empty())
716 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
717 &value[0], value.size(),
719 return (rc == MEMCACHED_SUCCESS);
723 * Overwrite data in the server as long as the cas_arg value
724 * is still the same in the server. The server to use is
725 * specified by the master_key parameter.
727 * @param[in] master_key specifies server to operate on
728 * @param[in] key key of object in server
729 * @param[in] value value to store for object in server
730 * @param[in] cas_arg "cas" value
732 bool casByKey(const std::string &master_key,
733 const std::string &key,
734 const std::vector<char> &value,
737 if (master_key.empty() ||
743 memcached_return rc= memcached_cas_by_key(&memc,
751 return (rc == MEMCACHED_SUCCESS);
755 * Delete an object from the server specified by the key given.
757 * @param[in] key key of object to delete
758 * @return true on success; false otherwise
760 bool remove(const std::string &key)
766 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
767 return (rc == MEMCACHED_SUCCESS);
771 * Delete an object from the server specified by the key given.
773 * @param[in] key key of object to delete
774 * @param[in] expiration time to delete the object after
775 * @return true on success; false otherwise
777 bool remove(const std::string &key,
784 memcached_return rc= memcached_delete(&memc,
788 return (rc == MEMCACHED_SUCCESS);
792 * Delete an object from the server specified by the key given.
794 * @param[in] master_key specifies server to remove object from
795 * @param[in] key key of object to delete
796 * @return true on success; false otherwise
798 bool removeByKey(const std::string &master_key,
799 const std::string &key)
801 if (master_key.empty() || key.empty())
805 memcached_return rc= memcached_delete_by_key(&memc,
811 return (rc == MEMCACHED_SUCCESS);
815 * Delete an object from the server specified by the key given.
817 * @param[in] master_key specifies server to remove object from
818 * @param[in] key key of object to delete
819 * @param[in] expiration time to delete the object after
820 * @return true on success; false otherwise
822 bool removeByKey(const std::string &master_key,
823 const std::string &key,
826 if (master_key.empty() || key.empty())
830 memcached_return rc= memcached_delete_by_key(&memc,
836 return (rc == MEMCACHED_SUCCESS);
840 * Wipe the contents of memcached servers.
842 * @param[in] expiration time to wait until wiping contents of
844 * @return true on success; false otherwise
846 bool flush(time_t expiration)
848 memcached_return rc= memcached_flush(&memc, expiration);
849 return (rc == MEMCACHED_SUCCESS);
853 * Callback function for result sets. It passes the result
854 * sets to the list of functions provided.
856 * @param[in] callback list of callback functions
857 * @param[in] context pointer to memory reference that is
858 * supplied to the calling function
859 * @param[in] num_of_callbacks number of callback functions
860 * @return true on success; false otherwise
862 bool fetchExecute(memcached_execute_function *callback,
864 unsigned int num_of_callbacks)
866 memcached_return rc= memcached_fetch_execute(&memc,
870 return (rc == MEMCACHED_SUCCESS);
874 * Get the library version string.
875 * @return std::string containing a copy of the library version string.
877 const std::string libVersion() const
879 const char *ver= memcached_lib_version();
880 const std::string version(ver);
886 std::string servers_list;
888 memcached_server_st *servers;
889 memcached_result_st result;
894 #endif /* LIBMEMCACHEDPP_H */