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, const std::string &value)
118 memcached_return rc= memcached_set(&memc,
119 key.c_str(), key.length(),
120 value.c_str(), value.length(),
121 time_t(0), uint32_t(0));
122 return (rc == MEMCACHED_SUCCESS);
125 bool set_by_key(const std::string &master_key,
126 const std::string &key,
127 const std::string &value)
129 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
131 key.c_str(), key.length(),
132 value.c_str(), value.length(),
135 return (rc == MEMCACHED_SUCCESS);
138 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
140 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
142 return (rc == MEMCACHED_SUCCESS);
145 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
147 memcached_return rc= memcached_decrement(&memc, key.c_str(),
150 return (rc == MEMCACHED_SUCCESS);
154 bool add(const std::string &key, const std::string &value)
156 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
157 value.c_str(), value.length(), 0, 0);
158 return (rc == MEMCACHED_SUCCESS);
161 bool add_by_key(const std::string &master_key,
162 const std::string &key,
163 const std::string &value)
165 memcached_return rc= memcached_add_by_key(&memc,
173 return (rc == MEMCACHED_SUCCESS);
176 bool replace(const std::string &key, const std::string &value)
178 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
179 value.c_str(), value.length(),
181 return (rc == MEMCACHED_SUCCESS);
184 bool replace_by_key(const std::string &master_key,
185 const std::string &key,
186 const std::string &value)
188 memcached_return rc= memcached_replace_by_key(&memc,
196 return (rc == MEMCACHED_SUCCESS);
199 bool prepend(const std::string &key, const std::string &value)
201 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
202 value.c_str(), value.length(), 0, 0);
203 return (rc == MEMCACHED_SUCCESS);
206 bool prepend_by_key(const std::string &master_key,
207 const std::string &key,
208 const std::string &value)
210 memcached_return rc= memcached_prepend_by_key(&memc,
219 return (rc == MEMCACHED_SUCCESS);
222 bool append(const std::string &key, const std::string &value)
224 memcached_return rc= memcached_append(&memc,
230 return (rc == MEMCACHED_SUCCESS);
233 bool append_by_key(const std::string &master_key,
234 const std::string &key,
235 const std::string &value)
237 memcached_return rc= memcached_append_by_key(&memc,
245 return (rc == MEMCACHED_SUCCESS);
248 bool cas(const std::string &key,
249 const std::string &value,
252 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
253 value.c_str(), value.length(),
255 return (rc == MEMCACHED_SUCCESS);
258 bool cas_by_key(const std::string &master_key,
259 const std::string &key,
260 const std::string &value,
263 memcached_return rc= memcached_cas_by_key(&memc,
271 return (rc == MEMCACHED_SUCCESS);
274 // using 'remove' vs. 'delete' since 'delete' is a keyword
275 bool remove(const std::string &key)
277 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
278 return (rc == MEMCACHED_SUCCESS);
281 bool delete_by_key(const std::string &master_key,
282 const std::string &key)
284 memcached_return rc= memcached_delete_by_key(&memc,
290 return (rc == MEMCACHED_SUCCESS);
293 const std::string lib_version() const
295 const char *ver= memcached_lib_version();
296 const std::string version(ver);
302 memcached_result_st result;