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>
26 * This is the core memcached library (if later, other objects
27 * are needed, they will be created from this class).
38 memcached_create(&memc);
41 Memcached(memcached_st *clone)
46 memcached_clone(&memc, clone);
49 Memcached(const Memcached &rhs)
54 memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
59 memcached_free(&memc);
63 * Get the internal memcached_st *
65 memcached_st &getImpl()
71 * Get the internal memcached_st *
73 const memcached_st &getImpl() const
79 * Return an error string for the given return structure.
81 * @param[in] rc a memcached_return structure
82 * @return error string corresponding to given return code in the library.
84 const std::string getError(memcached_return rc) const
86 /* first parameter to strerror is unused */
87 return memcached_strerror(NULL, rc);
90 bool fetch(std::string &key,
91 std::vector<char> &ret_val,
95 char ret_key[MEMCACHED_MAX_KEY];
96 size_t value_length= 0;
98 char *value= memcached_fetch(&memc, ret_key, &key_length,
99 &value_length, flags, rc);
100 if (value && ret_val.empty())
102 ret_val.reserve(value_length);
103 memcpy(&*ret_val.begin(), value, value_length);
110 std::vector<char> &get(const std::string &key,
111 std::vector<char> &ret_val)
115 size_t value_length= 0;
121 char *value= memcached_get(&memc, key.c_str(), key.length(),
122 &value_length, &flags, &rc);
123 if (value != NULL && ret_val.empty())
125 ret_val.reserve(value_length);
126 memcpy(&ret_val[0], value, value_length);
131 std::vector<char> &getByKey(const std::string &master_key,
132 const std::string &key,
133 std::vector<char> &ret_val)
137 size_t value_length= 0;
139 if (master_key.empty() || key.empty())
143 char *value= memcached_get_by_key(&memc,
144 master_key.c_str(), master_key.length(),
145 key.c_str(), key.length(),
146 &value_length, &flags, &rc);
149 ret_val.reserve(value_length);
150 memcpy(&*ret_val.begin(), value, value_length);
155 bool mget(std::vector<std::string> &keys)
157 std::vector<const char *> real_keys;
158 std::vector<size_t> key_len;
160 * Construct an array which will contain the length
161 * of each of the strings in the input vector. Also, to
162 * interface with the memcached C API, we need to convert
163 * the vector of std::string's to a vector of char *.
165 real_keys.reserve(keys.size());
166 key_len.reserve(keys.size());
168 std::vector<std::string>::iterator it= keys.begin();
170 while (it != keys.end())
172 real_keys.push_back(const_cast<char *>((*it).c_str()));
173 key_len.push_back((*it).length());
178 * If the std::vector of keys is empty then we cannot
179 * call memcached_mget as we will get undefined behavior.
181 if (! real_keys.empty())
183 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
185 return (rc == MEMCACHED_SUCCESS);
191 bool set(const std::string &key,
192 const std::vector<char> &value,
196 if (key.empty() || value.empty())
200 memcached_return rc= memcached_set(&memc,
201 key.c_str(), key.length(),
202 &value[0], value.size(),
204 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
207 bool setAll(std::vector<std::string> &keys,
208 std::vector< std::vector<char> > &values,
212 if (keys.size() != values.size())
217 std::vector<std::string>::iterator key_it= keys.begin();
218 std::vector< std::vector<char> >::iterator val_it= values.begin();
219 while (key_it != keys.end())
221 retval= set((*key_it), (*val_it), expiration, flags);
232 bool setByKey(const std::string &master_key,
233 const std::string &key,
234 const std::vector<char> &value,
238 if (master_key.empty() ||
244 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
246 key.c_str(), key.length(),
247 &value[0], value.size(),
250 return (rc == MEMCACHED_SUCCESS);
253 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
259 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
261 return (rc == MEMCACHED_SUCCESS);
264 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
270 memcached_return rc= memcached_decrement(&memc, key.c_str(),
273 return (rc == MEMCACHED_SUCCESS);
277 bool add(const std::string &key, const std::vector<char> &value)
279 if (key.empty() || value.empty())
283 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
284 &value[0], value.size(), 0, 0);
285 return (rc == MEMCACHED_SUCCESS);
288 bool addByKey(const std::string &master_key,
289 const std::string &key,
290 const std::vector<char> &value)
292 if (master_key.empty() ||
298 memcached_return rc= memcached_add_by_key(&memc,
306 return (rc == MEMCACHED_SUCCESS);
309 bool replace(const std::string &key, const std::vector<char> &value)
316 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
317 &value[0], value.size(),
319 return (rc == MEMCACHED_SUCCESS);
322 bool replaceByKey(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_replace_by_key(&memc,
340 return (rc == MEMCACHED_SUCCESS);
343 bool prepend(const std::string &key, const std::vector<char> &value)
345 if (key.empty() || value.empty())
349 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
350 &value[0], value.size(), 0, 0);
351 return (rc == MEMCACHED_SUCCESS);
354 bool prependByKey(const std::string &master_key,
355 const std::string &key,
356 const std::vector<char> &value)
358 if (master_key.empty() ||
364 memcached_return rc= memcached_prepend_by_key(&memc,
373 return (rc == MEMCACHED_SUCCESS);
376 bool append(const std::string &key, const std::vector<char> &value)
378 if (key.empty() || value.empty())
382 memcached_return rc= memcached_append(&memc,
388 return (rc == MEMCACHED_SUCCESS);
391 bool appendByKey(const std::string &master_key,
392 const std::string &key,
393 const std::vector<char> &value)
395 if (master_key.empty() ||
401 memcached_return rc= memcached_append_by_key(&memc,
409 return (rc == MEMCACHED_SUCCESS);
412 bool cas(const std::string &key,
413 const std::vector<char> &value,
416 if (key.empty() || value.empty())
420 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
421 &value[0], value.size(),
423 return (rc == MEMCACHED_SUCCESS);
426 bool casByKey(const std::string &master_key,
427 const std::string &key,
428 const std::vector<char> &value,
431 if (master_key.empty() ||
437 memcached_return rc= memcached_cas_by_key(&memc,
445 return (rc == MEMCACHED_SUCCESS);
448 bool remove(const std::string &key)
454 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
455 return (rc == MEMCACHED_SUCCESS);
458 bool removeByKey(const std::string &master_key,
459 const std::string &key)
461 if (master_key.empty() || key.empty())
465 memcached_return rc= memcached_delete_by_key(&memc,
471 return (rc == MEMCACHED_SUCCESS);
474 bool flush(time_t expiration)
476 memcached_return rc= memcached_flush(&memc, expiration);
477 return (rc == MEMCACHED_SUCCESS);
480 bool fetchExecute(memcached_execute_function *callback,
482 unsigned int num_of_callbacks)
484 memcached_return rc= memcached_fetch_execute(&memc,
488 return (rc == MEMCACHED_SUCCESS);
492 * Get the library version string.
493 * @return std::string containing a copy of the library version string.
495 const std::string libVersion() const
497 const char *ver= memcached_lib_version();
498 const std::string version(ver);
505 memcached_result_st result;
508 #endif /* LIBMEMCACHEDPP_H */