]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_cache.c
df5199a3d0299fa3df31c92834d50ff197307385
[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         if( seconds < 0 ) seconds = __osrfCacheMaxSeconds;
38
39         mc_set(__osrfCache, key, strlen(key), s, strlen(s), seconds, 0);
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         if( seconds < 0 ) seconds = __osrfCacheMaxSeconds;
47         mc_set(__osrfCache, key, strlen(key), value, strlen(value), seconds, 0);
48         return 0;
49 }
50
51 jsonObject* osrfCacheGetObject( 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                         obj = jsonParseString( data );
58                         return obj;
59                 }
60         }
61         return NULL;
62 }
63
64 char* osrfCacheGetString( char* key, ... ) {
65         if( key ) {
66                 VA_LIST_TO_STRING(key);
67                 return (char*) mc_aget(__osrfCache, VA_BUF, strlen(VA_BUF) );
68         }
69         return NULL;
70 }
71
72
73 int osrfCacheRemove( char* key, ... ) {
74         if( key ) {
75                 VA_LIST_TO_STRING(key);
76                 return mc_delete(__osrfCache, VA_BUF, strlen(VA_BUF), 0 );
77         }
78         return -1;
79 }
80
81