Excellent work. We are using memcached with our J2EE webapps. It is a very simple way to cache stuff. This example code shows how to use a single memcached server, but you can actually use a pool of servers!!!
Here is a simple junit testcase that shows how to use memcached from Java. You will need to download the java memcached client from:
http://www.whalin.com/memcached/
---------------------------------------------------
import junit.framework.TestCase;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class TestMemCached extends TestCase {
public void testStoreAndRetrieveFromCache() {
// this is the address that is displayed when you start up the hermes appliance
String[] cacheServers = { "10.10.16.100:11211" };
// create a pool of memcached servers with the name hermes
SockIOPool pool = SockIOPool.getInstance("hermes");
// set the list of servers used by the pool
pool.setServers(cacheServers);
// initialize the pool
pool.initialize();
// create a new memcached client which uses the above pool
MemCachedClient mc = new MemCachedClient();
mc.setPoolName("hermes");
// turn off storing compressed data
mc.setCompressEnable(false);
// add data to the cache
mc.set("testcachedata", new StringBuffer("Testing hermes virtual appliance"));
// get the data that you just added to the cache
StringBuffer o = (StringBuffer) mc.get("testcachedata");
// assert that you actually got it

assertEquals("Testing hermes virtual appliance", o.toString());
}
}
-------------------------------------------------------------
+5
--marco
Message was edited by:
marco_inzaghi