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)
86 * Construct an array which will contain the length
87 * of each of the strings in the input vector. Also, to
88 * interface with the memcached C API, we need to convert
89 * the vector of std::string's to a vector of char *.
91 size_t *key_len= static_cast<size_t *>(malloc(keys.size() * sizeof(size_t)));
96 std::vector<char *> real_keys;
97 std::vector<std::string>::iterator it= keys.begin();
99 while (it != keys.end())
101 real_keys.push_back(const_cast<char *>((*it).c_str()));
102 key_len[i++]= (*it).length();
107 * If the std::vector of keys is empty then we cannot
108 * call memcached_mget as we will get undefined behavior.
110 if (!real_keys.empty())
112 memcached_return rc= memcached_mget(&memc, &real_keys[0], key_len,
113 static_cast<unsigned int>(real_keys.size()));
114 return (rc == MEMCACHED_SUCCESS);
120 bool set(const std::string &key,
121 const std::string &value,
125 memcached_return rc= memcached_set(&memc,
126 key.c_str(), key.length(),
127 value.c_str(), value.length(),
129 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
132 bool set_all(std::vector<std::string> &keys,
133 std::vector<std::string> &values,
137 if (keys.size() != values.size())
142 std::vector<std::string>::iterator key_it= keys.begin();
143 std::vector<std::string>::iterator val_it= values.begin();
144 while (key_it != keys.end())
146 retval= set((*key_it), (*val_it), expiration, flags);
157 bool set_by_key(const std::string &master_key,
158 const std::string &key,
159 const std::string &value,
163 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
165 key.c_str(), key.length(),
166 value.c_str(), value.length(),
169 return (rc == MEMCACHED_SUCCESS);
172 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
174 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
176 return (rc == MEMCACHED_SUCCESS);
179 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
181 memcached_return rc= memcached_decrement(&memc, key.c_str(),
184 return (rc == MEMCACHED_SUCCESS);
188 bool add(const std::string &key, const std::string &value)
190 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
191 value.c_str(), value.length(), 0, 0);
192 return (rc == MEMCACHED_SUCCESS);
195 bool add_by_key(const std::string &master_key,
196 const std::string &key,
197 const std::string &value)
199 memcached_return rc= memcached_add_by_key(&memc,
207 return (rc == MEMCACHED_SUCCESS);
210 bool replace(const std::string &key, const std::string &value)
212 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
213 value.c_str(), value.length(),
215 return (rc == MEMCACHED_SUCCESS);
218 bool replace_by_key(const std::string &master_key,
219 const std::string &key,
220 const std::string &value)
222 memcached_return rc= memcached_replace_by_key(&memc,
230 return (rc == MEMCACHED_SUCCESS);
233 bool prepend(const std::string &key, const std::string &value)
235 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
236 value.c_str(), value.length(), 0, 0);
237 return (rc == MEMCACHED_SUCCESS);
240 bool prepend_by_key(const std::string &master_key,
241 const std::string &key,
242 const std::string &value)
244 memcached_return rc= memcached_prepend_by_key(&memc,
253 return (rc == MEMCACHED_SUCCESS);
256 bool append(const std::string &key, const std::string &value)
258 memcached_return rc= memcached_append(&memc,
264 return (rc == MEMCACHED_SUCCESS);
267 bool append_by_key(const std::string &master_key,
268 const std::string &key,
269 const std::string &value)
271 memcached_return rc= memcached_append_by_key(&memc,
279 return (rc == MEMCACHED_SUCCESS);
282 bool cas(const std::string &key,
283 const std::string &value,
286 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
287 value.c_str(), value.length(),
289 return (rc == MEMCACHED_SUCCESS);
292 bool cas_by_key(const std::string &master_key,
293 const std::string &key,
294 const std::string &value,
297 memcached_return rc= memcached_cas_by_key(&memc,
305 return (rc == MEMCACHED_SUCCESS);
308 // using 'remove' vs. 'delete' since 'delete' is a keyword
309 bool remove(const std::string &key)
311 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
312 return (rc == MEMCACHED_SUCCESS);
315 bool delete_by_key(const std::string &master_key,
316 const std::string &key)
318 memcached_return rc= memcached_delete_by_key(&memc,
324 return (rc == MEMCACHED_SUCCESS);
327 bool flush(time_t expiration)
329 memcached_return rc= memcached_flush(&memc, expiration);
330 return (rc == MEMCACHED_SUCCESS);
333 bool fetch_execute(memcached_execute_function *callback,
335 unsigned int num_of_callbacks)
337 memcached_return rc= memcached_fetch_execute(&memc,
341 return (rc == MEMCACHED_SUCCESS);
344 const std::string lib_version() const
346 const char *ver= memcached_lib_version();
347 const std::string version(ver);
353 memcached_result_st result;