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);
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;
117 char *value= memcached_get(&memc, key.c_str(), key.length(),
118 &value_length, &flags, &rc);
121 ret_val.reserve(value_length);
122 memcpy(&ret_val[0], value, value_length);
127 std::vector<char> &getByKey(const std::string &master_key,
128 const std::string &key,
129 std::vector<char> &ret_val)
133 size_t value_length= 0;
135 char *value= memcached_get_by_key(&memc,
136 master_key.c_str(), master_key.length(),
137 key.c_str(), key.length(),
138 &value_length, &flags, &rc);
141 ret_val.reserve(value_length);
142 memcpy(&*ret_val.begin(), value, value_length);
147 bool mget(std::vector<std::string> &keys)
149 std::vector<const char *> real_keys;
150 std::vector<size_t> key_len;
152 * Construct an array which will contain the length
153 * of each of the strings in the input vector. Also, to
154 * interface with the memcached C API, we need to convert
155 * the vector of std::string's to a vector of char *.
157 real_keys.reserve(keys.size());
158 key_len.reserve(keys.size());
160 std::vector<std::string>::iterator it= keys.begin();
162 while (it != keys.end())
164 real_keys.push_back(const_cast<char *>((*it).c_str()));
165 key_len.push_back((*it).length());
170 * If the std::vector of keys is empty then we cannot
171 * call memcached_mget as we will get undefined behavior.
173 if (!real_keys.empty())
175 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
177 return (rc == MEMCACHED_SUCCESS);
183 bool set(const std::string &key,
184 const std::vector<char> &value,
188 memcached_return rc= memcached_set(&memc,
189 key.c_str(), key.length(),
190 &value[0], value.size(),
192 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
195 bool setAll(std::vector<std::string> &keys,
196 std::vector< std::vector<char> > &values,
200 if (keys.size() != values.size())
205 std::vector<std::string>::iterator key_it= keys.begin();
206 std::vector< std::vector<char> >::iterator val_it= values.begin();
207 while (key_it != keys.end())
209 retval= set((*key_it), (*val_it), expiration, flags);
220 bool setByKey(const std::string &master_key,
221 const std::string &key,
222 const std::vector<char> &value,
226 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
228 key.c_str(), key.length(),
229 &value[0], value.size(),
232 return (rc == MEMCACHED_SUCCESS);
235 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
237 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
239 return (rc == MEMCACHED_SUCCESS);
242 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
244 memcached_return rc= memcached_decrement(&memc, key.c_str(),
247 return (rc == MEMCACHED_SUCCESS);
251 bool add(const std::string &key, const std::vector<char> &value)
253 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
254 &value[0], value.size(), 0, 0);
255 return (rc == MEMCACHED_SUCCESS);
258 bool addByKey(const std::string &master_key,
259 const std::string &key,
260 const std::vector<char> &value)
262 memcached_return rc= memcached_add_by_key(&memc,
270 return (rc == MEMCACHED_SUCCESS);
273 bool replace(const std::string &key, const std::vector<char> &value)
275 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
276 &value[0], value.size(),
278 return (rc == MEMCACHED_SUCCESS);
281 bool replaceByKey(const std::string &master_key,
282 const std::string &key,
283 const std::vector<char> &value)
285 memcached_return rc= memcached_replace_by_key(&memc,
293 return (rc == MEMCACHED_SUCCESS);
296 bool prepend(const std::string &key, const std::vector<char> &value)
298 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
299 &value[0], value.size(), 0, 0);
300 return (rc == MEMCACHED_SUCCESS);
303 bool prependByKey(const std::string &master_key,
304 const std::string &key,
305 const std::vector<char> &value)
307 memcached_return rc= memcached_prepend_by_key(&memc,
316 return (rc == MEMCACHED_SUCCESS);
319 bool append(const std::string &key, const std::vector<char> &value)
321 memcached_return rc= memcached_append(&memc,
327 return (rc == MEMCACHED_SUCCESS);
330 bool appendByKey(const std::string &master_key,
331 const std::string &key,
332 const std::vector<char> &value)
334 memcached_return rc= memcached_append_by_key(&memc,
342 return (rc == MEMCACHED_SUCCESS);
345 bool cas(const std::string &key,
346 const std::vector<char> &value,
349 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
350 &value[0], value.size(),
352 return (rc == MEMCACHED_SUCCESS);
355 bool casByKey(const std::string &master_key,
356 const std::string &key,
357 const std::vector<char> &value,
360 memcached_return rc= memcached_cas_by_key(&memc,
368 return (rc == MEMCACHED_SUCCESS);
371 bool remove(const std::string &key)
373 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
374 return (rc == MEMCACHED_SUCCESS);
377 bool removeByKey(const std::string &master_key,
378 const std::string &key)
380 memcached_return rc= memcached_delete_by_key(&memc,
386 return (rc == MEMCACHED_SUCCESS);
389 bool flush(time_t expiration)
391 memcached_return rc= memcached_flush(&memc, expiration);
392 return (rc == MEMCACHED_SUCCESS);
395 bool fetchExecute(memcached_execute_function *callback,
397 unsigned int num_of_callbacks)
399 memcached_return rc= memcached_fetch_execute(&memc,
403 return (rc == MEMCACHED_SUCCESS);
407 * Get the library version string.
408 * @return std::string containing a copy of the library version string.
410 const std::string libVersion() const
412 const char *ver= memcached_lib_version();
413 const std::string version(ver);
420 memcached_result_st result;
423 #endif /* LIBMEMCACHEDPP_H */