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 void ZOOM_memcached_init(ZOOM_connection c)
25 #if HAVE_LIBMEMCACHED_MEMCACHED_H
30 void ZOOM_memcached_destroy(ZOOM_connection c)
32 #if HAVE_LIBMEMCACHED_MEMCACHED_H
34 memcached_free(c->mc_st);
38 int ZOOM_memcached_configure(ZOOM_connection c)
41 #if HAVE_LIBMEMCACHED_MEMCACHED_H
44 memcached_free(c->mc_st);
48 val = ZOOM_options_get(c->options, "memcached");
51 #if HAVE_LIBMEMCACHED_MEMCACHED_H
52 c->mc_st = memcached(val, strlen(val));
55 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
58 memcached_behavior_set(c->mc_st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
60 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
67 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
69 #if HAVE_LIBMEMCACHED_MEMCACHED_H
70 ZOOM_connection c = r->connection;
71 r->mc_key = wrbuf_alloc();
72 wrbuf_puts(r->mc_key, "0;");
73 wrbuf_puts(r->mc_key, c->host_port);
74 wrbuf_puts(r->mc_key, ";");
76 wrbuf_puts(r->mc_key, c->user);
77 wrbuf_puts(r->mc_key, ";");
79 wrbuf_puts(r->mc_key, c->group);
80 wrbuf_puts(r->mc_key, ";");
82 wrbuf_sha1_puts(r->mc_key, c->password, 1);
83 wrbuf_puts(r->mc_key, ";");
85 WRBUF w = wrbuf_alloc();
86 ZOOM_query_get_hash(q, w);
87 wrbuf_sha1_puts(r->mc_key, wrbuf_cstr(w), 1);
90 wrbuf_puts(r->mc_key, ";");
92 wrbuf_puts(r->mc_key, r->req_facets);
96 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
98 #if HAVE_LIBMEMCACHED_MEMCACHED_H
99 /* TODO: add sorting */
100 if (c->mc_st && resultset->live_set == 0)
104 memcached_return_t rc;
105 char *v = memcached_get(c->mc_st, wrbuf_buf(resultset->mc_key),
106 wrbuf_len(resultset->mc_key),
107 &v_len, &flags, &rc);
111 WRBUF w = wrbuf_alloc();
113 wrbuf_write(w, v, v_len);
115 resultset->size = odr_atoi(wrbuf_cstr(w));
117 yaz_log(YLOG_LOG, "For key %s got value %s",
118 wrbuf_cstr(resultset->mc_key), wrbuf_cstr(w));
121 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
122 ZOOM_connection_put_event(c, event);
123 resultset->live_set = 1;
129 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset)
131 #if HAVE_LIBMEMCACHED_MEMCACHED_H
132 if (c->mc_st && resultset->live_set == 0)
135 memcached_return_t rc;
136 time_t expiration = 36000;
139 sprintf(str, ODR_INT_PRINTF, resultset->size);
140 rc = memcached_set(c->mc_st,
141 wrbuf_buf(resultset->mc_key),wrbuf_len(resultset->mc_key),
142 str, strlen(str), expiration, flags);
143 yaz_log(YLOG_LOG, "Store hit count key=%s value=%s rc=%u %s",
144 wrbuf_cstr(resultset->mc_key), str, (unsigned) rc,
145 memcached_last_error_message(c->mc_st));
150 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
152 const char *syntax, const char *elementSetName,
154 Z_SRW_diagnostic *diag)
156 #if HAVE_LIBMEMCACHED_MEMCACHED_H
157 if (r->connection->mc_st &&
158 !diag && npr->which == Z_NamePlusRecord_databaseRecord)
160 WRBUF k = wrbuf_alloc();
162 memcached_return_t rc;
163 time_t expiration = 36000;
164 ODR odr = odr_createmem(ODR_ENCODE);
168 z_NamePlusRecord(odr, &npr, 0, 0);
169 rec_buf = odr_getbuf(odr, &rec_len, 0);
171 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
172 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
173 syntax ? syntax : "",
174 elementSetName ? elementSetName : "",
175 schema ? schema : "");
176 rc = memcached_set(r->connection->mc_st,
177 wrbuf_buf(k),wrbuf_len(k),
181 yaz_log(YLOG_LOG, "Store record lkey=%s len=%d rc=%u %s",
182 wrbuf_cstr(k), rec_len, (unsigned) rc,
183 memcached_last_error_message(r->connection->mc_st));
190 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
192 const char *elementSetName,
195 #if HAVE_LIBMEMCACHED_MEMCACHED_H
196 if (r->connection && r->connection->mc_st)
198 WRBUF k = wrbuf_alloc();
202 memcached_return_t rc;
204 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
205 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
206 syntax ? syntax : "",
207 elementSetName ? elementSetName : "",
208 schema ? schema : "");
210 yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
211 v_buf = memcached_get(r->connection->mc_st, wrbuf_buf(k), wrbuf_len(k),
212 &v_len, &flags, &rc);
216 Z_NamePlusRecord *npr = 0;
218 odr_setbuf(r->odr, v_buf, v_len, 0);
219 z_NamePlusRecord(r->odr, &npr, 0, 0);
222 yaz_log(YLOG_LOG, "returned memcached copy");
233 * c-file-style: "Stroustrup"
234 * indent-tabs-mode: nil
236 * vim: shiftwidth=4 tabstop=8 expandtab