added a memcache client and test
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 16 May 2007 19:30:03 +0000 (19:30 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 16 May 2007 19:30:03 +0000 (19:30 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@892 9efc2488-bf62-4759-914b-345cdb29e865

src/java/org/opensrf/test/TestCache.java [new file with mode: 0644]
src/java/org/opensrf/test/TestSettings.java
src/java/org/opensrf/util/Cache.java [new file with mode: 0644]

diff --git a/src/java/org/opensrf/test/TestCache.java b/src/java/org/opensrf/test/TestCache.java
new file mode 100644 (file)
index 0000000..1e47c32
--- /dev/null
@@ -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"));
+    }
+}
+
+
index d176acf..116bbe1 100644 (file)
@@ -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 (file)
index 0000000..5303688
--- /dev/null
@@ -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<String> serverList) {
+        initCache(serverList.toArray(new String[]{}));
+    }
+}
+