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>
28 memcached_create(&memc);
31 Memcached(memcached_st *clone)
36 memcached_clone(&memc, clone);
41 memcached_free(&memc);
44 bool fetch(std::string &key,
45 std::vector<char> &ret_val,
49 char ret_key[MEMCACHED_MAX_KEY];
50 size_t value_length= 0;
52 char *value= memcached_fetch(&memc, ret_key, &key_length,
53 &value_length, flags, rc);
56 ret_val.reserve(value_length);
57 memcpy(&*ret_val.begin(), value, value_length);
64 std::vector<char> &get(const std::string &key,
65 std::vector<char> &ret_val)
69 size_t value_length= 0;
71 char *value= memcached_get(&memc, key.c_str(), key.length(),
72 &value_length, &flags, &rc);
75 ret_val.reserve(value_length);
76 memcpy(&ret_val[0], value, value_length);
81 std::vector<char> &getByKey(const std::string &master_key,
82 const std::string &key,
83 std::vector<char> &ret_val)
87 size_t value_length= 0;
89 char *value= memcached_get_by_key(&memc,
90 master_key.c_str(), master_key.length(),
91 key.c_str(), key.length(),
92 &value_length, &flags, &rc);
95 ret_val.reserve(value_length);
96 memcpy(&*ret_val.begin(), value, value_length);
101 bool mget(std::vector<std::string> &keys)
103 std::vector<const char *> real_keys;
104 std::vector<size_t> key_len;
106 * Construct an array which will contain the length
107 * of each of the strings in the input vector. Also, to
108 * interface with the memcached C API, we need to convert
109 * the vector of std::string's to a vector of char *.
111 real_keys.reserve(keys.size());
112 key_len.reserve(keys.size());
114 std::vector<std::string>::iterator it= keys.begin();
116 while (it != keys.end())
118 real_keys.push_back(const_cast<char *>((*it).c_str()));
119 key_len.push_back((*it).length());
124 * If the std::vector of keys is empty then we cannot
125 * call memcached_mget as we will get undefined behavior.
127 if (!real_keys.empty())
129 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
131 return (rc == MEMCACHED_SUCCESS);
137 bool set(const std::string &key,
138 const std::vector<char> &value,
142 memcached_return rc= memcached_set(&memc,
143 key.c_str(), key.length(),
144 &value[0], value.size(),
146 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
149 bool setAll(std::vector<std::string> &keys,
150 std::vector< std::vector<char> > &values,
154 if (keys.size() != values.size())
159 std::vector<std::string>::iterator key_it= keys.begin();
160 std::vector< std::vector<char> >::iterator val_it= values.begin();
161 while (key_it != keys.end())
163 retval= set((*key_it), (*val_it), expiration, flags);
174 bool setByKey(const std::string &master_key,
175 const std::string &key,
176 const std::vector<char> &value,
180 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
182 key.c_str(), key.length(),
183 &value[0], value.size(),
186 return (rc == MEMCACHED_SUCCESS);
189 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
191 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
193 return (rc == MEMCACHED_SUCCESS);
196 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
198 memcached_return rc= memcached_decrement(&memc, key.c_str(),
201 return (rc == MEMCACHED_SUCCESS);
205 bool add(const std::string &key, const std::string &value)
207 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
208 value.c_str(), value.length(), 0, 0);
209 return (rc == MEMCACHED_SUCCESS);
212 bool addByKey(const std::string &master_key,
213 const std::string &key,
214 const std::string &value)
216 memcached_return rc= memcached_add_by_key(&memc,
224 return (rc == MEMCACHED_SUCCESS);
227 bool replace(const std::string &key, const std::string &value)
229 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
230 value.c_str(), value.length(),
232 return (rc == MEMCACHED_SUCCESS);
235 bool replaceByKey(const std::string &master_key,
236 const std::string &key,
237 const std::string &value)
239 memcached_return rc= memcached_replace_by_key(&memc,
247 return (rc == MEMCACHED_SUCCESS);
250 bool prepend(const std::string &key, const std::string &value)
252 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
253 value.c_str(), value.length(), 0, 0);
254 return (rc == MEMCACHED_SUCCESS);
257 bool prependByKey(const std::string &master_key,
258 const std::string &key,
259 const std::string &value)
261 memcached_return rc= memcached_prepend_by_key(&memc,
270 return (rc == MEMCACHED_SUCCESS);
273 bool append(const std::string &key, const std::string &value)
275 memcached_return rc= memcached_append(&memc,
281 return (rc == MEMCACHED_SUCCESS);
284 bool appendByKey(const std::string &master_key,
285 const std::string &key,
286 const std::string &value)
288 memcached_return rc= memcached_append_by_key(&memc,
296 return (rc == MEMCACHED_SUCCESS);
299 bool cas(const std::string &key,
300 const std::string &value,
303 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
304 value.c_str(), value.length(),
306 return (rc == MEMCACHED_SUCCESS);
309 bool casByKey(const std::string &master_key,
310 const std::string &key,
311 const std::string &value,
314 memcached_return rc= memcached_cas_by_key(&memc,
322 return (rc == MEMCACHED_SUCCESS);
325 // using 'remove' vs. 'delete' since 'delete' is a keyword
326 bool remove(const std::string &key)
328 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
329 return (rc == MEMCACHED_SUCCESS);
332 bool removeByKey(const std::string &master_key,
333 const std::string &key)
335 memcached_return rc= memcached_delete_by_key(&memc,
341 return (rc == MEMCACHED_SUCCESS);
344 bool flush(time_t expiration)
346 memcached_return rc= memcached_flush(&memc, expiration);
347 return (rc == MEMCACHED_SUCCESS);
350 bool fetchExecute(memcached_execute_function *callback,
352 unsigned int num_of_callbacks)
354 memcached_return rc= memcached_fetch_execute(&memc,
358 return (rc == MEMCACHED_SUCCESS);
361 const std::string libVersion() const
363 const char *ver= memcached_lib_version();
364 const std::string version(ver);
370 memcached_result_st result;
373 #endif /* LIBMEMCACHEDPP_H */