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);
59 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
66 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
68 #if HAVE_LIBMEMCACHED_MEMCACHED_H
69 ZOOM_connection c = r->connection;
70 r->mc_key = wrbuf_alloc();
71 wrbuf_puts(r->mc_key, "0;");
72 wrbuf_puts(r->mc_key, c->host_port);
73 wrbuf_puts(r->mc_key, ";");
75 wrbuf_puts(r->mc_key, c->user);
76 wrbuf_puts(r->mc_key, ";");
78 wrbuf_puts(r->mc_key, c->group);
79 wrbuf_puts(r->mc_key, ";");
81 wrbuf_sha1_puts(r->mc_key, c->password, 1);
82 wrbuf_puts(r->mc_key, ";");
83 wrbuf_sha1_puts(r->mc_key, ZOOM_query_get_query_string(q), 1);
84 wrbuf_puts(r->mc_key, ";");
88 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
90 #if HAVE_LIBMEMCACHED_MEMCACHED_H
91 /* TODO: add sorting */
92 if (c->mc_st && resultset->live_set == 0)
96 memcached_return_t rc;
97 char *v = memcached_get(c->mc_st, wrbuf_buf(resultset->mc_key),
98 wrbuf_len(resultset->mc_key),
103 WRBUF w = wrbuf_alloc();
105 wrbuf_write(w, v, v_len);
107 resultset->size = odr_atoi(wrbuf_cstr(w));
109 yaz_log(YLOG_LOG, "For key %s got value %s",
110 wrbuf_cstr(resultset->mc_key), wrbuf_cstr(w));
113 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
114 ZOOM_connection_put_event(c, event);
115 resultset->live_set = 1;
121 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset)
123 #if HAVE_LIBMEMCACHED_MEMCACHED_H
124 if (c->mc_st && resultset->live_set == 0)
127 memcached_return_t rc;
128 time_t expiration = 36000;
131 sprintf(str, ODR_INT_PRINTF, resultset->size);
132 rc = memcached_set(c->mc_st,
133 wrbuf_buf(resultset->mc_key),wrbuf_len(resultset->mc_key),
134 str, strlen(str), expiration, flags);
135 yaz_log(YLOG_LOG, "Store SRU hit count key=%s value=%s rc=%u %s",
136 wrbuf_cstr(resultset->mc_key), str, (unsigned) rc,
137 memcached_last_error_message(c->mc_st));
142 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
144 const char *syntax, const char *elementSetName,
146 Z_SRW_diagnostic *diag)
148 #if HAVE_LIBMEMCACHED_MEMCACHED_H
149 if (r->connection->mc_st &&
150 !diag && npr->which == Z_NamePlusRecord_databaseRecord)
152 WRBUF k = wrbuf_alloc();
154 memcached_return_t rc;
155 time_t expiration = 36000;
156 ODR odr = odr_createmem(ODR_ENCODE);
160 z_NamePlusRecord(odr, &npr, 0, 0);
161 rec_buf = odr_getbuf(odr, &rec_len, 0);
163 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
164 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
165 syntax ? syntax : "",
166 elementSetName ? elementSetName : "",
167 schema ? schema : "");
168 rc = memcached_set(r->connection->mc_st,
169 wrbuf_buf(k),wrbuf_len(k),
173 yaz_log(YLOG_LOG, "Store record lkey=%s len=%d rc=%u %s",
174 wrbuf_cstr(k), rec_len, (unsigned) rc,
175 memcached_last_error_message(r->connection->mc_st));
182 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
184 const char *elementSetName,
187 #if HAVE_LIBMEMCACHED_MEMCACHED_H
188 if (r->connection && r->connection->mc_st)
190 WRBUF k = wrbuf_alloc();
194 memcached_return_t rc;
196 wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
197 wrbuf_printf(k, ";%d;%s;%s;%s", pos,
198 syntax ? syntax : "",
199 elementSetName ? elementSetName : "",
200 schema ? schema : "");
202 yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
203 v_buf = memcached_get(r->connection->mc_st, wrbuf_buf(k), wrbuf_len(k),
204 &v_len, &flags, &rc);
208 Z_NamePlusRecord *npr = 0;
210 odr_setbuf(r->odr, v_buf, v_len, 0);
211 z_NamePlusRecord(r->odr, &npr, 0, 0);
214 yaz_log(YLOG_LOG, "returned memcached copy");
225 * c-file-style: "Stroustrup"
226 * indent-tabs-mode: nil
228 * vim: shiftwidth=4 tabstop=8 expandtab