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 static struct memcache* _osrfCache = NULL;
19 static time_t _osrfCacheMaxSeconds = -1;
21 int osrfCacheInit( const char* serverStrings[], int size, time_t maxCacheSeconds ) {
22 if( !(serverStrings && size > 0) ) return -1;
23 osrfCacheCleanup(); /* in case we've already been init-ed */
26 _osrfCache = mc_new();
27 _osrfCacheMaxSeconds = maxCacheSeconds;
29 for( i = 0; i < size && serverStrings[i]; i++ )
30 mc_server_add4( _osrfCache, serverStrings[i] );
35 int osrfCachePutObject( char* key, const jsonObject* obj, time_t seconds ) {
36 if( !(key && obj) ) return -1;
37 char* s = jsonObjectToJSON( obj );
38 osrfLogInternal( OSRF_LOG_MARK, "osrfCachePut(): Putting object (key=%s): %s", key, s);
39 osrfCachePutString(key, s, seconds);
44 int osrfCachePutString( char* key, const char* value, time_t seconds ) {
45 if( !(key && value) ) return -1;
46 seconds = (seconds <= 0 || seconds > _osrfCacheMaxSeconds) ? _osrfCacheMaxSeconds : seconds;
47 osrfLogInternal( OSRF_LOG_MARK, "osrfCachePutString(): Putting string (key=%s): %s", key, value);
48 mc_set(_osrfCache, key, strlen(key), value, strlen(value), seconds, 0);
52 jsonObject* osrfCacheGetObject( const char* key, ... ) {
53 jsonObject* obj = NULL;
55 VA_LIST_TO_STRING(key);
56 const char* data = (const char*) mc_aget( _osrfCache, VA_BUF, strlen(VA_BUF) );
58 osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object (key=%s): %s", VA_BUF, data);
59 obj = jsonParseString( data );
62 osrfLogDebug(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
67 char* osrfCacheGetString( const char* key, ... ) {
69 VA_LIST_TO_STRING(key);
70 char* data = (char*) mc_aget(_osrfCache, VA_BUF, strlen(VA_BUF) );
71 osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetString(): Returning object (key=%s): %s", VA_BUF, data);
72 if(!data) osrfLogDebug(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
79 int osrfCacheRemove( const char* key, ... ) {
81 VA_LIST_TO_STRING(key);
82 return mc_delete(_osrfCache, VA_BUF, strlen(VA_BUF), 0 );
88 int osrfCacheSetExpire( time_t seconds, const char* key, ... ) {
90 VA_LIST_TO_STRING(key);
91 jsonObject* o = osrfCacheGetObject( VA_BUF );
92 //osrfCacheRemove(VA_BUF);
93 int rc = osrfCachePutObject( VA_BUF, o, seconds );
100 void osrfCacheCleanup() {