Tuesday, January 10, 2012

Memcached Java Client Simple Usage

1. Download library from here.
2. Import java_memcached-release_2.6.3.jar into your project.
3. Simple code for testing:

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemCacheClient {
static{
String[] serverList = {"localhost:11211"};
Integer[] weight = {3};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverList); 
pool.setWeights(weight);
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000*60*60*6);
pool.initialize();
}
public static void main (String args[]){
MemCachedClient mcc = new MemCachedClient();
mcc.set("testA", "AA");
mcc.set("testB", "BB");
System.out.println(mcc.get("testA"));
System.out.println(mcc.get("testB"));
mcc.delete("testA");
System.out.println(mcc.get("testA"));
}
}


No comments:

Post a Comment