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 \
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 \
@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
CFLAGS += -DASSUME_STATELESS -rdynamic -fno-strict-aliasing
-LDLIBS += -lxml2 -lobjson -ldl
+LDLIBS += -lxml2 -lobjson -ldl -lmemcache
TARGETS = osrf_message.o \
osrf_app_session.o \
osrf_prefork.o \
osrfConfig.o \
osrf_application.o \
+ osrf_cache.o \
xml_utils.o
HEADERS = osrf_message.h \
osrf_prefork.h \
osrfConfig.h \
osrf_application.h \
+ osrf_cache.h \
xml_utils.h
all: xml_utils.o $(TARGETS) copy
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
#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 <host> <bootstrap_config> <config_context>\n", argv[0]);
return 1;
}
--- /dev/null
+/*
+Copyright (C) 2005 Georgia Public Library Service
+Bill Erickson <highfalutin@gmail.com>
+
+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;
+}
+
+
--- /dev/null
+/*
+Copyright (C) 2005 Georgia Public Library Service
+Bill Erickson <highfalutin@gmail.com>
+
+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 );
+
+
+
}
+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;
jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
osrfStringArray* arr = osrfNewStringArray(8);
+
+ _osrfSystemInitCache();
+ return 0;
if(apps) {
int i = 0;
#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
int osrf_system_disconnect_client();
int osrf_system_shutdown();
+int _osrfSystemInitCache();
+
#endif