From 82e4edfa11ef163e5357ee95d3a435e9defce0c5 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 9 Sep 2005 03:07:24 +0000 Subject: [PATCH] added caching and cache initialize routine to the bootstrap call git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@533 9efc2488-bf62-4759-914b-345cdb29e865 --- src/Makefile | 4 ++- src/libstack/Makefile | 5 ++- src/libstack/opensrf.c | 3 +- src/libstack/osrf_cache.c | 73 +++++++++++++++++++++++++++++++++++++++++++ src/libstack/osrf_cache.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++ src/libstack/osrf_system.c | 34 ++++++++++++++++++++ src/libstack/osrf_system.h | 3 ++ 7 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 src/libstack/osrf_cache.c create mode 100644 src/libstack/osrf_cache.h diff --git a/src/Makefile b/src/Makefile index 6ff8ea7..e0fe5d9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -26,6 +26,7 @@ OPENSRF_TARGETS = libtransport/transport_session.o \ libstack/osrf_prefork.o \ libstack/osrf_system.o \ libstack/osrf_application.o \ + libstack/osrf_cache.o \ libstack/xml_utils.o \ utils/socket_bundle.o \ utils/string_array.o \ @@ -45,6 +46,7 @@ OPENSRF_HEADERS = libtransport/transport_session.h \ libstack/osrf_prefork.h \ libstack/osrf_system.h \ libstack/osrf_application.h \ + libstack/osrf_cache.h \ libstack/xml_utils.h \ utils/socket_bundle.h \ utils/string_array.h \ @@ -70,7 +72,7 @@ libopensrf.so: objson/libobjson.so @echo stack make -C libstack @echo $@ - $(CC) -shared -W1 $(LDFLAGS) -lobjson $(OPENSRF_TARGETS) -o $(TMPDIR)/$(LIBOPENSRF) + $(CC) -shared -W1 $(LDFLAGS) -lmemcache -lobjson $(OPENSRF_TARGETS) -o $(TMPDIR)/$(LIBOPENSRF) @echo apps make -C c-apps diff --git a/src/libstack/Makefile b/src/libstack/Makefile index f8887e5..2059027 100644 --- a/src/libstack/Makefile +++ b/src/libstack/Makefile @@ -1,6 +1,6 @@ CFLAGS += -DASSUME_STATELESS -rdynamic -fno-strict-aliasing -LDLIBS += -lxml2 -lobjson -ldl +LDLIBS += -lxml2 -lobjson -ldl -lmemcache TARGETS = osrf_message.o \ osrf_app_session.o \ @@ -10,6 +10,7 @@ TARGETS = osrf_message.o \ osrf_prefork.o \ osrfConfig.o \ osrf_application.o \ + osrf_cache.o \ xml_utils.o HEADERS = osrf_message.h \ @@ -20,6 +21,7 @@ HEADERS = osrf_message.h \ osrf_prefork.h \ osrfConfig.h \ osrf_application.h \ + osrf_cache.h \ xml_utils.h all: xml_utils.o $(TARGETS) copy @@ -40,6 +42,7 @@ osrf_settings.o: osrf_settings.c osrf_settings.h osrf_prefork.o: osrf_prefork.c osrf_prefork.h osrfConfig.o: osrfConfig.c osrfConfig.h xml_utils.o osrf_application.o: osrf_application.c osrf_application.h +osrf_cache.o: osrf_cache.c osrf_cache.h clean: /bin/rm -f *.o libopensrf_stack.so xml_utils.h xml_utils.c diff --git a/src/libstack/opensrf.c b/src/libstack/opensrf.c index f3ab1ba..403a3d6 100644 --- a/src/libstack/opensrf.c +++ b/src/libstack/opensrf.c @@ -1,10 +1,9 @@ #include "osrf_system.h" -#include "opensrf/utils.h" int main( int argc, char* argv[] ) { if( argc < 4 ) { - fprintf(stderr, "Host, Bootstrap, and context required\n"); + fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } diff --git a/src/libstack/osrf_cache.c b/src/libstack/osrf_cache.c new file mode 100644 index 0000000..2e77c40 --- /dev/null +++ b/src/libstack/osrf_cache.c @@ -0,0 +1,73 @@ +/* +Copyright (C) 2005 Georgia Public Library Service +Bill Erickson + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +*/ + +#include "osrf_cache.h" + +struct memcache* __osrfCache = NULL; +time_t __osrfCacheMaxSeconds = -1; + +int osrfCacheInit( char* serverStrings[], int size, time_t maxCacheSeconds ) { + if( !(serverStrings && size > 0) ) return -1; + + int i; + __osrfCache = mc_new(); + __osrfCacheMaxSeconds = maxCacheSeconds; + + for( i = 0; i < size && serverStrings[i]; i++ ) + mc_server_add4( __osrfCache, serverStrings[i] ); + + return 0; +} + +int osrfCachePutObject( char* key, const jsonObject* obj, time_t seconds ) { + if( !(key && obj) ) return -1; + char* s = jsonObjectToJSON( obj ); + if( seconds < 0 ) seconds = __osrfCacheMaxSeconds; + mc_add(__osrfCache, key, strlen(key), s, strlen(s), seconds, 0); + free(s); + return 0; +} + +int osrfCachePutString( char* key, const char* value, time_t seconds ) { + if( !(key && value) ) return -1; + if( seconds < 0 ) seconds = __osrfCacheMaxSeconds; + mc_add(__osrfCache, key, strlen(key), value, strlen(value), seconds, 0 ); + return 0; +} + +jsonObject* osrfCacheGetObject( char* key ) { + jsonObject* obj = NULL; + if( key ) { + char* data = (char*) mc_aget( __osrfCache, key, strlen(key) ); + if( data ) { + obj = jsonParseString( data ); + return obj; + } + } + return NULL; +} + +char* osrfCacheGetString( char* key ) { + if( key ) return (char*) mc_aget(__osrfCache, key, strlen(key) ); + return NULL; +} + + +int osrfCacheRemove( char* key ) { + if( key ) return mc_delete(__osrfCache, key, strlen(key), 0 ); + return -1; +} + + diff --git a/src/libstack/osrf_cache.h b/src/libstack/osrf_cache.h new file mode 100644 index 0000000..d81a6f0 --- /dev/null +++ b/src/libstack/osrf_cache.h @@ -0,0 +1,77 @@ +/* +Copyright (C) 2005 Georgia Public Library Service +Bill Erickson + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +*/ + + +#include "objson/object.h" +#include "objson/json_parser.h" +#include "memcache.h" + +/** + osrfCache is a globally shared cache API + */ + + +/** + Initialize the cache. + @param serverStrings An array of "ip:port" strings to use as cache servers + @param size The size of the serverStrings array + @param maxCacheSeconds The maximum amount of time an object / string may + be cached. Negative number means there is no limit + */ +int osrfCacheInit( char* serverStrings[], int size, time_t maxCacheSeconds ); + + +/** + Puts an object into the cache + @param key The cache key + @param obj The object to cache + @param seconds The amount of time to cache the data, negative number means + to cache up to 'maxCacheSeconds' as set by osrfCacheInit() + @return 0 on success, -1 on error + */ +int osrfCachePutObject( char* key, const jsonObject* obj, time_t seconds ); + +/** + Puts a string into the cache + @param key The cache key + @param value The string to cache + @param seconds The amount of time to cache the data, negative number means + to cache up to 'maxCacheSeconds' as set by osrfCacheInit() + @return 0 on success, -1 on error + */ +int osrfCachePutString( char* key, const char* value, time_t seconds); + +/** + Grabs an object from the cache. + @param key The cache key + @return The object (which must be freed) if it exists, otherwise returns NULL + */ +jsonObject* osrfCacheGetObject( char* key ); + +/** + Grabs a string from the cache. + @param key The cache key + @return The string (which must be freed) if it exists, otherwise returns NULL + */ +char* osrfCacheGetString( char* key ); + +/** + Removes the item with the given key from the cache. + @return 0 on success, -1 on error. + */ +int osrfCacheRemove( char* key ); + + + diff --git a/src/libstack/osrf_system.c b/src/libstack/osrf_system.c index a8609e7..0459209 100644 --- a/src/libstack/osrf_system.c +++ b/src/libstack/osrf_system.c @@ -24,6 +24,37 @@ int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* r } +int _osrfSystemInitCache() { + + jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server"); + char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time"); + + if( cacheServers && maxCache) { + + if( cacheServers->type == JSON_ARRAY ) { + int i; + char* servers[cacheServers->size]; + for( i = 0; i != cacheServers->size; i++ ) { + servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) ); + info_handler("Adding cache server %s", servers[i]); + } + osrfCacheInit( servers, cacheServers->size, atoi(maxCache) ); + + } else { + char* servers[] = { jsonObjectGetString(cacheServers) }; + info_handler("Adding cache server %s", servers[0]); + osrfCacheInit( servers, 1, atoi(maxCache) ); + } + + } else { + fatal_handler( "Missing config value for /cache/global/servers/server _or_ " + "/cache/global/max_cache_time"); + } + + return 0; +} + + int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) { if( !(configfile && contextNode) ) return -1; @@ -37,6 +68,9 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) { jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname"); osrfStringArray* arr = osrfNewStringArray(8); + + _osrfSystemInitCache(); + return 0; if(apps) { int i = 0; diff --git a/src/libstack/osrf_system.h b/src/libstack/osrf_system.h index 7200e14..14b5d16 100644 --- a/src/libstack/osrf_system.h +++ b/src/libstack/osrf_system.h @@ -6,6 +6,7 @@ #include "opensrf/logging.h" #include "osrf_settings.h" #include "osrfConfig.h" +#include "osrf_cache.h" /** Connects to jabber. Returns 1 on success, 0 on failure @@ -43,4 +44,6 @@ transport_client* osrf_system_get_transport_client(); int osrf_system_disconnect_client(); int osrf_system_shutdown(); +int _osrfSystemInitCache(); + #endif -- 2.11.0