2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@gmail.com>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
16 #include <opensrf/osrf_cache.h>
18 #define MAX_KEY_LEN 250
20 static struct memcached_st* _osrfCache = NULL;
21 static time_t _osrfCacheMaxSeconds = -1;
22 static char* _clean_key( const char* );
24 int osrfCacheInit( const char* serverStrings[], int size, time_t maxCacheSeconds ) {
25 memcached_server_st *server_pool;
28 if( !(serverStrings && size > 0) ) return -1;
29 osrfCacheCleanup(); /* in case we've already been init-ed */
32 _osrfCache = memcached_create(NULL);
33 _osrfCacheMaxSeconds = maxCacheSeconds;
35 for( i = 0; i < size && serverStrings[i]; i++ ) {
36 /* TODO: modify caller to pass a list of servers all at once */
37 server_pool = memcached_servers_parse(serverStrings[i]);
38 rc = memcached_server_push(_osrfCache, server_pool);
39 if (rc != MEMCACHED_SUCCESS) {
40 osrfLogError(OSRF_LOG_MARK,
41 "Failed to add memcached server: %s - %s",
42 serverStrings[i], memcached_strerror(_osrfCache, rc));
49 int osrfCachePutObject( const char* key, const jsonObject* obj, time_t seconds ) {
50 if( !(key && obj) ) return -1;
51 char* s = jsonObjectToJSON( obj );
52 osrfLogInternal( OSRF_LOG_MARK, "osrfCachePut(): Putting object (key=%s): %s", key, s);
53 osrfCachePutString(key, s, seconds);
58 char* _clean_key( const char* key ) {
59 char* clean_key = (char*)strdup(key);
62 do while(isspace(*s) || iscntrl(*s)) s++; while(*d++ = *s++);
63 if (strlen(clean_key) > MAX_KEY_LEN) {
64 char *hashed = md5sum(clean_key);
66 strncat(clean_key, "shortened_", 11);
67 strncat(clean_key, hashed, MAX_KEY_LEN);
73 int osrfCachePutString( const char* key, const char* value, time_t seconds ) {
75 if( !(key && value) ) return -1;
76 seconds = (seconds <= 0 || seconds > _osrfCacheMaxSeconds) ? _osrfCacheMaxSeconds : seconds;
77 osrfLogInternal( OSRF_LOG_MARK, "osrfCachePutString(): Putting string (key=%s): %s", key, value);
79 char* clean_key = _clean_key( key );
81 /* add or overwrite existing key:value pair */
82 rc = memcached_set(_osrfCache, clean_key, strlen(clean_key), value, strlen(value), seconds, 0);
83 if (rc != MEMCACHED_SUCCESS) {
84 osrfLogError(OSRF_LOG_MARK, "Failed to cache key:value [%s]:[%s] - %s",
85 key, value, memcached_strerror(_osrfCache, rc));
92 jsonObject* osrfCacheGetObject( const char* key, ... ) {
96 jsonObject* obj = NULL;
98 VA_LIST_TO_STRING(key);
99 char* clean_key = _clean_key( VA_BUF );
100 const char* data = (const char*) memcached_get(_osrfCache, clean_key, strlen(clean_key), &val_len, &flags, &rc);
102 if (rc != MEMCACHED_SUCCESS) {
103 osrfLogDebug(OSRF_LOG_MARK, "Failed to get key [%s] - %s",
104 VA_BUF, memcached_strerror(_osrfCache, rc));
107 osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object (key=%s): %s", VA_BUF, data);
108 obj = jsonParse( data );
111 osrfLogDebug(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
116 char* osrfCacheGetString( const char* key, ... ) {
121 VA_LIST_TO_STRING(key);
122 char* clean_key = _clean_key( VA_BUF );
123 char* data = (char*) memcached_get(_osrfCache, clean_key, strlen(clean_key), &val_len, &flags, &rc);
125 if (rc != MEMCACHED_SUCCESS) {
126 osrfLogDebug(OSRF_LOG_MARK, "Failed to get key [%s] - %s",
127 VA_BUF, memcached_strerror(_osrfCache, rc));
129 osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetString(): Returning object (key=%s): %s", VA_BUF, data);
130 if(!data) osrfLogDebug(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
137 int osrfCacheRemove( const char* key, ... ) {
140 VA_LIST_TO_STRING(key);
141 rc = memcached_delete(_osrfCache, VA_BUF, strlen(VA_BUF), 0 );
142 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED) {
143 osrfLogDebug(OSRF_LOG_MARK, "Failed to delete key [%s] - %s",
144 VA_BUF, memcached_strerror(_osrfCache, rc));
152 int osrfCacheSetExpire( time_t seconds, const char* key, ... ) {
154 VA_LIST_TO_STRING(key);
155 jsonObject* o = osrfCacheGetObject( VA_BUF );
156 //osrfCacheRemove(VA_BUF);
157 int rc = osrfCachePutObject( VA_BUF, o, seconds );
164 void osrfCacheCleanup() {
166 memcached_free(_osrfCache);