]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_cache.c
fc4d488d509aff7b26bad3f255b1b5928c59496e
[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 memcached_st* _osrfCache = NULL;
19 static time_t _osrfCacheMaxSeconds = -1;
20
21 int osrfCacheInit( const char* serverStrings[], int size, time_t maxCacheSeconds ) {
22         memcached_server_st *server_pool;
23         memcached_return rc;
24
25         if( !(serverStrings && size > 0) ) return -1;
26         osrfCacheCleanup(); /* in case we've already been init-ed */
27
28         int i;
29         _osrfCache = memcached_create(NULL);
30         _osrfCacheMaxSeconds = maxCacheSeconds;
31
32         for( i = 0; i < size && serverStrings[i]; i++ ) {
33                 /* TODO: modify caller to pass a list of servers all at once */
34                 server_pool = memcached_servers_parse(serverStrings[i]);
35                 rc = memcached_server_push(_osrfCache, server_pool);
36                 if (rc != MEMCACHED_SUCCESS) {
37                         osrfLogError(OSRF_LOG_MARK,
38                                 "Failed to add memcached server: %s - %s",
39                                 serverStrings[i], memcached_strerror(_osrfCache, rc));
40                 }
41         }
42
43         return 0;
44 }
45
46 int osrfCachePutObject( const char* key, const jsonObject* obj, time_t seconds ) {
47         if( !(key && obj) ) return -1;
48         char* s = jsonObjectToJSON( obj );
49         osrfLogInternal( OSRF_LOG_MARK, "osrfCachePut(): Putting object (key=%s): %s", key, s);
50         osrfCachePutString(key, s, seconds);
51         free(s);
52         return 0;
53 }
54
55 int osrfCachePutString( const char* key, const char* value, time_t seconds ) {
56         memcached_return rc;
57         if( !(key && value) ) return -1;
58         seconds = (seconds <= 0 || seconds > _osrfCacheMaxSeconds) ? _osrfCacheMaxSeconds : seconds;
59         osrfLogInternal( OSRF_LOG_MARK, "osrfCachePutString(): Putting string (key=%s): %s", key, value);
60         /* add or overwrite existing key:value pair */
61         rc = memcached_set(_osrfCache, key, strlen(key), value, strlen(value), seconds, 0);
62         if (rc != MEMCACHED_SUCCESS) {
63                 osrfLogError(OSRF_LOG_MARK, "Failed to cache key:value [%s]:[%s] - %s",
64                         key, value, memcached_strerror(_osrfCache, rc));
65         }
66         return 0;
67 }
68
69 jsonObject* osrfCacheGetObject( const char* key, ... ) {
70         size_t val_len;
71         uint32_t flags;
72         memcached_return rc;
73         jsonObject* obj = NULL;
74         if( key ) {
75                 VA_LIST_TO_STRING(key);
76                 const char* data = (const char*) memcached_get(_osrfCache, VA_BUF, strlen(VA_BUF), &val_len, &flags, &rc);
77                 if (rc != MEMCACHED_SUCCESS) {
78                         osrfLogDebug(OSRF_LOG_MARK, "Failed to get key [%s] - %s",
79                                 VA_BUF, memcached_strerror(_osrfCache, rc));
80                 }
81                 if( data ) {
82                         osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object (key=%s): %s", VA_BUF, data);
83                         obj = jsonParse( data );
84                         return obj;
85                 }
86                 osrfLogDebug(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
87         }
88         return NULL;
89 }
90
91 char* osrfCacheGetString( const char* key, ... ) {
92         size_t val_len;
93         uint32_t flags;
94         memcached_return rc;
95         if( key ) {
96                 VA_LIST_TO_STRING(key);
97                 char* data = (char*) memcached_get(_osrfCache, VA_BUF, strlen(VA_BUF), &val_len, &flags, &rc);
98                 if (rc != MEMCACHED_SUCCESS) {
99                         osrfLogDebug(OSRF_LOG_MARK, "Failed to get key [%s] - %s",
100                                 VA_BUF, memcached_strerror(_osrfCache, rc));
101                 }
102                 osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetString(): Returning object (key=%s): %s", VA_BUF, data);
103                 if(!data) osrfLogDebug(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
104                 return data;
105         }
106         return NULL;
107 }
108
109
110 int osrfCacheRemove( const char* key, ... ) {
111         memcached_return rc;
112         if( key ) {
113                 VA_LIST_TO_STRING(key);
114                 rc = memcached_delete(_osrfCache, VA_BUF, strlen(VA_BUF), 0 );
115                 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED) {
116                         osrfLogDebug(OSRF_LOG_MARK, "Failed to delete key [%s] - %s",
117                                 VA_BUF, memcached_strerror(_osrfCache, rc));
118                 }
119                 return 0;
120         }
121         return -1;
122 }
123
124
125 int osrfCacheSetExpire( time_t seconds, const char* key, ... ) {
126         if( key ) {
127                 VA_LIST_TO_STRING(key);
128                 jsonObject* o = osrfCacheGetObject( VA_BUF );
129                 //osrfCacheRemove(VA_BUF);
130                 int rc = osrfCachePutObject( VA_BUF, o, seconds );
131                 jsonObjectFree(o);
132                 return rc;
133         }
134         return -1;
135 }
136
137 void osrfCacheCleanup() {
138         if(_osrfCache) {
139                 memcached_free(_osrfCache);
140         }
141 }
142
143