1 #include <libmemcached/memcached.h>
15 memcached_create(&memc);
18 Memcached(memcached_st *clone)
23 memcached_clone(&memc, clone);
28 memcached_free(&memc);
31 bool fetch(std::string &key,
38 char ret_key[MEMCACHED_MAX_KEY];
39 char *value= memcached_fetch(&memc, ret_key, key_length,
40 value_length, flags, rc);
43 ret_val.assign(value);
50 std::string get(const std::string &key, size_t *value_length)
56 char *value= memcached_get(&memc, key.c_str(), key.length(),
57 value_length, &flags, &rc);
60 ret_val.assign(value);
65 std::string get_by_key(const std::string &master_key,
66 const std::string &key,
73 char *value= memcached_get_by_key(&memc, master_key.c_str(), master_key.length(),
74 key.c_str(), key.length(),
75 value_length, &flags, &rc);
78 ret_val.assign(value);
83 bool mget(std::vector<std::string> &keys)
85 std::vector<const char *> real_keys;
86 std::vector<size_t> key_len;
88 * Construct an array which will contain the length
89 * of each of the strings in the input vector. Also, to
90 * interface with the memcached C API, we need to convert
91 * the vector of std::string's to a vector of char *.
93 real_keys.reserve(keys.size());
94 key_len.reserve(keys.size());
96 std::vector<std::string>::iterator it= keys.begin();
98 while (it != keys.end())
100 real_keys.push_back(const_cast<char *>((*it).c_str()));
101 key_len.push_back((*it).length());
106 * If the std::vector of keys is empty then we cannot
107 * call memcached_mget as we will get undefined behavior.
109 if (!real_keys.empty())
111 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
113 return (rc == MEMCACHED_SUCCESS);
119 bool set(const std::string &key,
120 const std::string &value,
124 memcached_return rc= memcached_set(&memc,
125 key.c_str(), key.length(),
126 value.c_str(), value.length(),
128 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
131 bool set_all(std::vector<std::string> &keys,
132 std::vector<std::string> &values,
136 if (keys.size() != values.size())
141 std::vector<std::string>::iterator key_it= keys.begin();
142 std::vector<std::string>::iterator val_it= values.begin();
143 while (key_it != keys.end())
145 retval= set((*key_it), (*val_it), expiration, flags);
156 bool set_by_key(const std::string &master_key,
157 const std::string &key,
158 const std::string &value,
162 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
164 key.c_str(), key.length(),
165 value.c_str(), value.length(),
168 return (rc == MEMCACHED_SUCCESS);
171 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
173 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
175 return (rc == MEMCACHED_SUCCESS);
178 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
180 memcached_return rc= memcached_decrement(&memc, key.c_str(),
183 return (rc == MEMCACHED_SUCCESS);
187 bool add(const std::string &key, const std::string &value)
189 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
190 value.c_str(), value.length(), 0, 0);
191 return (rc == MEMCACHED_SUCCESS);
194 bool add_by_key(const std::string &master_key,
195 const std::string &key,
196 const std::string &value)
198 memcached_return rc= memcached_add_by_key(&memc,
206 return (rc == MEMCACHED_SUCCESS);
209 bool replace(const std::string &key, const std::string &value)
211 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
212 value.c_str(), value.length(),
214 return (rc == MEMCACHED_SUCCESS);
217 bool replace_by_key(const std::string &master_key,
218 const std::string &key,
219 const std::string &value)
221 memcached_return rc= memcached_replace_by_key(&memc,
229 return (rc == MEMCACHED_SUCCESS);
232 bool prepend(const std::string &key, const std::string &value)
234 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
235 value.c_str(), value.length(), 0, 0);
236 return (rc == MEMCACHED_SUCCESS);
239 bool prepend_by_key(const std::string &master_key,
240 const std::string &key,
241 const std::string &value)
243 memcached_return rc= memcached_prepend_by_key(&memc,
252 return (rc == MEMCACHED_SUCCESS);
255 bool append(const std::string &key, const std::string &value)
257 memcached_return rc= memcached_append(&memc,
263 return (rc == MEMCACHED_SUCCESS);
266 bool append_by_key(const std::string &master_key,
267 const std::string &key,
268 const std::string &value)
270 memcached_return rc= memcached_append_by_key(&memc,
278 return (rc == MEMCACHED_SUCCESS);
281 bool cas(const std::string &key,
282 const std::string &value,
285 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
286 value.c_str(), value.length(),
288 return (rc == MEMCACHED_SUCCESS);
291 bool cas_by_key(const std::string &master_key,
292 const std::string &key,
293 const std::string &value,
296 memcached_return rc= memcached_cas_by_key(&memc,
304 return (rc == MEMCACHED_SUCCESS);
307 // using 'remove' vs. 'delete' since 'delete' is a keyword
308 bool remove(const std::string &key)
310 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
311 return (rc == MEMCACHED_SUCCESS);
314 bool delete_by_key(const std::string &master_key,
315 const std::string &key)
317 memcached_return rc= memcached_delete_by_key(&memc,
323 return (rc == MEMCACHED_SUCCESS);
326 bool flush(time_t expiration)
328 memcached_return rc= memcached_flush(&memc, expiration);
329 return (rc == MEMCACHED_SUCCESS);
332 bool fetch_execute(memcached_execute_function *callback,
334 unsigned int num_of_callbacks)
336 memcached_return rc= memcached_fetch_execute(&memc,
340 return (rc == MEMCACHED_SUCCESS);
343 const std::string lib_version() const
345 const char *ver= memcached_lib_version();
346 const std::string version(ver);
352 memcached_result_st result;