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.
97 * @param[in] key key of object to fetch
98 * @param[out] ret_val store returned object in this vector
99 * @return true on success; false on failure
101 bool fetch(std::string &key,
102 std::vector<char> &ret_val)
104 char ret_key[MEMCACHED_MAX_KEY];
105 size_t value_length= 0;
106 size_t key_length= 0;
109 char *value= memcached_fetch(&memc, ret_key, &key_length,
110 &value_length, &flags, &rc);
111 if (value && ret_val.empty())
113 ret_val.reserve(value_length);
114 ret_val.assign(value, value + value_length);
121 std::vector<char> &get(const std::string &key,
122 std::vector<char> &ret_val)
126 size_t value_length= 0;
132 char *value= memcached_get(&memc, key.c_str(), key.length(),
133 &value_length, &flags, &rc);
134 if (value != NULL && ret_val.empty())
136 ret_val.reserve(value_length);
137 ret_val.assign(value, value + value_length);
142 std::vector<char> &getByKey(const std::string &master_key,
143 const std::string &key,
144 std::vector<char> &ret_val)
148 size_t value_length= 0;
150 if (master_key.empty() || key.empty())
154 char *value= memcached_get_by_key(&memc,
155 master_key.c_str(), master_key.length(),
156 key.c_str(), key.length(),
157 &value_length, &flags, &rc);
160 ret_val.reserve(value_length);
161 ret_val.assign(value, value + value_length);
166 bool mget(std::vector<std::string> &keys)
168 std::vector<const char *> real_keys;
169 std::vector<size_t> key_len;
171 * Construct an array which will contain the length
172 * of each of the strings in the input vector. Also, to
173 * interface with the memcached C API, we need to convert
174 * the vector of std::string's to a vector of char *.
176 real_keys.reserve(keys.size());
177 key_len.reserve(keys.size());
179 std::vector<std::string>::iterator it= keys.begin();
181 while (it != keys.end())
183 real_keys.push_back(const_cast<char *>((*it).c_str()));
184 key_len.push_back((*it).length());
189 * If the std::vector of keys is empty then we cannot
190 * call memcached_mget as we will get undefined behavior.
192 if (! real_keys.empty())
194 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
196 return (rc == MEMCACHED_SUCCESS);
202 bool set(const std::string &key,
203 const std::vector<char> &value,
207 if (key.empty() || value.empty())
211 memcached_return rc= memcached_set(&memc,
212 key.c_str(), key.length(),
213 &value[0], value.size(),
215 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
218 bool setAll(std::vector<std::string> &keys,
219 std::vector< std::vector<char> *> &values,
223 if (keys.size() != values.size())
228 std::vector<std::string>::iterator key_it= keys.begin();
229 std::vector< std::vector<char> *>::iterator val_it= values.begin();
230 while (key_it != keys.end())
232 retval= set((*key_it), *(*val_it), expiration, flags);
243 bool setAll(std::map<const std::string, std::vector<char> > &key_value_map,
247 if (key_value_map.empty())
252 std::map<const std::string, std::vector<char> >::iterator it=
253 key_value_map.begin();
254 while (it != key_value_map.end())
256 retval= set(it->first, it->second, expiration, flags);
266 bool setByKey(const std::string &master_key,
267 const std::string &key,
268 const std::vector<char> &value,
272 if (master_key.empty() ||
278 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
280 key.c_str(), key.length(),
281 &value[0], value.size(),
284 return (rc == MEMCACHED_SUCCESS);
287 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
293 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
295 return (rc == MEMCACHED_SUCCESS);
298 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
304 memcached_return rc= memcached_decrement(&memc, key.c_str(),
307 return (rc == MEMCACHED_SUCCESS);
311 bool add(const std::string &key, const std::vector<char> &value)
313 if (key.empty() || value.empty())
317 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
318 &value[0], value.size(), 0, 0);
319 return (rc == MEMCACHED_SUCCESS);
322 bool addByKey(const std::string &master_key,
323 const std::string &key,
324 const std::vector<char> &value)
326 if (master_key.empty() ||
332 memcached_return rc= memcached_add_by_key(&memc,
340 return (rc == MEMCACHED_SUCCESS);
343 bool replace(const std::string &key, const std::vector<char> &value)
350 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
351 &value[0], value.size(),
353 return (rc == MEMCACHED_SUCCESS);
356 bool replaceByKey(const std::string &master_key,
357 const std::string &key,
358 const std::vector<char> &value)
360 if (master_key.empty() ||
366 memcached_return rc= memcached_replace_by_key(&memc,
374 return (rc == MEMCACHED_SUCCESS);
377 bool prepend(const std::string &key, const std::vector<char> &value)
379 if (key.empty() || value.empty())
383 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
384 &value[0], value.size(), 0, 0);
385 return (rc == MEMCACHED_SUCCESS);
388 bool prependByKey(const std::string &master_key,
389 const std::string &key,
390 const std::vector<char> &value)
392 if (master_key.empty() ||
398 memcached_return rc= memcached_prepend_by_key(&memc,
407 return (rc == MEMCACHED_SUCCESS);
410 bool append(const std::string &key, const std::vector<char> &value)
412 if (key.empty() || value.empty())
416 memcached_return rc= memcached_append(&memc,
422 return (rc == MEMCACHED_SUCCESS);
425 bool appendByKey(const std::string &master_key,
426 const std::string &key,
427 const std::vector<char> &value)
429 if (master_key.empty() ||
435 memcached_return rc= memcached_append_by_key(&memc,
443 return (rc == MEMCACHED_SUCCESS);
446 bool cas(const std::string &key,
447 const std::vector<char> &value,
450 if (key.empty() || value.empty())
454 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
455 &value[0], value.size(),
457 return (rc == MEMCACHED_SUCCESS);
460 bool casByKey(const std::string &master_key,
461 const std::string &key,
462 const std::vector<char> &value,
465 if (master_key.empty() ||
471 memcached_return rc= memcached_cas_by_key(&memc,
479 return (rc == MEMCACHED_SUCCESS);
482 bool remove(const std::string &key)
488 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
489 return (rc == MEMCACHED_SUCCESS);
492 bool removeByKey(const std::string &master_key,
493 const std::string &key)
495 if (master_key.empty() || key.empty())
499 memcached_return rc= memcached_delete_by_key(&memc,
505 return (rc == MEMCACHED_SUCCESS);
508 bool flush(time_t expiration)
510 memcached_return rc= memcached_flush(&memc, expiration);
511 return (rc == MEMCACHED_SUCCESS);
514 bool fetchExecute(memcached_execute_function *callback,
516 unsigned int num_of_callbacks)
518 memcached_return rc= memcached_fetch_execute(&memc,
522 return (rc == MEMCACHED_SUCCESS);
526 * Get the library version string.
527 * @return std::string containing a copy of the library version string.
529 const std::string libVersion() const
531 const char *ver= memcached_lib_version();
532 const std::string version(ver);
539 memcached_result_st result;
544 #endif /* LIBMEMCACHEDPP_H */