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 int ZOOM_memcached_configure(ZOOM_connection c)
48 #if HAVE_LIBMEMCACHED_MEMCACHED_H
51 memcached_free(c->mc_st);
55 val = ZOOM_options_get(c->options, "memcached");
58 #if HAVE_LIBMEMCACHED_MEMCACHED_H
59 memcached_return_t rc;
61 c->mc_st = memcached_create(0);
62 rc = memcached_server_add(c->mc_st, val, 11211);
63 yaz_log(YLOG_LOG, "memcached_server_add host=%s rc=%u %s",
64 val, (unsigned) rc, memcached_strerror(c->mc_st, rc));
65 if (rc != MEMCACHED_SUCCESS)
67 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
68 memcached_free(c->mc_st);
72 memcached_behavior_set(c->mc_st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
74 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
81 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
83 #if HAVE_LIBMEMCACHED_MEMCACHED_H
84 ZOOM_connection c = r->connection;
85 r->mc_key = wrbuf_alloc();
86 wrbuf_puts(r->mc_key, "0;");
87 wrbuf_puts(r->mc_key, c->host_port);
88 wrbuf_puts(r->mc_key, ";");
90 wrbuf_puts(r->mc_key, c->user);
91 wrbuf_puts(r->mc_key, ";");
93 wrbuf_puts(r->mc_key, c->group);
94 wrbuf_puts(r->mc_key, ";");
96 wrbuf_sha1_puts(r->mc_key, c->password, 1);
97 wrbuf_puts(r->mc_key, ";");
99 WRBUF w = wrbuf_alloc();
100 ZOOM_query_get_hash(q, w);
101 wrbuf_sha1_puts(r->mc_key, wrbuf_cstr(w), 1);
104 wrbuf_puts(r->mc_key, ";");
106 wrbuf_puts(r->mc_key, r->req_facets);
110 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
112 #if HAVE_LIBMEMCACHED_MEMCACHED_H
113 if (c->mc_st && resultset->live_set == 0)
117 memcached_return_t rc;
118 char *v = memcached_get(c->mc_st, wrbuf_buf(resultset->mc_key),
119 wrbuf_len(resultset->mc_key),
120 &v_len, &flags, &rc);
121 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
125 size_t lead_len = strlen(v) + 1;
127 resultset->size = odr_atoi(v);
129 yaz_log(YLOG_LOG, "For key %s got value %s lead_len=%d len=%d",
130 wrbuf_cstr(resultset->mc_key), v, (int) lead_len,
132 if (v_len > lead_len)
134 Z_OtherInformation *oi = 0;
135 int oi_len = v_len - lead_len;
136 odr_setbuf(resultset->odr, v + lead_len, oi_len, 0);
137 if (!z_OtherInformation(resultset->odr, &oi, 0, 0))
139 yaz_log(YLOG_WARN, "oi decoding failed");
143 ZOOM_handle_search_result(c, resultset, oi);
144 ZOOM_handle_facet_result(c, resultset, oi);
147 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
148 ZOOM_connection_put_event(c, event);
149 resultset->live_set = 1;
155 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset,
156 Z_OtherInformation *oi, const char *precision)
158 #if HAVE_LIBMEMCACHED_MEMCACHED_H
159 if (c->mc_st && resultset->live_set == 0)
162 memcached_return_t rc;
163 time_t expiration = 36000;
165 ODR odr = odr_createmem(ODR_ENCODE);
170 str = odr_malloc(odr, 20 + strlen(precision));
171 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
172 sprintf(str, ODR_INT_PRINTF ";%s", resultset->size, precision);
175 z_OtherInformation(odr, &oi, 0, 0);
176 oi_buf = odr_getbuf(odr, &oi_len, 0);
178 key = odr_malloc(odr, strlen(str) + 1 + oi_len);
181 memcpy(key + strlen(str) + 1, oi_buf, oi_len);
183 rc = memcached_set(c->mc_st,
184 wrbuf_buf(resultset->mc_key),
185 wrbuf_len(resultset->mc_key),
186 key, strlen(str) + 1 + oi_len, expiration, flags);
187 yaz_log(YLOG_LOG, "Store hit count key=%s value=%s oi_len=%d rc=%u %s",
188 wrbuf_cstr(resultset->mc_key), str, oi_len, (unsigned) rc,
189 memcached_strerror(c->mc_st, rc));
195 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
197 const char *syntax, const char *elementSetName,
199 Z_SRW_diagnostic *diag)
201 #if HAVE_LIBMEMCACHED_MEMCACHED_H
202 if (r->connection->mc_st &&
203 !diag && npr->which == Z_NamePlusRecord_databaseRecord)
205 WRBUF k = wrbuf_alloc();
206 WRBUF rec_sha1 = wrbuf_alloc();
208 memcached_return_t rc;
209 time_t expiration = 36000;
210 ODR odr = odr_createmem(ODR_ENCODE);
214 z_NamePlusRecord(odr, &npr, 0, 0);
215 rec_buf = odr_getbuf(odr, &rec_len, 0);
217 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
218 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
219 syntax ? syntax : "",
220 elementSetName ? elementSetName : "",
221 schema ? schema : "");
223 wrbuf_sha1_write(rec_sha1, rec_buf, rec_len, 1);
225 rc = memcached_set(r->connection->mc_st,
226 wrbuf_buf(k), wrbuf_len(k),
227 wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
230 yaz_log(YLOG_LOG, "Store record key=%s val=%s rc=%u %s",
231 wrbuf_cstr(k), wrbuf_cstr(rec_sha1), (unsigned) rc,
232 memcached_strerror(r->connection->mc_st, rc));
234 rc = memcached_add(r->connection->mc_st,
235 wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
239 yaz_log(YLOG_LOG, "Add record key=%s rec_len=%d rc=%u %s",
240 wrbuf_cstr(rec_sha1), rec_len, (unsigned) rc,
241 memcached_strerror(r->connection->mc_st, rc));
245 wrbuf_destroy(rec_sha1);
250 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
252 const char *elementSetName,
255 #if HAVE_LIBMEMCACHED_MEMCACHED_H
256 if (r->connection && r->connection->mc_st)
258 WRBUF k = wrbuf_alloc();
262 memcached_return_t rc;
264 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
265 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
266 syntax ? syntax : "",
267 elementSetName ? elementSetName : "",
268 schema ? schema : "");
270 yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
271 sha1_buf = memcached_get(r->connection->mc_st,
272 wrbuf_buf(k), wrbuf_len(k),
273 &sha1_len, &flags, &rc);
281 yaz_log(YLOG_LOG, "Lookup record %.*s", (int) sha1_len, sha1_buf);
282 v_buf = memcached_get(r->connection->mc_st, sha1_buf, sha1_len,
283 &v_len, &flags, &rc);
287 Z_NamePlusRecord *npr = 0;
289 odr_setbuf(r->odr, v_buf, v_len, 0);
290 z_NamePlusRecord(r->odr, &npr, 0, 0);
293 yaz_log(YLOG_LOG, "returned memcached copy");
305 * c-file-style: "Stroustrup"
306 * indent-tabs-mode: nil
308 * vim: shiftwidth=4 tabstop=8 expandtab