Converted some of the memcached API's to use size_t instead of unsigned int.
[m6w6/libmemcached] / libmemcached / memcached.hh
1 #include <libmemcached/memcached.h>
2
3 #include <string>
4 #include <vector>
5
6 class Memcached
7 {
8 public:
9
10 Memcached()
11 :
12 memc(),
13 result()
14 {
15 memcached_create(&memc);
16 }
17
18 Memcached(memcached_st *clone)
19 :
20 memc(),
21 result()
22 {
23 memcached_clone(&memc, clone);
24 }
25
26 ~Memcached()
27 {
28 memcached_free(&memc);
29 }
30
31 bool fetch(std::string &key,
32 std::string &ret_val,
33 size_t *key_length,
34 size_t *value_length,
35 uint32_t *flags,
36 memcached_return *rc)
37 {
38 char ret_key[MEMCACHED_MAX_KEY];
39 char *value= memcached_fetch(&memc, ret_key, key_length,
40 value_length, flags, rc);
41 if (value)
42 {
43 ret_val.assign(value);
44 key.assign(ret_key);
45 return true;
46 }
47 return false;
48 }
49
50 std::string get(const std::string &key, size_t *value_length)
51 {
52 uint32_t flags;
53 memcached_return rc;
54 std::string ret_val;
55
56 char *value= memcached_get(&memc, key.c_str(), key.length(),
57 value_length, &flags, &rc);
58 if (value)
59 {
60 ret_val.assign(value);
61 }
62 return ret_val;
63 }
64
65 std::string get_by_key(const std::string &master_key,
66 const std::string &key,
67 size_t *value_length)
68 {
69 uint32_t flags;
70 memcached_return rc;
71 std::string ret_val;
72
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);
76 if (value)
77 {
78 ret_val.assign(value);
79 }
80 return ret_val;
81 }
82
83 bool mget(std::vector<std::string> &keys)
84 {
85 std::vector<const char *> real_keys;
86 std::vector<size_t> key_len;
87 /*
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 *.
92 */
93 real_keys.reserve(keys.size());
94 key_len.reserve(keys.size());
95
96 std::vector<std::string>::iterator it= keys.begin();
97
98 while (it != keys.end())
99 {
100 real_keys.push_back(const_cast<char *>((*it).c_str()));
101 key_len.push_back((*it).length());
102 ++it;
103 }
104
105 /*
106 * If the std::vector of keys is empty then we cannot
107 * call memcached_mget as we will get undefined behavior.
108 */
109 if (!real_keys.empty())
110 {
111 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
112 real_keys.size());
113 return (rc == MEMCACHED_SUCCESS);
114 }
115
116 return false;
117 }
118
119 bool set(const std::string &key,
120 const std::string &value,
121 time_t expiration,
122 uint32_t flags)
123 {
124 memcached_return rc= memcached_set(&memc,
125 key.c_str(), key.length(),
126 value.c_str(), value.length(),
127 expiration, flags);
128 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
129 }
130
131 bool set_all(std::vector<std::string> &keys,
132 std::vector<std::string> &values,
133 time_t expiration,
134 uint32_t flags)
135 {
136 if (keys.size() != values.size())
137 {
138 return false;
139 }
140 bool retval= true;
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())
144 {
145 retval= set((*key_it), (*val_it), expiration, flags);
146 if (retval == false)
147 {
148 return retval;
149 }
150 ++key_it;
151 ++val_it;
152 }
153 return retval;
154 }
155
156 bool set_by_key(const std::string &master_key,
157 const std::string &key,
158 const std::string &value,
159 time_t expiration,
160 uint32_t flags)
161 {
162 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
163 master_key.length(),
164 key.c_str(), key.length(),
165 value.c_str(), value.length(),
166 expiration,
167 flags);
168 return (rc == MEMCACHED_SUCCESS);
169 }
170
171 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
172 {
173 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
174 offset, value);
175 return (rc == MEMCACHED_SUCCESS);
176 }
177
178 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
179 {
180 memcached_return rc= memcached_decrement(&memc, key.c_str(),
181 key.length(),
182 offset, value);
183 return (rc == MEMCACHED_SUCCESS);
184 }
185
186
187 bool add(const std::string &key, const std::string &value)
188 {
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);
192 }
193
194 bool add_by_key(const std::string &master_key,
195 const std::string &key,
196 const std::string &value)
197 {
198 memcached_return rc= memcached_add_by_key(&memc,
199 master_key.c_str(),
200 master_key.length(),
201 key.c_str(),
202 key.length(),
203 value.c_str(),
204 value.length(),
205 0, 0);
206 return (rc == MEMCACHED_SUCCESS);
207 }
208
209 bool replace(const std::string &key, const std::string &value)
210 {
211 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
212 value.c_str(), value.length(),
213 0, 0);
214 return (rc == MEMCACHED_SUCCESS);
215 }
216
217 bool replace_by_key(const std::string &master_key,
218 const std::string &key,
219 const std::string &value)
220 {
221 memcached_return rc= memcached_replace_by_key(&memc,
222 master_key.c_str(),
223 master_key.length(),
224 key.c_str(),
225 key.length(),
226 value.c_str(),
227 value.length(),
228 0, 0);
229 return (rc == MEMCACHED_SUCCESS);
230 }
231
232 bool prepend(const std::string &key, const std::string &value)
233 {
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);
237 }
238
239 bool prepend_by_key(const std::string &master_key,
240 const std::string &key,
241 const std::string &value)
242 {
243 memcached_return rc= memcached_prepend_by_key(&memc,
244 master_key.c_str(),
245 master_key.length(),
246 key.c_str(),
247 key.length(),
248 value.c_str(),
249 value.length(),
250 0,
251 0);
252 return (rc == MEMCACHED_SUCCESS);
253 }
254
255 bool append(const std::string &key, const std::string &value)
256 {
257 memcached_return rc= memcached_append(&memc,
258 key.c_str(),
259 key.length(),
260 value.c_str(),
261 value.length(),
262 0, 0);
263 return (rc == MEMCACHED_SUCCESS);
264 }
265
266 bool append_by_key(const std::string &master_key,
267 const std::string &key,
268 const std::string &value)
269 {
270 memcached_return rc= memcached_append_by_key(&memc,
271 master_key.c_str(),
272 master_key.length(),
273 key.c_str(),
274 key.length(),
275 value.c_str(),
276 value.length(),
277 0, 0);
278 return (rc == MEMCACHED_SUCCESS);
279 }
280
281 bool cas(const std::string &key,
282 const std::string &value,
283 uint64_t cas_arg)
284 {
285 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
286 value.c_str(), value.length(),
287 0, 0, cas_arg);
288 return (rc == MEMCACHED_SUCCESS);
289 }
290
291 bool cas_by_key(const std::string &master_key,
292 const std::string &key,
293 const std::string &value,
294 uint64_t cas_arg)
295 {
296 memcached_return rc= memcached_cas_by_key(&memc,
297 master_key.c_str(),
298 master_key.length(),
299 key.c_str(),
300 key.length(),
301 value.c_str(),
302 value.length(),
303 0, 0, cas_arg);
304 return (rc == MEMCACHED_SUCCESS);
305 }
306
307 // using 'remove' vs. 'delete' since 'delete' is a keyword
308 bool remove(const std::string &key)
309 {
310 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
311 return (rc == MEMCACHED_SUCCESS);
312 }
313
314 bool delete_by_key(const std::string &master_key,
315 const std::string &key)
316 {
317 memcached_return rc= memcached_delete_by_key(&memc,
318 master_key.c_str(),
319 master_key.length(),
320 key.c_str(),
321 key.length(),
322 0);
323 return (rc == MEMCACHED_SUCCESS);
324 }
325
326 bool flush(time_t expiration)
327 {
328 memcached_return rc= memcached_flush(&memc, expiration);
329 return (rc == MEMCACHED_SUCCESS);
330 }
331
332 bool fetch_execute(memcached_execute_function *callback,
333 void *context,
334 unsigned int num_of_callbacks)
335 {
336 memcached_return rc= memcached_fetch_execute(&memc,
337 callback,
338 context,
339 num_of_callbacks);
340 return (rc == MEMCACHED_SUCCESS);
341 }
342
343 const std::string lib_version() const
344 {
345 const char *ver= memcached_lib_version();
346 const std::string version(ver);
347 return version;
348 }
349
350 private:
351 memcached_st memc;
352 memcached_result_st result;
353 };