1 #include <libmemcached/memcached.h>
15 memcached_create(&memc);
18 Memcached(memcached_st *clone)
23 memcached_clone(&memc, clone);
28 memcached_free(&memc);
31 std::string fetch(std::string &key, size_t *key_length, size_t *value_length)
37 char *value= memcached_fetch(&memc, const_cast<char *>(key.c_str()), key_length,
38 value_length, &flags, &rc);
41 ret_val.assign(value);
46 std::string get(const std::string &key, size_t *value_length)
52 char *value= memcached_get(&memc, key.c_str(), key.length(),
53 value_length, &flags, &rc);
56 ret_val.assign(value);
61 std::string get_by_key(const std::string &master_key,
62 const std::string &key,
69 char *value= memcached_get_by_key(&memc, master_key.c_str(), master_key.length(),
70 key.c_str(), key.length(),
71 value_length, &flags, &rc);
74 ret_val.assign(value);
79 bool mget(std::vector<std::string> &keys)
82 * Construct an array which will contain the length
83 * of each of the strings in the input vector. Also, to
84 * interface with the memcached C API, we need to convert
85 * the vector of std::string's to a vector of char *.
87 size_t *key_len= static_cast<size_t *>(malloc(keys.size() * sizeof(size_t)));
92 std::vector<char *> real_keys;
93 std::vector<std::string>::iterator it= keys.begin();
95 while (it != keys.end())
97 real_keys.push_back(const_cast<char *>((*it).c_str()));
98 key_len[i++]= (*it).length();
103 * If the std::vector of keys is empty then we cannot
104 * call memcached_mget as we will get undefined behavior.
106 if (!real_keys.empty())
108 memcached_return rc= memcached_mget(&memc, &real_keys[0], key_len,
109 static_cast<unsigned int>(real_keys.size()));
110 return (rc == MEMCACHED_SUCCESS);
116 bool set(const std::string &key,
117 const std::string &value,
121 memcached_return rc= memcached_set(&memc,
122 key.c_str(), key.length(),
123 value.c_str(), value.length(),
125 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
128 bool set_all(std::vector<std::string> &keys,
129 std::vector<std::string> &values,
133 if (keys.size() != values.size())
138 std::vector<std::string>::iterator key_it= keys.begin();
139 std::vector<std::string>::iterator val_it= values.begin();
140 while (key_it != keys.end())
142 retval= set((*key_it), (*val_it), expiration, flags);
153 bool set_by_key(const std::string &master_key,
154 const std::string &key,
155 const std::string &value,
159 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
161 key.c_str(), key.length(),
162 value.c_str(), value.length(),
165 return (rc == MEMCACHED_SUCCESS);
168 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
170 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
172 return (rc == MEMCACHED_SUCCESS);
175 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
177 memcached_return rc= memcached_decrement(&memc, key.c_str(),
180 return (rc == MEMCACHED_SUCCESS);
184 bool add(const std::string &key, const std::string &value)
186 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
187 value.c_str(), value.length(), 0, 0);
188 return (rc == MEMCACHED_SUCCESS);
191 bool add_by_key(const std::string &master_key,
192 const std::string &key,
193 const std::string &value)
195 memcached_return rc= memcached_add_by_key(&memc,
203 return (rc == MEMCACHED_SUCCESS);
206 bool replace(const std::string &key, const std::string &value)
208 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
209 value.c_str(), value.length(),
211 return (rc == MEMCACHED_SUCCESS);
214 bool replace_by_key(const std::string &master_key,
215 const std::string &key,
216 const std::string &value)
218 memcached_return rc= memcached_replace_by_key(&memc,
226 return (rc == MEMCACHED_SUCCESS);
229 bool prepend(const std::string &key, const std::string &value)
231 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
232 value.c_str(), value.length(), 0, 0);
233 return (rc == MEMCACHED_SUCCESS);
236 bool prepend_by_key(const std::string &master_key,
237 const std::string &key,
238 const std::string &value)
240 memcached_return rc= memcached_prepend_by_key(&memc,
249 return (rc == MEMCACHED_SUCCESS);
252 bool append(const std::string &key, const std::string &value)
254 memcached_return rc= memcached_append(&memc,
260 return (rc == MEMCACHED_SUCCESS);
263 bool append_by_key(const std::string &master_key,
264 const std::string &key,
265 const std::string &value)
267 memcached_return rc= memcached_append_by_key(&memc,
275 return (rc == MEMCACHED_SUCCESS);
278 bool cas(const std::string &key,
279 const std::string &value,
282 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
283 value.c_str(), value.length(),
285 return (rc == MEMCACHED_SUCCESS);
288 bool cas_by_key(const std::string &master_key,
289 const std::string &key,
290 const std::string &value,
293 memcached_return rc= memcached_cas_by_key(&memc,
301 return (rc == MEMCACHED_SUCCESS);
304 // using 'remove' vs. 'delete' since 'delete' is a keyword
305 bool remove(const std::string &key)
307 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
308 return (rc == MEMCACHED_SUCCESS);
311 bool delete_by_key(const std::string &master_key,
312 const std::string &key)
314 memcached_return rc= memcached_delete_by_key(&memc,
320 return (rc == MEMCACHED_SUCCESS);
323 bool flush(time_t expiration)
325 memcached_return rc= memcached_flush(&memc, expiration);
326 return (rc == MEMCACHED_SUCCESS);
329 bool fetch_execute(memcached_execute_function *callback,
331 unsigned int num_of_callbacks)
333 memcached_return rc= memcached_fetch_execute(&memc,
337 return (rc == MEMCACHED_SUCCESS);
340 const std::string lib_version() const
342 const char *ver= memcached_lib_version();
343 const std::string version(ver);
349 memcached_result_st result;