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_MEMCACHED_RETURN_T
25 typedef memcached_return memcached_return_t;
28 void ZOOM_memcached_init(ZOOM_connection c)
30 #if HAVE_LIBMEMCACHED_MEMCACHED_H
35 void ZOOM_memcached_destroy(ZOOM_connection c)
37 #if HAVE_LIBMEMCACHED_MEMCACHED_H
39 memcached_free(c->mc_st);
43 int ZOOM_memcached_configure(ZOOM_connection c)
46 #if HAVE_LIBMEMCACHED_MEMCACHED_H
49 memcached_free(c->mc_st);
53 val = ZOOM_options_get(c->options, "memcached");
56 #if HAVE_LIBMEMCACHED_MEMCACHED_H
57 memcached_return_t rc;
59 c->mc_st = memcached_create(0);
60 rc = memcached_server_add(c->mc_st, val, 11211);
61 yaz_log(YLOG_LOG, "memcached_server_add host=%s rc=%u %s",
62 val, (unsigned) rc, memcached_strerror(c->mc_st, rc));
63 if (rc != MEMCACHED_SUCCESS)
65 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
66 memcached_free(c->mc_st);
70 memcached_behavior_set(c->mc_st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
72 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
79 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
81 #if HAVE_LIBMEMCACHED_MEMCACHED_H
82 ZOOM_connection c = r->connection;
83 r->mc_key = wrbuf_alloc();
84 wrbuf_puts(r->mc_key, "0;");
85 wrbuf_puts(r->mc_key, c->host_port);
86 wrbuf_puts(r->mc_key, ";");
88 wrbuf_puts(r->mc_key, c->user);
89 wrbuf_puts(r->mc_key, ";");
91 wrbuf_puts(r->mc_key, c->group);
92 wrbuf_puts(r->mc_key, ";");
94 wrbuf_sha1_puts(r->mc_key, c->password, 1);
95 wrbuf_puts(r->mc_key, ";");
97 WRBUF w = wrbuf_alloc();
98 ZOOM_query_get_hash(q, w);
99 wrbuf_sha1_puts(r->mc_key, wrbuf_cstr(w), 1);
102 wrbuf_puts(r->mc_key, ";");
104 wrbuf_puts(r->mc_key, r->req_facets);
108 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
110 #if HAVE_LIBMEMCACHED_MEMCACHED_H
111 if (c->mc_st && resultset->live_set == 0)
115 memcached_return_t rc;
116 char *v = memcached_get(c->mc_st, wrbuf_buf(resultset->mc_key),
117 wrbuf_len(resultset->mc_key),
118 &v_len, &flags, &rc);
119 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
123 size_t lead_len = strlen(v) + 1;
125 resultset->size = odr_atoi(v);
127 yaz_log(YLOG_LOG, "For key %s got value %s lead_len=%d len=%d",
128 wrbuf_cstr(resultset->mc_key), v, (int) lead_len,
130 if (v_len > lead_len)
132 Z_OtherInformation *oi = 0;
133 int oi_len = v_len - lead_len;
134 odr_setbuf(resultset->odr, v + lead_len, oi_len, 0);
135 if (!z_OtherInformation(resultset->odr, &oi, 0, 0))
137 yaz_log(YLOG_WARN, "oi decoding failed");
141 ZOOM_handle_search_result(c, resultset, oi);
142 ZOOM_handle_facet_result(c, resultset, oi);
145 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
146 ZOOM_connection_put_event(c, event);
147 resultset->live_set = 1;
153 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset,
154 Z_OtherInformation *oi, const char *precision)
156 #if HAVE_LIBMEMCACHED_MEMCACHED_H
157 if (c->mc_st && resultset->live_set == 0)
160 memcached_return_t rc;
161 time_t expiration = 36000;
163 ODR odr = odr_createmem(ODR_ENCODE);
168 str = odr_malloc(odr, 20 + strlen(precision));
169 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
170 sprintf(str, ODR_INT_PRINTF ";%s", resultset->size, precision);
173 z_OtherInformation(odr, &oi, 0, 0);
174 oi_buf = odr_getbuf(odr, &oi_len, 0);
176 key = odr_malloc(odr, strlen(str) + 1 + oi_len);
179 memcpy(key + strlen(str) + 1, oi_buf, oi_len);
181 rc = memcached_set(c->mc_st,
182 wrbuf_buf(resultset->mc_key),
183 wrbuf_len(resultset->mc_key),
184 key, strlen(str) + 1 + oi_len, expiration, flags);
185 yaz_log(YLOG_LOG, "Store hit count key=%s value=%s oi_len=%d rc=%u %s",
186 wrbuf_cstr(resultset->mc_key), str, oi_len, (unsigned) rc,
187 memcached_strerror(c->mc_st, rc));
193 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
195 const char *syntax, const char *elementSetName,
197 Z_SRW_diagnostic *diag)
199 #if HAVE_LIBMEMCACHED_MEMCACHED_H
200 if (r->connection->mc_st &&
201 !diag && npr->which == Z_NamePlusRecord_databaseRecord)
203 WRBUF k = wrbuf_alloc();
204 WRBUF rec_sha1 = wrbuf_alloc();
206 memcached_return_t rc;
207 time_t expiration = 36000;
208 ODR odr = odr_createmem(ODR_ENCODE);
212 z_NamePlusRecord(odr, &npr, 0, 0);
213 rec_buf = odr_getbuf(odr, &rec_len, 0);
215 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
216 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
217 syntax ? syntax : "",
218 elementSetName ? elementSetName : "",
219 schema ? schema : "");
221 wrbuf_sha1_write(rec_sha1, rec_buf, rec_len, 1);
223 rc = memcached_set(r->connection->mc_st,
224 wrbuf_buf(k), wrbuf_len(k),
225 wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
228 yaz_log(YLOG_LOG, "Store record key=%s val=%s rc=%u %s",
229 wrbuf_cstr(k), wrbuf_cstr(rec_sha1), (unsigned) rc,
230 memcached_strerror(r->connection->mc_st, rc));
232 rc = memcached_add(r->connection->mc_st,
233 wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
237 yaz_log(YLOG_LOG, "Add record key=%s rec_len=%d rc=%u %s",
238 wrbuf_cstr(rec_sha1), rec_len, (unsigned) rc,
239 memcached_strerror(r->connection->mc_st, rc));
243 wrbuf_destroy(rec_sha1);
248 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
250 const char *elementSetName,
253 #if HAVE_LIBMEMCACHED_MEMCACHED_H
254 if (r->connection && r->connection->mc_st)
256 WRBUF k = wrbuf_alloc();
260 memcached_return_t rc;
262 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
263 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
264 syntax ? syntax : "",
265 elementSetName ? elementSetName : "",
266 schema ? schema : "");
268 yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
269 sha1_buf = memcached_get(r->connection->mc_st,
270 wrbuf_buf(k), wrbuf_len(k),
271 &sha1_len, &flags, &rc);
279 yaz_log(YLOG_LOG, "Lookup record %.*s", (int) sha1_len, sha1_buf);
280 v_buf = memcached_get(r->connection->mc_st, sha1_buf, sha1_len,
281 &v_len, &flags, &rc);
285 Z_NamePlusRecord *npr = 0;
287 odr_setbuf(r->odr, v_buf, v_len, 0);
288 z_NamePlusRecord(r->odr, &npr, 0, 0);
291 yaz_log(YLOG_LOG, "returned memcached copy");
303 * c-file-style: "Stroustrup"
304 * indent-tabs-mode: nil
306 * vim: shiftwidth=4 tabstop=8 expandtab