2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@gmail.com>
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.
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.
16 #include <opensrf/osrf_cache.h>
18 static struct memcached_st* _osrfCache = NULL;
19 static time_t _osrfCacheMaxSeconds = -1;
21 int osrfCacheInit( const char* serverStrings[], int size, time_t maxCacheSeconds ) {
22 memcached_server_st *server_pool;
25 if( !(serverStrings && size > 0) ) return -1;
26 osrfCacheCleanup(); /* in case we've already been init-ed */
29 _osrfCache = memcached_create(NULL);
30 _osrfCacheMaxSeconds = maxCacheSeconds;
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));
46 int osrfCachePutObject( 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);
55 int osrfCachePutString( char* key, const char* value, time_t seconds ) {
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));
69 jsonObject* osrfCacheGetObject( const char* key, ... ) {
73 jsonObject* obj = NULL;
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));
82 osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object (key=%s): %s", VA_BUF, data);
83 obj = jsonParse( data );
86 osrfLogDebug(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
91 char* osrfCacheGetString( const char* 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));
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);
110 int osrfCacheRemove( const char* 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));
125 int osrfCacheSetExpire( time_t seconds, const char* 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 );
137 void osrfCacheCleanup() {
139 memcached_free(_osrfCache);