1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
6 * \file zoom-memcached.c
7 * \brief Implements query/record caching using memcached
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
21 #include <yaz/diagbib1.h>
23 #if HAVE_LIBMEMCACHED_MEMCACHED_H
24 #if HAVE_MEMCACHED_RETURN_T
26 typedef memcached_return memcached_return_t;
30 void ZOOM_memcached_init(ZOOM_connection c)
32 #if HAVE_LIBMEMCACHED_MEMCACHED_H
37 void ZOOM_memcached_destroy(ZOOM_connection c)
39 #if HAVE_LIBMEMCACHED_MEMCACHED_H
41 memcached_free(c->mc_st);
45 #if HAVE_LIBMEMCACHED_MEMCACHED_H
46 /* memcached wrapper.. Because memcached function do not exist in older libs */
47 static memcached_st *yaz_memcached_wrap(const char *conf)
50 return memcached(conf, strlen(conf));
54 memcached_st *mc = memcached_create(0);
55 NMEM nmem = nmem_create();
56 memcached_return_t rc;
58 nmem_strsplit_blank(nmem, conf, &darray, &num);
59 for (i = 0; mc && i < num; i++)
61 if (!yaz_strncasecmp(darray[i], "--SERVER=", 9))
63 char *host = darray[i] + 9;
64 char *port = strchr(host, ':');
65 char *weight = strstr(host, "/?");
73 rc = memcached_server_add(mc, host, port ? atoi(port) : 11211);
74 yaz_log(YLOG_LOG, "memcached_server_add host=%s rc=%u %s",
75 host, (unsigned) rc, memcached_strerror(mc, rc));
76 if (rc != MEMCACHED_SUCCESS)
95 int ZOOM_memcached_configure(ZOOM_connection c)
98 #if HAVE_LIBMEMCACHED_MEMCACHED_H
101 memcached_free(c->mc_st);
105 val = ZOOM_options_get(c->options, "memcached");
108 #if HAVE_LIBMEMCACHED_MEMCACHED_H
109 c->mc_st = yaz_memcached_wrap(val);
112 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED,
113 "could not create memcached");
116 memcached_behavior_set(c->mc_st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
118 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
125 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
127 #if HAVE_LIBMEMCACHED_MEMCACHED_H
128 ZOOM_connection c = r->connection;
129 r->mc_key = wrbuf_alloc();
130 wrbuf_puts(r->mc_key, "0;");
131 wrbuf_puts(r->mc_key, c->host_port);
132 wrbuf_puts(r->mc_key, ";");
134 wrbuf_puts(r->mc_key, c->user);
135 wrbuf_puts(r->mc_key, ";");
137 wrbuf_puts(r->mc_key, c->group);
138 wrbuf_puts(r->mc_key, ";");
140 wrbuf_sha1_puts(r->mc_key, c->password, 1);
141 wrbuf_puts(r->mc_key, ";");
143 WRBUF w = wrbuf_alloc();
144 ZOOM_query_get_hash(q, w);
145 wrbuf_sha1_puts(r->mc_key, wrbuf_cstr(w), 1);
148 wrbuf_puts(r->mc_key, ";");
150 wrbuf_puts(r->mc_key, r->req_facets);
154 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
156 #if HAVE_LIBMEMCACHED_MEMCACHED_H
157 if (c->mc_st && resultset->live_set == 0)
161 memcached_return_t rc;
162 char *v = memcached_get(c->mc_st, wrbuf_buf(resultset->mc_key),
163 wrbuf_len(resultset->mc_key),
164 &v_len, &flags, &rc);
165 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
169 size_t lead_len = strlen(v) + 1;
171 resultset->size = odr_atoi(v);
173 yaz_log(YLOG_LOG, "For key %s got value %s lead_len=%d len=%d",
174 wrbuf_cstr(resultset->mc_key), v, (int) lead_len,
176 if (v_len > lead_len)
178 Z_OtherInformation *oi = 0;
179 int oi_len = v_len - lead_len;
180 odr_setbuf(resultset->odr, v + lead_len, oi_len, 0);
181 if (!z_OtherInformation(resultset->odr, &oi, 0, 0))
183 yaz_log(YLOG_WARN, "oi decoding failed");
187 ZOOM_handle_search_result(c, resultset, oi);
188 ZOOM_handle_facet_result(c, resultset, oi);
191 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
192 ZOOM_connection_put_event(c, event);
193 resultset->live_set = 1;
199 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset,
200 Z_OtherInformation *oi, const char *precision)
202 #if HAVE_LIBMEMCACHED_MEMCACHED_H
203 if (c->mc_st && resultset->live_set == 0)
206 memcached_return_t rc;
207 time_t expiration = 36000;
209 ODR odr = odr_createmem(ODR_ENCODE);
214 str = odr_malloc(odr, 20 + strlen(precision));
215 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
216 sprintf(str, ODR_INT_PRINTF ";%s", resultset->size, precision);
219 z_OtherInformation(odr, &oi, 0, 0);
220 oi_buf = odr_getbuf(odr, &oi_len, 0);
222 key = odr_malloc(odr, strlen(str) + 1 + oi_len);
225 memcpy(key + strlen(str) + 1, oi_buf, oi_len);
227 rc = memcached_set(c->mc_st,
228 wrbuf_buf(resultset->mc_key),
229 wrbuf_len(resultset->mc_key),
230 key, strlen(str) + 1 + oi_len, expiration, flags);
231 yaz_log(YLOG_LOG, "Store hit count key=%s value=%s oi_len=%d rc=%u %s",
232 wrbuf_cstr(resultset->mc_key), str, oi_len, (unsigned) rc,
233 memcached_strerror(c->mc_st, rc));
239 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
241 const char *syntax, const char *elementSetName,
243 Z_SRW_diagnostic *diag)
245 #if HAVE_LIBMEMCACHED_MEMCACHED_H
246 if (r->connection->mc_st &&
247 !diag && npr->which == Z_NamePlusRecord_databaseRecord)
249 WRBUF k = wrbuf_alloc();
250 WRBUF rec_sha1 = wrbuf_alloc();
252 memcached_return_t rc;
253 time_t expiration = 36000;
254 ODR odr = odr_createmem(ODR_ENCODE);
258 z_NamePlusRecord(odr, &npr, 0, 0);
259 rec_buf = odr_getbuf(odr, &rec_len, 0);
261 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
262 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
263 syntax ? syntax : "",
264 elementSetName ? elementSetName : "",
265 schema ? schema : "");
267 wrbuf_sha1_write(rec_sha1, rec_buf, rec_len, 1);
269 rc = memcached_set(r->connection->mc_st,
270 wrbuf_buf(k), wrbuf_len(k),
271 wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
274 yaz_log(YLOG_LOG, "Store record key=%s val=%s rc=%u %s",
275 wrbuf_cstr(k), wrbuf_cstr(rec_sha1), (unsigned) rc,
276 memcached_strerror(r->connection->mc_st, rc));
278 rc = memcached_add(r->connection->mc_st,
279 wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
283 yaz_log(YLOG_LOG, "Add record key=%s rec_len=%d rc=%u %s",
284 wrbuf_cstr(rec_sha1), rec_len, (unsigned) rc,
285 memcached_strerror(r->connection->mc_st, rc));
289 wrbuf_destroy(rec_sha1);
294 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
296 const char *elementSetName,
299 #if HAVE_LIBMEMCACHED_MEMCACHED_H
300 if (r->connection && r->connection->mc_st)
302 WRBUF k = wrbuf_alloc();
306 memcached_return_t rc;
308 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
309 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
310 syntax ? syntax : "",
311 elementSetName ? elementSetName : "",
312 schema ? schema : "");
314 yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
315 sha1_buf = memcached_get(r->connection->mc_st,
316 wrbuf_buf(k), wrbuf_len(k),
317 &sha1_len, &flags, &rc);
325 yaz_log(YLOG_LOG, "Lookup record %.*s", (int) sha1_len, sha1_buf);
326 v_buf = memcached_get(r->connection->mc_st, sha1_buf, sha1_len,
327 &v_len, &flags, &rc);
331 Z_NamePlusRecord *npr = 0;
333 odr_setbuf(r->odr, v_buf, v_len, 0);
334 z_NamePlusRecord(r->odr, &npr, 0, 0);
337 yaz_log(YLOG_LOG, "returned memcached copy");
349 * c-file-style: "Stroustrup"
350 * indent-tabs-mode: nil
352 * vim: shiftwidth=4 tabstop=8 expandtab