]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_cache.c
Bootstrap setuptools, drop unnecessary directory metadata
[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
24         int i;
25         _osrfCache = mc_new();
26         _osrfCacheMaxSeconds = maxCacheSeconds;
27
28         for( i = 0; i < size && serverStrings[i]; i++ ) 
29                 mc_server_add4( _osrfCache, serverStrings[i] );
30
31         return 0;
32 }
33
34 int osrfCachePutObject( char* key, const jsonObject* obj, time_t seconds ) {
35         if( !(key && obj) ) return -1;
36         char* s = jsonObjectToJSON( obj );
37         osrfLogInternal( OSRF_LOG_MARK, "osrfCachePut(): Putting object: %s", s);
38     osrfCachePutString(key, s, seconds);
39         free(s);
40         return 0;
41 }
42
43 int osrfCachePutString( char* key, const char* value, time_t seconds ) {
44         if( !(key && value) ) return -1;
45     seconds = (seconds <= 0 || seconds > _osrfCacheMaxSeconds) ? _osrfCacheMaxSeconds : seconds;
46         osrfLogInternal( OSRF_LOG_MARK, "osrfCachePutString(): Putting string: %s", value);
47         mc_set(_osrfCache, key, strlen(key), value, strlen(value), seconds, 0);
48         return 0;
49 }
50
51 jsonObject* osrfCacheGetObject( const char* key, ... ) {
52         jsonObject* obj = NULL;
53         if( key ) {
54                 VA_LIST_TO_STRING(key);
55                 char* data = (char*) mc_aget( _osrfCache, VA_BUF, strlen(VA_BUF) );
56                 if( data ) {
57                         osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data);
58                         obj = jsonParseString( data );
59                         return obj;
60                 }
61                 osrfLogWarning(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
62         }
63         return NULL;
64 }
65
66 char* osrfCacheGetString( const char* key, ... ) {
67         if( key ) {
68                 VA_LIST_TO_STRING(key);
69                 char* data = (char*) mc_aget(_osrfCache, VA_BUF, strlen(VA_BUF) );
70                 osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data);
71                 if(!data) osrfLogWarning(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
72                 return data;
73         }
74         return NULL;
75 }
76
77
78 int osrfCacheRemove( const char* key, ... ) {
79         if( key ) {
80                 VA_LIST_TO_STRING(key);
81                 return mc_delete(_osrfCache, VA_BUF, strlen(VA_BUF), 0 );
82         }
83         return -1;
84 }
85
86
87 int osrfCacheSetExpire( time_t seconds, const char* key, ... ) {
88         if( key ) {
89                 VA_LIST_TO_STRING(key);
90                 jsonObject* o = osrfCacheGetObject( VA_BUF );
91                 //osrfCacheRemove(VA_BUF);
92                 return osrfCachePutObject( VA_BUF, o, seconds );
93         }
94         return -1;
95 }
96
97 void osrfCacheCleanup() {
98     if(_osrfCache)
99         mc_free(_osrfCache);
100 }
101
102