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
40 void ZOOM_memcached_destroy(ZOOM_connection c)
42 #if HAVE_LIBMEMCACHED_MEMCACHED_H
44 memcached_free(c->mc_st);
48 redisFree(c->redis_c);
52 #if HAVE_LIBMEMCACHED_MEMCACHED_H
53 /* memcached wrapper.. Because memcached function do not exist in older libs */
54 static memcached_st *yaz_memcached_wrap(const char *conf)
56 #if HAVE_MEMCACHED_FUNC
57 return memcached(conf, strlen(conf));
61 memcached_st *mc = memcached_create(0);
62 NMEM nmem = nmem_create();
63 memcached_return_t rc;
65 nmem_strsplit_blank(nmem, conf, &darray, &num);
66 for (i = 0; mc && i < num; i++)
68 if (!yaz_strncasecmp(darray[i], "--SERVER=", 9))
70 char *host = darray[i] + 9;
71 char *port = strchr(host, ':');
72 char *weight = strstr(host, "/?");
80 rc = memcached_server_add(mc, host, port ? atoi(port) : 11211);
81 yaz_log(YLOG_LOG, "memcached_server_add host=%s rc=%u %s",
82 host, (unsigned) rc, memcached_strerror(mc, rc));
83 if (rc != MEMCACHED_SUCCESS)
103 static redisContext *create_redis(const char *conf)
107 NMEM nmem = nmem_create();
108 redisContext *context = 0;
110 nmem_strsplit_blank(nmem, conf, &darray, &num);
111 for (i = 0; i < num; i++)
113 if (!yaz_strncasecmp(darray[i], "--SERVER=", 9))
115 struct timeval timeout = { 1, 500000 }; /* 1.5 seconds */
116 char *host = darray[i] + 9;
117 char *port = strchr(host, ':');
120 context = redisConnectWithTimeout(host,
121 port ? atoi(port) : 6379,
130 int ZOOM_memcached_configure(ZOOM_connection c)
136 redisFree(c->redis_c);
140 #if HAVE_LIBMEMCACHED_MEMCACHED_H
143 memcached_free(c->mc_st);
148 val = ZOOM_options_get(c->options, "redis");
152 c->redis_c = create_redis(val);
153 if (c->redis_c == 0 || c->redis_c->err)
155 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED,
156 "could not create redis");
159 return 0; /* don't bother with memcached if redis is enabled */
161 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
165 val = ZOOM_options_get(c->options, "memcached");
168 #if HAVE_LIBMEMCACHED_MEMCACHED_H
169 c->mc_st = yaz_memcached_wrap(val);
172 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED,
173 "could not create memcached");
176 memcached_behavior_set(c->mc_st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
178 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
186 static void wrbuf_vary_puts(WRBUF w, const char *v)
192 wrbuf_sha1_puts(w, v, 1);
202 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
205 ZOOM_connection c = r->connection;
207 r->mc_key = wrbuf_alloc();
208 wrbuf_puts(r->mc_key, "1;");
209 wrbuf_vary_puts(r->mc_key, c->host_port);
210 wrbuf_puts(r->mc_key, ";");
211 wrbuf_vary_puts(r->mc_key, ZOOM_resultset_option_get(r, "extraArgs"));
212 wrbuf_puts(r->mc_key, ";");
213 wrbuf_vary_puts(r->mc_key, c->user);
214 wrbuf_puts(r->mc_key, ";");
215 wrbuf_vary_puts(r->mc_key, c->group);
216 wrbuf_puts(r->mc_key, ";");
218 wrbuf_sha1_puts(r->mc_key, c->password, 1);
219 wrbuf_puts(r->mc_key, ";");
221 WRBUF w = wrbuf_alloc();
222 ZOOM_query_get_hash(q, w);
223 wrbuf_sha1_puts(r->mc_key, wrbuf_cstr(w), 1);
226 wrbuf_puts(r->mc_key, ";");
227 wrbuf_vary_puts(r->mc_key, r->req_facets);
231 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
234 if (c->redis_c && resultset->live_set == 0)
240 argv[1] = wrbuf_cstr(resultset->mc_key);
242 reply = redisCommandArgv(c->redis_c, 2, argv, 0);
243 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
244 if (reply && reply->type == REDIS_REPLY_STRING)
246 char *v = reply->str;
247 int v_len = reply->len;
249 size_t lead_len = strlen(v) + 1;
251 resultset->size = odr_atoi(v);
253 yaz_log(YLOG_LOG, "For key %s got value %s lead_len=%d len=%d",
254 wrbuf_cstr(resultset->mc_key), v, (int) lead_len,
256 if (v_len > lead_len)
258 Z_OtherInformation *oi = 0;
259 int oi_len = v_len - lead_len;
260 odr_setbuf(resultset->odr, v + lead_len, oi_len, 0);
261 if (!z_OtherInformation(resultset->odr, &oi, 0, 0))
263 yaz_log(YLOG_WARN, "oi decoding failed");
264 freeReplyObject(reply);
267 ZOOM_handle_search_result(c, resultset, oi);
268 ZOOM_handle_facet_result(c, resultset, oi);
270 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
271 ZOOM_connection_put_event(c, event);
272 resultset->live_set = 1;
274 freeReplyObject(reply);
277 #if HAVE_LIBMEMCACHED_MEMCACHED_H
278 if (c->mc_st && resultset->live_set == 0)
282 memcached_return_t rc;
283 char *v = memcached_get(c->mc_st, wrbuf_buf(resultset->mc_key),
284 wrbuf_len(resultset->mc_key),
285 &v_len, &flags, &rc);
286 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
290 size_t lead_len = strlen(v) + 1;
292 resultset->size = odr_atoi(v);
294 yaz_log(YLOG_LOG, "For key %s got value %s lead_len=%d len=%d",
295 wrbuf_cstr(resultset->mc_key), v, (int) lead_len,
297 if (v_len > lead_len)
299 Z_OtherInformation *oi = 0;
300 int oi_len = v_len - lead_len;
301 odr_setbuf(resultset->odr, v + lead_len, oi_len, 0);
302 if (!z_OtherInformation(resultset->odr, &oi, 0, 0))
304 yaz_log(YLOG_WARN, "oi decoding failed");
308 ZOOM_handle_search_result(c, resultset, oi);
309 ZOOM_handle_facet_result(c, resultset, oi);
312 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
313 ZOOM_connection_put_event(c, event);
314 resultset->live_set = 1;
320 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset,
321 Z_OtherInformation *oi, const char *precision)
324 if (c->redis_c && resultset->live_set == 0)
327 ODR odr = odr_createmem(ODR_ENCODE);
332 str = odr_malloc(odr, 20 + strlen(precision));
333 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
334 sprintf(str, ODR_INT_PRINTF ";%s", resultset->size, precision);
337 z_OtherInformation(odr, &oi, 0, 0);
338 oi_buf = odr_getbuf(odr, &oi_len, 0);
340 key = odr_malloc(odr, strlen(str) + 1 + oi_len);
343 memcpy(key + strlen(str) + 1, oi_buf, oi_len);
351 argv[1] = wrbuf_buf(resultset->mc_key);
352 argvlen[1] = wrbuf_len(resultset->mc_key);
354 argvlen[2] = strlen(str) + 1 + oi_len;
355 reply = redisCommandArgv(c->redis_c, 3, argv, argvlen);
356 freeReplyObject(reply);
361 #if HAVE_LIBMEMCACHED_MEMCACHED_H
362 if (c->mc_st && resultset->live_set == 0)
365 memcached_return_t rc;
366 time_t expiration = 36000;
368 ODR odr = odr_createmem(ODR_ENCODE);
373 str = odr_malloc(odr, 20 + strlen(precision));
374 /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
375 sprintf(str, ODR_INT_PRINTF ";%s", resultset->size, precision);
378 z_OtherInformation(odr, &oi, 0, 0);
379 oi_buf = odr_getbuf(odr, &oi_len, 0);
381 key = odr_malloc(odr, strlen(str) + 1 + oi_len);
384 memcpy(key + strlen(str) + 1, oi_buf, oi_len);
386 rc = memcached_set(c->mc_st,
387 wrbuf_buf(resultset->mc_key),
388 wrbuf_len(resultset->mc_key),
389 key, strlen(str) + 1 + oi_len, expiration, flags);
390 yaz_log(YLOG_LOG, "Store hit count key=%s value=%s oi_len=%d rc=%u %s",
391 wrbuf_cstr(resultset->mc_key), str, oi_len, (unsigned) rc,
392 memcached_strerror(c->mc_st, rc));
398 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
400 const char *syntax, const char *elementSetName,
402 Z_SRW_diagnostic *diag)
405 if (r->connection->redis_c &&
406 !diag && npr->which == Z_NamePlusRecord_databaseRecord)
408 WRBUF k = wrbuf_alloc();
409 WRBUF rec_sha1 = wrbuf_alloc();
410 ODR odr = odr_createmem(ODR_ENCODE);
417 z_NamePlusRecord(odr, &npr, 0, 0);
418 rec_buf = odr_getbuf(odr, &rec_len, 0);
420 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
421 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
422 syntax ? syntax : "",
423 elementSetName ? elementSetName : "",
424 schema ? schema : "");
426 wrbuf_sha1_write(rec_sha1, rec_buf, rec_len, 1);
430 argv[1] = wrbuf_buf(k);
431 argvlen[1] = wrbuf_len(k);
432 argv[2] = wrbuf_buf(rec_sha1);
433 argvlen[2] = wrbuf_len(rec_sha1);
435 reply = redisCommandArgv(r->connection->redis_c, 3, argv, argvlen);
436 yaz_log(YLOG_LOG, "Store record key=%s val=%s",
437 wrbuf_cstr(k), wrbuf_cstr(rec_sha1));
438 freeReplyObject(reply);
440 argv[1] = wrbuf_buf(rec_sha1);
441 argvlen[1] = wrbuf_len(rec_sha1);
443 argvlen[2] = rec_len;
445 reply = redisCommandArgv(r->connection->redis_c, 3, argv, argvlen);
446 yaz_log(YLOG_LOG, "Add record key=%s rec_len=%d",
447 wrbuf_cstr(rec_sha1), rec_len);
448 freeReplyObject(reply);
452 wrbuf_destroy(rec_sha1);
455 #if HAVE_LIBMEMCACHED_MEMCACHED_H
456 if (r->connection->mc_st &&
457 !diag && npr->which == Z_NamePlusRecord_databaseRecord)
459 WRBUF k = wrbuf_alloc();
460 WRBUF rec_sha1 = wrbuf_alloc();
462 memcached_return_t rc;
463 time_t expiration = 36000;
464 ODR odr = odr_createmem(ODR_ENCODE);
468 z_NamePlusRecord(odr, &npr, 0, 0);
469 rec_buf = odr_getbuf(odr, &rec_len, 0);
471 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
472 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
473 syntax ? syntax : "",
474 elementSetName ? elementSetName : "",
475 schema ? schema : "");
477 wrbuf_sha1_write(rec_sha1, rec_buf, rec_len, 1);
479 rc = memcached_set(r->connection->mc_st,
480 wrbuf_buf(k), wrbuf_len(k),
481 wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
484 yaz_log(YLOG_LOG, "Store record key=%s val=%s rc=%u %s",
485 wrbuf_cstr(k), wrbuf_cstr(rec_sha1), (unsigned) rc,
486 memcached_strerror(r->connection->mc_st, rc));
488 rc = memcached_add(r->connection->mc_st,
489 wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
493 yaz_log(YLOG_LOG, "Add record key=%s rec_len=%d rc=%u %s",
494 wrbuf_cstr(rec_sha1), rec_len, (unsigned) rc,
495 memcached_strerror(r->connection->mc_st, rc));
499 wrbuf_destroy(rec_sha1);
504 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
506 const char *elementSetName,
510 if (r->connection && r->connection->redis_c)
512 WRBUF k = wrbuf_alloc();
517 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
518 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
519 syntax ? syntax : "",
520 elementSetName ? elementSetName : "",
521 schema ? schema : "");
523 yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
526 argv[1] = wrbuf_buf(k);
527 argvlen[1] = wrbuf_len(k);
528 reply1 = redisCommandArgv(r->connection->redis_c, 2, argv, argvlen);
531 if (reply1 && reply1->type == REDIS_REPLY_STRING)
534 char *sha1_buf = reply1->str;
535 int sha1_len = reply1->len;
537 yaz_log(YLOG_LOG, "Lookup record %.*s", (int) sha1_len, sha1_buf);
542 argvlen[1] = sha1_len;
544 reply2 = redisCommandArgv(r->connection->redis_c, 2, argv, argvlen);
545 if (reply2 && reply2->type == REDIS_REPLY_STRING)
547 Z_NamePlusRecord *npr = 0;
548 char *v_buf = reply2->str;
549 int v_len = reply2->len;
551 odr_setbuf(r->odr, v_buf, v_len, 0);
552 z_NamePlusRecord(r->odr, &npr, 0, 0);
554 yaz_log(YLOG_LOG, "returned redis copy");
555 freeReplyObject(reply2);
556 freeReplyObject(reply1);
559 freeReplyObject(reply2);
561 freeReplyObject(reply1);
564 #if HAVE_LIBMEMCACHED_MEMCACHED_H
565 if (r->connection && r->connection->mc_st)
567 WRBUF k = wrbuf_alloc();
571 memcached_return_t rc;
573 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
574 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
575 syntax ? syntax : "",
576 elementSetName ? elementSetName : "",
577 schema ? schema : "");
579 yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
580 sha1_buf = memcached_get(r->connection->mc_st,
581 wrbuf_buf(k), wrbuf_len(k),
582 &sha1_len, &flags, &rc);
590 yaz_log(YLOG_LOG, "Lookup record %.*s", (int) sha1_len, sha1_buf);
591 v_buf = memcached_get(r->connection->mc_st, sha1_buf, sha1_len,
592 &v_len, &flags, &rc);
596 Z_NamePlusRecord *npr = 0;
598 odr_setbuf(r->odr, v_buf, v_len, 0);
599 z_NamePlusRecord(r->odr, &npr, 0, 0);
602 yaz_log(YLOG_LOG, "returned memcached copy");
614 * c-file-style: "Stroustrup"
615 * indent-tabs-mode: nil
617 * vim: shiftwidth=4 tabstop=8 expandtab