2 * Summary: C++ interface for memcached server
4 * Copy: See Copyright for the status of this software.
6 * Authors: Padraig O'Sullivan, Patrick Galbraith
9 #ifndef LIBMEMCACHEDPP_H
10 #define LIBMEMCACHEDPP_H
12 #include <libmemcached/memcached.h>
26 memcached_create(&memc);
29 Memcached(memcached_st *clone)
34 memcached_clone(&memc, clone);
39 memcached_free(&memc);
42 bool fetch(std::string &key,
49 char ret_key[MEMCACHED_MAX_KEY];
50 char *value= memcached_fetch(&memc, ret_key, key_length,
51 value_length, flags, rc);
54 ret_val.assign(value);
61 std::string get(const std::string &key, size_t *value_length)
67 char *value= memcached_get(&memc, key.c_str(), key.length(),
68 value_length, &flags, &rc);
71 ret_val.assign(value);
76 std::string getByKey(const std::string &master_key,
77 const std::string &key,
84 char *value= memcached_get_by_key(&memc,
85 master_key.c_str(), master_key.length(),
86 key.c_str(), key.length(),
87 value_length, &flags, &rc);
90 ret_val.assign(value);
95 bool mget(std::vector<std::string> &keys)
97 std::vector<const char *> real_keys;
98 std::vector<size_t> key_len;
100 * Construct an array which will contain the length
101 * of each of the strings in the input vector. Also, to
102 * interface with the memcached C API, we need to convert
103 * the vector of std::string's to a vector of char *.
105 real_keys.reserve(keys.size());
106 key_len.reserve(keys.size());
108 std::vector<std::string>::iterator it= keys.begin();
110 while (it != keys.end())
112 real_keys.push_back(const_cast<char *>((*it).c_str()));
113 key_len.push_back((*it).length());
118 * If the std::vector of keys is empty then we cannot
119 * call memcached_mget as we will get undefined behavior.
121 if (!real_keys.empty())
123 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
125 return (rc == MEMCACHED_SUCCESS);
131 bool set(const std::string &key,
132 const std::string &value,
136 memcached_return rc= memcached_set(&memc,
137 key.c_str(), key.length(),
138 value.c_str(), value.length(),
140 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
143 bool setAll(std::vector<std::string> &keys,
144 std::vector<std::string> &values,
148 if (keys.size() != values.size())
153 std::vector<std::string>::iterator key_it= keys.begin();
154 std::vector<std::string>::iterator val_it= values.begin();
155 while (key_it != keys.end())
157 retval= set((*key_it), (*val_it), expiration, flags);
168 bool setByKey(const std::string &master_key,
169 const std::string &key,
170 const std::string &value,
174 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
176 key.c_str(), key.length(),
177 value.c_str(), value.length(),
180 return (rc == MEMCACHED_SUCCESS);
183 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
185 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
187 return (rc == MEMCACHED_SUCCESS);
190 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
192 memcached_return rc= memcached_decrement(&memc, key.c_str(),
195 return (rc == MEMCACHED_SUCCESS);
199 bool add(const std::string &key, const std::string &value)
201 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
202 value.c_str(), value.length(), 0, 0);
203 return (rc == MEMCACHED_SUCCESS);
206 bool addByKey(const std::string &master_key,
207 const std::string &key,
208 const std::string &value)
210 memcached_return rc= memcached_add_by_key(&memc,
218 return (rc == MEMCACHED_SUCCESS);
221 bool replace(const std::string &key, const std::string &value)
223 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
224 value.c_str(), value.length(),
226 return (rc == MEMCACHED_SUCCESS);
229 bool replaceByKey(const std::string &master_key,
230 const std::string &key,
231 const std::string &value)
233 memcached_return rc= memcached_replace_by_key(&memc,
241 return (rc == MEMCACHED_SUCCESS);
244 bool prepend(const std::string &key, const std::string &value)
246 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
247 value.c_str(), value.length(), 0, 0);
248 return (rc == MEMCACHED_SUCCESS);
251 bool prependByKey(const std::string &master_key,
252 const std::string &key,
253 const std::string &value)
255 memcached_return rc= memcached_prepend_by_key(&memc,
264 return (rc == MEMCACHED_SUCCESS);
267 bool append(const std::string &key, const std::string &value)
269 memcached_return rc= memcached_append(&memc,
275 return (rc == MEMCACHED_SUCCESS);
278 bool appendByKey(const std::string &master_key,
279 const std::string &key,
280 const std::string &value)
282 memcached_return rc= memcached_append_by_key(&memc,
290 return (rc == MEMCACHED_SUCCESS);
293 bool cas(const std::string &key,
294 const std::string &value,
297 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
298 value.c_str(), value.length(),
300 return (rc == MEMCACHED_SUCCESS);
303 bool casByKey(const std::string &master_key,
304 const std::string &key,
305 const std::string &value,
308 memcached_return rc= memcached_cas_by_key(&memc,
316 return (rc == MEMCACHED_SUCCESS);
319 // using 'remove' vs. 'delete' since 'delete' is a keyword
320 bool remove(const std::string &key)
322 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
323 return (rc == MEMCACHED_SUCCESS);
326 bool removeByKey(const std::string &master_key,
327 const std::string &key)
329 memcached_return rc= memcached_delete_by_key(&memc,
335 return (rc == MEMCACHED_SUCCESS);
338 bool flush(time_t expiration)
340 memcached_return rc= memcached_flush(&memc, expiration);
341 return (rc == MEMCACHED_SUCCESS);
344 bool fetchExecute(memcached_execute_function *callback,
346 unsigned int num_of_callbacks)
348 memcached_return rc= memcached_fetch_execute(&memc,
352 return (rc == MEMCACHED_SUCCESS);
355 const std::string libVersion() const
357 const char *ver= memcached_lib_version();
358 const std::string version(ver);
364 memcached_result_st result;
367 #endif /* LIBMEMCACHEDPP_H */