From ee39f33d614c9da502e2ab4d5374d7d6a721ef5d Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 16 May 2007 19:30:03 +0000 Subject: [PATCH] added a memcache client and test git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@892 9efc2488-bf62-4759-914b-345cdb29e865 --- src/java/org/opensrf/test/TestCache.java | 27 ++++++++++++++++++++ src/java/org/opensrf/test/TestSettings.java | 2 -- src/java/org/opensrf/util/Cache.java | 38 +++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/java/org/opensrf/test/TestCache.java create mode 100644 src/java/org/opensrf/util/Cache.java diff --git a/src/java/org/opensrf/test/TestCache.java b/src/java/org/opensrf/test/TestCache.java new file mode 100644 index 0000000..1e47c32 --- /dev/null +++ b/src/java/org/opensrf/test/TestCache.java @@ -0,0 +1,27 @@ +package org.opensrf.test; +import org.opensrf.*; +import org.opensrf.util.*; +import java.util.List; +import java.util.ArrayList; + +public class TestCache { + public static void main(String args[]) throws Exception { + + /** + * args is a list of string like so: server:port server2:port server3:port ... + */ + + Cache.initCache(args); + Cache cache = new Cache(); + + cache.set("key1", "HI, MA!"); + cache.set("key2", "HI, MA! 2"); + cache.set("key3", "HI, MA! 3"); + + System.out.println("got key1 = " + (String) cache.get("key1")); + System.out.println("got key2 = " + (String) cache.get("key2")); + System.out.println("got key3 = " + (String) cache.get("key3")); + } +} + + diff --git a/src/java/org/opensrf/test/TestSettings.java b/src/java/org/opensrf/test/TestSettings.java index d176acf..116bbe1 100644 --- a/src/java/org/opensrf/test/TestSettings.java +++ b/src/java/org/opensrf/test/TestSettings.java @@ -6,8 +6,6 @@ public class TestSettings { public static void main(String args[]) throws Exception { Sys.bootstrapClient(args[0], "/config/opensrf"); SettingsClient client = SettingsClient.instance(); - //Object obj = client.get("/apps"); - //System.out.println(new JSONWriter(obj).write()); String lang = client.getString("/apps/opensrf.settings/language"); String impl = client.getString("/apps/opensrf.settings/implementation"); System.out.println("opensrf.settings language = " + lang); diff --git a/src/java/org/opensrf/util/Cache.java b/src/java/org/opensrf/util/Cache.java new file mode 100644 index 0000000..5303688 --- /dev/null +++ b/src/java/org/opensrf/util/Cache.java @@ -0,0 +1,38 @@ +package org.opensrf.util; +import com.danga.MemCached.*; +import java.util.List; + +/** + * Memcache client + */ +public class Cache extends MemCachedClient { + + public Cache() { + super(); + setCompressThreshold(4096); /* ?? */ + } + + /** + * Initializes the cache client + * @param serverList Array of server:port strings specifying the + * set of memcache servers this client will talk to + */ + public static void initCache(String[] serverList) { + SockIOPool pool = SockIOPool.getInstance(); + pool.setServers(serverList); + pool.initialize(); + com.danga.MemCached.Logger logger = + com.danga.MemCached.Logger.getLogger(MemCachedClient.class.getName()); + logger.setLevel(logger.LEVEL_ERROR); + } + + /** + * Initializes the cache client + * @param serverList List of server:port strings specifying the + * set of memcache servers this client will talk to + */ + public static void initCache(List serverList) { + initCache(serverList.toArray(new String[]{})); + } +} + -- 2.11.0