1 |
torben |
644 |
/* |
2 |
|
|
based on original memcached.hh from libmemcached 0.31 |
3 |
|
|
additions: |
4 |
|
|
server_add() |
5 |
|
|
std::string overloads for set(), get(), remove() |
6 |
|
|
|
7 |
|
|
*/ |
8 |
|
|
|
9 |
|
|
#include "libmemcached/memcached.h" |
10 |
|
|
#include <string> |
11 |
|
|
#include <string.h> |
12 |
|
|
#include <stdio.h> |
13 |
|
|
|
14 |
|
|
class Memcached |
15 |
|
|
{ |
16 |
|
|
memcached_st memc; |
17 |
|
|
memcached_result_st result; |
18 |
|
|
|
19 |
|
|
public: |
20 |
|
|
|
21 |
|
|
Memcached() : memc(), result() |
22 |
|
|
{ |
23 |
|
|
memcached_create(&memc); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
Memcached(memcached_st *clone) : memc(), result() |
27 |
|
|
{ |
28 |
|
|
memcached_clone(&memc, clone); |
29 |
|
|
} |
30 |
|
|
char *fetch (char *key, size_t *key_length, size_t *value_length) |
31 |
|
|
{ |
32 |
|
|
uint32_t flags; |
33 |
|
|
memcached_return rc; |
34 |
|
|
|
35 |
|
|
return memcached_fetch(&memc, key, key_length, |
36 |
|
|
value_length, &flags, &rc); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
char *get(std::string key, size_t *value_length) |
41 |
|
|
{ |
42 |
|
|
return get(key.c_str(), value_length); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
char *get(const char *key, size_t *value_length) |
46 |
|
|
{ |
47 |
|
|
uint32_t flags; |
48 |
|
|
memcached_return rc; |
49 |
|
|
|
50 |
|
|
return memcached_get(&memc, key, strlen(key), |
51 |
|
|
value_length, &flags, &rc); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
char *get_by_key(const char *master_key, const char *key, |
55 |
|
|
size_t *value_length) |
56 |
|
|
{ |
57 |
|
|
uint32_t flags; |
58 |
|
|
memcached_return rc; |
59 |
|
|
|
60 |
|
|
return memcached_get_by_key(&memc, master_key, strlen(master_key), |
61 |
|
|
key, strlen(key), |
62 |
|
|
value_length, &flags, &rc); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
memcached_return mget(char **keys, size_t *key_length, |
66 |
|
|
unsigned int number_of_keys) |
67 |
|
|
{ |
68 |
|
|
|
69 |
|
|
return memcached_mget(&memc, keys, key_length, number_of_keys); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
|
73 |
|
|
memcached_return set(std::string key, const char *value, size_t value_length, unsigned int expire=0) |
74 |
|
|
{ |
75 |
|
|
return set(key.c_str(), value, value_length, expire); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
memcached_return set(const char *key, const char *value, size_t value_length, unsigned int expire=0) |
79 |
|
|
{ |
80 |
|
|
return memcached_set(&memc, key, strlen(key), |
81 |
|
|
value, value_length, |
82 |
|
|
time_t(expire), uint32_t(0)); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
memcached_return set_by_key(const char *master_key, const char *key, |
86 |
|
|
const char *value, size_t value_length) |
87 |
|
|
{ |
88 |
|
|
return memcached_set_by_key(&memc, master_key, strlen(master_key), |
89 |
|
|
key, strlen(key), |
90 |
|
|
value, value_length, |
91 |
|
|
time_t(0), |
92 |
|
|
uint32_t(0) ); |
93 |
|
|
} |
94 |
|
|
memcached_return |
95 |
|
|
increment(const char *key, unsigned int offset, uint64_t *value) |
96 |
|
|
{ |
97 |
|
|
return memcached_increment(&memc, key, strlen(key), |
98 |
|
|
offset, value); |
99 |
|
|
} |
100 |
|
|
memcached_return |
101 |
|
|
decrement(const char *key, unsigned int offset, uint64_t *value) |
102 |
|
|
{ |
103 |
|
|
return memcached_decrement(&memc, key, strlen(key), |
104 |
|
|
offset, value); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
|
108 |
|
|
memcached_return add(const char *key, const char *value, size_t value_length) |
109 |
|
|
{ |
110 |
|
|
return memcached_add(&memc, key, strlen(key), value, value_length, 0, 0); |
111 |
|
|
} |
112 |
|
|
memcached_return add_by_key(const char *master_key, const char *key, |
113 |
|
|
const char *value, size_t value_length) |
114 |
|
|
{ |
115 |
|
|
return memcached_add_by_key(&memc, master_key, strlen(master_key), |
116 |
|
|
key, strlen(key), |
117 |
|
|
value, value_length, |
118 |
|
|
0, 0); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
memcached_return replace(const char *key, const char *value, |
122 |
|
|
size_t value_length) |
123 |
|
|
{ |
124 |
|
|
return memcached_replace(&memc, key, strlen(key), |
125 |
|
|
value, value_length, |
126 |
|
|
0, 0); |
127 |
|
|
} |
128 |
|
|
memcached_return replace_by_key(const char *master_key, const char *key, |
129 |
|
|
const char *value, size_t value_length) |
130 |
|
|
{ |
131 |
|
|
return memcached_replace_by_key(&memc, master_key, strlen(master_key), |
132 |
|
|
key, strlen(key), |
133 |
|
|
value, value_length, 0, 0); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
memcached_return prepend(const char *key, const char *value, |
137 |
|
|
size_t value_length) |
138 |
|
|
{ |
139 |
|
|
return memcached_prepend(&memc, key, strlen(key), |
140 |
|
|
value, value_length, 0, 0); |
141 |
|
|
} |
142 |
|
|
memcached_return prepend_by_key(const char *master_key, const char *key, |
143 |
|
|
const char *value, size_t value_length) |
144 |
|
|
{ |
145 |
|
|
return memcached_prepend_by_key(&memc, master_key, strlen(master_key), |
146 |
|
|
key, strlen(key), |
147 |
|
|
value, value_length, |
148 |
|
|
0, |
149 |
|
|
0); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
memcached_return append(const char *key, const char *value, |
153 |
|
|
size_t value_length) |
154 |
|
|
{ |
155 |
|
|
return memcached_append(&memc, key, strlen(key), |
156 |
|
|
value, value_length, 0, 0); |
157 |
|
|
} |
158 |
|
|
memcached_return append_by_key(const char *master_key, const char *key, |
159 |
|
|
const char *value, size_t value_length) |
160 |
|
|
{ |
161 |
|
|
return memcached_append_by_key(&memc, |
162 |
|
|
master_key, strlen(master_key), |
163 |
|
|
key, strlen(key), |
164 |
|
|
value, value_length, 0, 0); |
165 |
|
|
} |
166 |
|
|
memcached_return cas(const char *key, const char *value, |
167 |
|
|
size_t value_length, uint64_t cas_arg) |
168 |
|
|
{ |
169 |
|
|
return memcached_cas(&memc, key, strlen(key), |
170 |
|
|
value, value_length, 0, 0, cas_arg); |
171 |
|
|
} |
172 |
|
|
memcached_return cas_by_key(const char *master_key, const char *key, |
173 |
|
|
const char *value, size_t value_length, |
174 |
|
|
uint64_t cas_arg) |
175 |
|
|
{ |
176 |
|
|
return memcached_cas_by_key(&memc, |
177 |
|
|
master_key, strlen(master_key), |
178 |
|
|
key, strlen(key), |
179 |
|
|
value, value_length, |
180 |
|
|
0, 0, cas_arg); |
181 |
|
|
} |
182 |
|
|
// using 'remove' vs. 'delete' since 'delete' is a keyword |
183 |
|
|
|
184 |
|
|
memcached_return remove(std::string key) |
185 |
|
|
{ |
186 |
|
|
return remove(key.c_str()); |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
memcached_return remove(const char *key) |
190 |
|
|
{ |
191 |
|
|
return memcached_delete (&memc, key, strlen(key), 0); |
192 |
|
|
|
193 |
|
|
} |
194 |
|
|
memcached_return delete_by_key(const char *master_key, const char *key) |
195 |
|
|
{ |
196 |
|
|
return memcached_delete_by_key(&memc, master_key, strlen(master_key), |
197 |
|
|
key, strlen(key), 0); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
memcached_return server_add(const char* hostname, unsigned int port=11211) |
201 |
|
|
{ |
202 |
|
|
return memcached_server_add(&memc, hostname, port); |
203 |
|
|
} |
204 |
|
|
~Memcached() |
205 |
|
|
{ |
206 |
|
|
memcached_free(&memc); |
207 |
|
|
} |
208 |
|
|
}; |