From: Adam Dickmeiss Date: Mon, 20 Jan 2014 14:29:15 +0000 (+0100) Subject: ZOOM: initial experiments with memcached X-Git-Tag: v5.0.13~50 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=32790840d73b1f4a4bde1478e6b14699aac00e94;p=yaz-moved-to-github.git ZOOM: initial experiments with memcached Option memcached if set, enables memcached and the value is options for memcached, eg "--server localhost". Key for hit count is stored and retrieved in memcached. The value is just logged; no optimization WRT ZOOM yet. --- diff --git a/include/yaz/zoom.h b/include/yaz/zoom.h index 27ab47e..e460704 100644 --- a/include/yaz/zoom.h +++ b/include/yaz/zoom.h @@ -143,6 +143,7 @@ ZOOM_diag_str (int error); #define ZOOM_ERROR_ES_INVALID_ACTION 10015 #define ZOOM_ERROR_ES_INVALID_VERSION 10016 #define ZOOM_ERROR_ES_INVALID_SYNTAX 10017 +#define ZOOM_ERROR_MEMCACHED 10018 ZOOM_API(int) ZOOM_connection_last_event(ZOOM_connection cs); diff --git a/src/zoom-c.c b/src/zoom-c.c index 93fb070..f2363b6 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -308,6 +308,10 @@ ZOOM_API(ZOOM_connection) c->no_redirects = 0; c->cookies = 0; c->saveAPDU_wrbuf = 0; + +#if HAVE_LIBMEMCACHED_MEMCACHED_H + c->mc_st = 0; +#endif return c; } @@ -546,6 +550,29 @@ ZOOM_API(void) yaz_cookies_destroy(c->cookies); c->cookies = yaz_cookies_create(); +#if HAVE_LIBMEMCACHED_MEMCACHED_H + if (c->mc_st) + { + memcached_free(c->mc_st); + c->mc_st = 0; + } +#endif + val = ZOOM_options_get(c->options, "memcached"); + if (val && *val) + { +#if HAVE_LIBMEMCACHED_MEMCACHED_H + c->mc_st = memcached(val, strlen(val)); + if (!c->mc_st) + { + ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val); + return; + } +#else + ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled"); + return; +#endif + } + if (c->sru_mode == zoom_sru_error) { ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val); @@ -590,6 +617,11 @@ ZOOM_API(void) if (!c) return; yaz_log(c->log_api, "%p ZOOM_connection_destroy", c); + +#if HAVE_LIBMEMCACHED_MEMCACHED_H + if (c->mc_st) + memcached_free(c->mc_st); +#endif if (c->cs) cs_close(c->cs); @@ -694,6 +726,7 @@ ZOOM_resultset ZOOM_resultset_create(void) } #endif resultset_use(1); + r->mc_key = 0; return r; } @@ -714,11 +747,10 @@ ZOOM_API(ZOOM_resultset) ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) { ZOOM_resultset r = ZOOM_resultset_create(); - ZOOM_task task; const char *cp; + ZOOM_task task; int start, count; const char *syntax, *elementSetName, *schema; - yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q); r->r_sort_spec = ZOOM_query_get_sortspec(q); r->query = q; @@ -744,6 +776,31 @@ ZOOM_API(ZOOM_resultset) r->connection = c; r->next = c->resultsets; c->resultsets = r; + +#if HAVE_LIBMEMCACHED_MEMCACHED_H + r->mc_key = wrbuf_alloc(); + wrbuf_puts(r->mc_key, c->host_port); + wrbuf_puts(r->mc_key, ";"); + wrbuf_puts(r->mc_key, ZOOM_query_get_query_string(q)); + if (c->mc_st) + { + size_t v_len; + uint32_t flags; + memcached_return_t rc; + char *v = memcached_get(c->mc_st, wrbuf_buf(r->mc_key), + wrbuf_len(r->mc_key), &v_len, &flags, &rc); + if (v) + { + yaz_log(YLOG_LOG, "For key %s got value %.*s", + wrbuf_cstr(r->mc_key), (int) v_len, v); + } + else + { + yaz_log(YLOG_LOG, "For key %s got NO value", wrbuf_cstr(r->mc_key)); + } + } +#endif + if (c->host_port && c->proto == PROTO_HTTP) { if (!c->cs) @@ -868,6 +925,7 @@ static void resultset_destroy(ZOOM_resultset r) #if SHPTR YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy); #endif + wrbuf_destroy(r->mc_key); resultset_use(-1); xfree(r); } @@ -1892,6 +1950,8 @@ ZOOM_API(const char *) return "Extended Service. invalid version"; case ZOOM_ERROR_ES_INVALID_SYNTAX: return "Extended Service. invalid syntax"; + case ZOOM_ERROR_MEMCACHED: + return "Memcached"; default: return diagbib1_str(error); } diff --git a/src/zoom-p.h b/src/zoom-p.h index 77f39e9..a2cab30 100644 --- a/src/zoom-p.h +++ b/src/zoom-p.h @@ -36,6 +36,9 @@ #include #include #include +#if HAVE_LIBMEMCACHED_MEMCACHED_H +#include +#endif #define SHPTR 1 @@ -108,6 +111,9 @@ struct ZOOM_connection_p { int log_details; int log_api; WRBUF saveAPDU_wrbuf; +#if HAVE_LIBMEMCACHED_MEMCACHED_H + memcached_st *mc_st; +#endif }; typedef struct ZOOM_record_cache_p *ZOOM_record_cache; @@ -136,6 +142,7 @@ struct ZOOM_resultset_p { ZOOM_facet_field *facets; int num_facets; char **facets_names; + WRBUF mc_key; }; struct facet_term_p { diff --git a/src/zoom-z3950.c b/src/zoom-z3950.c index 80ed718..a16a809 100644 --- a/src/zoom-z3950.c +++ b/src/zoom-z3950.c @@ -1273,6 +1273,24 @@ static void handle_Z3950_search_response(ZOOM_connection c, handle_facet_result(c, resultset, sr->additionalSearchInfo); resultset->size = *sr->resultCount; + +#if HAVE_LIBMEMCACHED_MEMCACHED_H + if (c->mc_st) + { + uint32_t flags = 0; + memcached_return_t rc; + time_t expiration = 36000; + char str[40]; + + sprintf(str, ODR_INT_PRINTF, *sr->resultCount); + rc = memcached_set(c->mc_st, + wrbuf_buf(resultset->mc_key),wrbuf_len(resultset->mc_key), + str, strlen(str), expiration, flags); + yaz_log(YLOG_LOG, "Key=%s value=%s rc=%u %s", + wrbuf_cstr(resultset->mc_key), str, (unsigned) rc, + memcached_last_error_message(c->mc_st)); + } +#endif handle_Z3950_records(c, sr->records, 0); }