]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_cache.c
Fix a bug whereby, if there was only one <service> entry for the
[OpenSRF.git] / src / libopensrf / osrf_cache.c
1 /*
2 Copyright (C) 2005  Georgia Public Library Service 
3 Bill Erickson <highfalutin@gmail.com>
4
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.
9
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.
14 */
15
16 #include <opensrf/osrf_cache.h>
17
18 static struct memcache* _osrfCache = NULL;
19 static time_t _osrfCacheMaxSeconds = -1;
20
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 */
24
25         int i;
26         _osrfCache = mc_new();
27         _osrfCacheMaxSeconds = maxCacheSeconds;
28
29         for( i = 0; i < size && serverStrings[i]; i++ ) 
30                 mc_server_add4( _osrfCache, serverStrings[i] );
31
32         return 0;
33 }
34
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);
40         free(s);
41         return 0;
42 }
43
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);
49         return 0;
50 }
51
52 jsonObject* osrfCacheGetObject( const char* key, ... ) {
53         jsonObject* obj = NULL;
54         if( key ) {
55                 VA_LIST_TO_STRING(key);
56                 const char* data = (const char*) mc_aget( _osrfCache, VA_BUF, strlen(VA_BUF) );
57                 if( data ) {
58                         osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object (key=%s): %s", VA_BUF, data);
59                         obj = jsonParseString( data );
60                         return obj;
61                 }
62                 osrfLogDebug(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
63         }
64         return NULL;
65 }
66
67 char* osrfCacheGetString( const char* key, ... ) {
68         if( 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);
73                 return data;
74         }
75         return NULL;
76 }
77
78
79 int osrfCacheRemove( const char* key, ... ) {
80         if( key ) {
81                 VA_LIST_TO_STRING(key);
82                 return mc_delete(_osrfCache, VA_BUF, strlen(VA_BUF), 0 );
83         }
84         return -1;
85 }
86
87
88 int osrfCacheSetExpire( time_t seconds, const char* key, ... ) {
89         if( 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 );
94                 jsonObjectFree(o);
95                 return rc;
96         }
97         return -1;
98 }
99
100 void osrfCacheCleanup() {
101     if(_osrfCache)
102         mc_free(_osrfCache);
103 }
104
105