55b099453a0c43203e1d388bb0c1313581a40492
[m6w6/libmemcached] / libmemcached / memcached.hh
1 #include "libmemcached/memcached.h"
2 #include <string.h>
3 #include <stdio.h>
4
5 #include <string>
6
7 class Memcached
8 {
9 memcached_st memc;
10 memcached_result_st result;
11
12 public:
13
14 Memcached()
15 :
16 memc(),
17 result()
18 {
19 memcached_create(&memc);
20 }
21
22 Memcached(memcached_st *clone)
23 :
24 memc(),
25 result()
26 {
27 memcached_clone(&memc, clone);
28 }
29
30 ~Memcached()
31 {
32 memcached_free(&memc);
33 }
34
35 std::string fetch(const std::string& key, size_t *key_length, size_t *value_length)
36 {
37 uint32_t flags;
38 memcached_return rc;
39 std::string ret_val;
40
41 char *value= memcached_fetch(&memc, const_cast<char *>(key.c_str()), key_length,
42 value_length, &flags, &rc);
43 if (value)
44 {
45 ret_val.assign(value);
46 }
47 return ret_val;
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 memcached_return mget(char **keys, size_t *key_length,
84 unsigned int number_of_keys)
85 {
86
87 return memcached_mget(&memc, keys, key_length, number_of_keys);
88 }
89
90 memcached_return set(const std::string& key, const char *value, size_t value_length)
91 {
92 return memcached_set(&memc, key.c_str(), key.length(),
93 value, value_length,
94 time_t(0), uint32_t(0));
95 }
96
97 memcached_return set_by_key(const std::string& master_key,
98 const std::string& key,
99 const char *value, size_t value_length)
100 {
101 return memcached_set_by_key(&memc, master_key.c_str(),
102 master_key.length(),
103 key.c_str(), key.length(),
104 value, value_length,
105 time_t(0),
106 uint32_t(0));
107 }
108
109 memcached_return
110 increment(const std::string& key, unsigned int offset, uint64_t *value)
111 {
112 return memcached_increment(&memc, key.c_str(), key.length(),
113 offset, value);
114 }
115
116 memcached_return
117 decrement(const char *key, unsigned int offset, uint64_t *value)
118 {
119 return memcached_decrement(&memc, key, strlen(key),
120 offset, value);
121 }
122
123
124 memcached_return add(const char *key, const char *value, size_t value_length)
125 {
126 return memcached_add(&memc, key, strlen(key), value, value_length, 0, 0);
127 }
128
129 memcached_return add_by_key(const char *master_key, const char *key,
130 const char *value, size_t value_length)
131 {
132 return memcached_add_by_key(&memc, master_key, strlen(master_key),
133 key, strlen(key),
134 value, value_length,
135 0, 0);
136 }
137
138 memcached_return replace(const char *key, const char *value,
139 size_t value_length)
140 {
141 return memcached_replace(&memc, key, strlen(key),
142 value, value_length,
143 0, 0);
144 }
145
146 memcached_return replace_by_key(const char *master_key, const char *key,
147 const char *value, size_t value_length)
148 {
149 return memcached_replace_by_key(&memc, master_key, strlen(master_key),
150 key, strlen(key),
151 value, value_length, 0, 0);
152 }
153
154 memcached_return prepend(const char *key, const char *value,
155 size_t value_length)
156 {
157 return memcached_prepend(&memc, key, strlen(key),
158 value, value_length, 0, 0);
159 }
160
161 memcached_return prepend_by_key(const char *master_key, const char *key,
162 const char *value, size_t value_length)
163 {
164 return memcached_prepend_by_key(&memc, master_key, strlen(master_key),
165 key, strlen(key),
166 value, value_length,
167 0,
168 0);
169 }
170
171 memcached_return append(const char *key, const char *value,
172 size_t value_length)
173 {
174 return memcached_append(&memc, key, strlen(key),
175 value, value_length, 0, 0);
176 }
177
178 memcached_return append_by_key(const char *master_key, const char *key,
179 const char *value, size_t value_length)
180 {
181 return memcached_append_by_key(&memc,
182 master_key, strlen(master_key),
183 key, strlen(key),
184 value, value_length, 0, 0);
185 }
186
187 memcached_return cas(const char *key, const char *value,
188 size_t value_length, uint64_t cas_arg)
189 {
190 return memcached_cas(&memc, key, strlen(key),
191 value, value_length, 0, 0, cas_arg);
192 }
193
194 memcached_return cas_by_key(const char *master_key, const char *key,
195 const char *value, size_t value_length,
196 uint64_t cas_arg)
197 {
198 return memcached_cas_by_key(&memc,
199 master_key, strlen(master_key),
200 key, strlen(key),
201 value, value_length,
202 0, 0, cas_arg);
203 }
204
205 // using 'remove' vs. 'delete' since 'delete' is a keyword
206 memcached_return remove(const char *key)
207 {
208 return memcached_delete (&memc, key, strlen(key), 0);
209
210 }
211
212 memcached_return delete_by_key(const char *master_key, const char *key)
213 {
214 return memcached_delete_by_key(&memc, master_key, strlen(master_key),
215 key, strlen(key), 0);
216 }
217 };