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);
94 bool fetch(std::string &key,
95 std::vector<char> &ret_val,
99 char ret_key[MEMCACHED_MAX_KEY];
100 size_t value_length= 0;
101 size_t key_length= 0;
102 char *value= memcached_fetch(&memc, ret_key, &key_length,
103 &value_length, flags, rc);
104 if (value && ret_val.empty())
106 ret_val.reserve(value_length);
107 ret_val.assign(value, value + value_length);
114 std::vector<char> &get(const std::string &key,
115 std::vector<char> &ret_val)
119 size_t value_length= 0;
125 char *value= memcached_get(&memc, key.c_str(), key.length(),
126 &value_length, &flags, &rc);
127 if (value != NULL && ret_val.empty())
129 ret_val.reserve(value_length);
130 ret_val.assign(value, value + value_length);
135 std::vector<char> &getByKey(const std::string &master_key,
136 const std::string &key,
137 std::vector<char> &ret_val)
141 size_t value_length= 0;
143 if (master_key.empty() || key.empty())
147 char *value= memcached_get_by_key(&memc,
148 master_key.c_str(), master_key.length(),
149 key.c_str(), key.length(),
150 &value_length, &flags, &rc);
153 ret_val.reserve(value_length);
154 ret_val.assign(value, value + value_length);
159 bool mget(std::vector<std::string> &keys)
161 std::vector<const char *> real_keys;
162 std::vector<size_t> key_len;
164 * Construct an array which will contain the length
165 * of each of the strings in the input vector. Also, to
166 * interface with the memcached C API, we need to convert
167 * the vector of std::string's to a vector of char *.
169 real_keys.reserve(keys.size());
170 key_len.reserve(keys.size());
172 std::vector<std::string>::iterator it= keys.begin();
174 while (it != keys.end())
176 real_keys.push_back(const_cast<char *>((*it).c_str()));
177 key_len.push_back((*it).length());
182 * If the std::vector of keys is empty then we cannot
183 * call memcached_mget as we will get undefined behavior.
185 if (! real_keys.empty())
187 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
189 return (rc == MEMCACHED_SUCCESS);
195 bool set(const std::string &key,
196 const std::vector<char> &value,
200 if (key.empty() || value.empty())
204 memcached_return rc= memcached_set(&memc,
205 key.c_str(), key.length(),
206 &value[0], value.size(),
208 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
211 bool setAll(std::vector<std::string> &keys,
212 std::vector< std::vector<char> *> &values,
216 if (keys.size() != values.size())
221 std::vector<std::string>::iterator key_it= keys.begin();
222 std::vector< std::vector<char> *>::iterator val_it= values.begin();
223 while (key_it != keys.end())
225 retval= set((*key_it), *(*val_it), expiration, flags);
236 bool setAll(std::map<const std::string, std::vector<char> > key_value_map,
240 if (key_value_map.empty())
245 std::map<const std::string, std::vector<char> >::iterator it=
246 key_value_map.begin();
247 while (it != key_value_map.end())
249 retval= set(it->first, it->second, expiration, flags);
259 bool setByKey(const std::string &master_key,
260 const std::string &key,
261 const std::vector<char> &value,
265 if (master_key.empty() ||
271 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
273 key.c_str(), key.length(),
274 &value[0], value.size(),
277 return (rc == MEMCACHED_SUCCESS);
280 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
286 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
288 return (rc == MEMCACHED_SUCCESS);
291 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
297 memcached_return rc= memcached_decrement(&memc, key.c_str(),
300 return (rc == MEMCACHED_SUCCESS);
304 bool add(const std::string &key, const std::vector<char> &value)
306 if (key.empty() || value.empty())
310 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
311 &value[0], value.size(), 0, 0);
312 return (rc == MEMCACHED_SUCCESS);
315 bool addByKey(const std::string &master_key,
316 const std::string &key,
317 const std::vector<char> &value)
319 if (master_key.empty() ||
325 memcached_return rc= memcached_add_by_key(&memc,
333 return (rc == MEMCACHED_SUCCESS);
336 bool replace(const std::string &key, const std::vector<char> &value)
343 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
344 &value[0], value.size(),
346 return (rc == MEMCACHED_SUCCESS);
349 bool replaceByKey(const std::string &master_key,
350 const std::string &key,
351 const std::vector<char> &value)
353 if (master_key.empty() ||
359 memcached_return rc= memcached_replace_by_key(&memc,
367 return (rc == MEMCACHED_SUCCESS);
370 bool prepend(const std::string &key, const std::vector<char> &value)
372 if (key.empty() || value.empty())
376 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
377 &value[0], value.size(), 0, 0);
378 return (rc == MEMCACHED_SUCCESS);
381 bool prependByKey(const std::string &master_key,
382 const std::string &key,
383 const std::vector<char> &value)
385 if (master_key.empty() ||
391 memcached_return rc= memcached_prepend_by_key(&memc,
400 return (rc == MEMCACHED_SUCCESS);
403 bool append(const std::string &key, const std::vector<char> &value)
405 if (key.empty() || value.empty())
409 memcached_return rc= memcached_append(&memc,
415 return (rc == MEMCACHED_SUCCESS);
418 bool appendByKey(const std::string &master_key,
419 const std::string &key,
420 const std::vector<char> &value)
422 if (master_key.empty() ||
428 memcached_return rc= memcached_append_by_key(&memc,
436 return (rc == MEMCACHED_SUCCESS);
439 bool cas(const std::string &key,
440 const std::vector<char> &value,
443 if (key.empty() || value.empty())
447 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
448 &value[0], value.size(),
450 return (rc == MEMCACHED_SUCCESS);
453 bool casByKey(const std::string &master_key,
454 const std::string &key,
455 const std::vector<char> &value,
458 if (master_key.empty() ||
464 memcached_return rc= memcached_cas_by_key(&memc,
472 return (rc == MEMCACHED_SUCCESS);
475 bool remove(const std::string &key)
481 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
482 return (rc == MEMCACHED_SUCCESS);
485 bool removeByKey(const std::string &master_key,
486 const std::string &key)
488 if (master_key.empty() || key.empty())
492 memcached_return rc= memcached_delete_by_key(&memc,
498 return (rc == MEMCACHED_SUCCESS);
501 bool flush(time_t expiration)
503 memcached_return rc= memcached_flush(&memc, expiration);
504 return (rc == MEMCACHED_SUCCESS);
507 bool fetchExecute(memcached_execute_function *callback,
509 unsigned int num_of_callbacks)
511 memcached_return rc= memcached_fetch_execute(&memc,
515 return (rc == MEMCACHED_SUCCESS);
519 * Get the library version string.
520 * @return std::string containing a copy of the library version string.
522 const std::string libVersion() const
524 const char *ver= memcached_lib_version();
525 const std::string version(ver);
532 memcached_result_st result;
537 #endif /* LIBMEMCACHEDPP_H */