]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_cache.c
Now, when no children are available to pass a request to, the parent
[Evergreen.git] / OpenSRF / src / libstack / 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 "osrf_cache.h"
17
18 struct memcache* __osrfCache = NULL;
19 time_t __osrfCacheMaxSeconds = -1;
20
21 int osrfCacheInit( 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         if( seconds < 0 ) seconds = __osrfCacheMaxSeconds;
39
40         mc_set(__osrfCache, key, strlen(key), s, strlen(s), seconds, 0);
41         free(s);
42         return 0;
43 }
44
45 int osrfCachePutString( char* key, const char* value, time_t seconds ) {
46         if( !(key && value) ) return -1;
47         if( seconds < 0 ) seconds = __osrfCacheMaxSeconds;
48         osrfLogInternal( OSRF_LOG_MARK, "osrfCachePutString(): Putting string: %s", value);
49         mc_set(__osrfCache, key, strlen(key), value, strlen(value), seconds, 0);
50         return 0;
51 }
52
53 jsonObject* osrfCacheGetObject( char* key, ... ) {
54         jsonObject* obj = NULL;
55         if( key ) {
56                 VA_LIST_TO_STRING(key);
57                 char* data = (char*) mc_aget( __osrfCache, VA_BUF, strlen(VA_BUF) );
58                 if( data ) {
59                         osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data);
60                         obj = jsonParseString( data );
61                         return obj;
62                 }
63                 osrfLogWarning(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
64         }
65         return NULL;
66 }
67
68 char* osrfCacheGetString( char* key, ... ) {
69         if( key ) {
70                 VA_LIST_TO_STRING(key);
71                 char* data = (char*) mc_aget(__osrfCache, VA_BUF, strlen(VA_BUF) );
72                 osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data);
73                 if(!data) osrfLogWarning(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
74                 return data;
75         }
76         return NULL;
77 }
78
79
80 int osrfCacheRemove( char* key, ... ) {
81         if( key ) {
82                 VA_LIST_TO_STRING(key);
83                 return mc_delete(__osrfCache, VA_BUF, strlen(VA_BUF), 0 );
84         }
85         return -1;
86 }
87
88
89 int osrfCacheSetExpire( time_t seconds, char* key, ... ) {
90         if( key ) {
91                 VA_LIST_TO_STRING(key);
92                 jsonObject* o = osrfCacheGetObject( VA_BUF );
93                 //osrfCacheRemove(VA_BUF);
94                 return osrfCachePutObject( VA_BUF, o, seconds );
95         }
96         return -1;
97 }
98
99