luotuo 2013-02-21
Memcached 客户端程序
Memcached的java客户端已经存在三种了:
一、官方提供的基于传统阻塞io由Greg Whalin维护的客户端 memcached client for java
二、Dustin Sallings实现的基于java nio的Spymemcached spymemcached
三、XMemcached xmemcached
三种API比较
1 ) memcached client for java
较早推出的memcached JAVA客户端API,应用广泛,运行比较稳定。
2) spymemcached
A simple, asynchronous, single-threaded memcached client written in java. 支持异步,单线程的 memcached客户端,用到了java1.5版本的concurrent和nio,存取速度会高于前者,但是稳定性不好,测试中常 报timeOut等相关异常。
3) xmemcached
XMemcached同样是基于java nio的客户端,java nio相比于传统阻塞io模型来说,有效率高(特别在高并发下)和资源耗费相对较少的优点。传统阻塞IO为了提高效率,需要创建一定数量的连接形成连接 池,而nio仅需要一个连接即可(当然,nio也是可以做池化处理),相对来说减少了线程创建和切换的开销,这一点在高并发下特别明显。因此 XMemcached与Spymemcached在性能都非常优秀,在某些方面(存储的数据比较小的情况下)Xmemcached比 Spymemcached的表现更为优秀,具体可以看这个Java Memcached Clients Benchmark。
Memcached java client
Memcached java client是官方推荐的最早的memcached java客户端。最新版本:java_memcached-release_2.6.1。
官方下载地址:https://github.com/gwhalin/Memcached-Java-Client
采用阻塞式SOCKET通讯,据说目前版本进行了很多优化,性能有所提高(只看过1.5的源代码,还没来及看最新的)
提供key方式的连接池,默认连接池key为default。(老版本了)。2.6.1版本支持apache-commoms-pool作为连接池。
支持权重配置。
后期的版本提增加了cas支持和getMutl功能
import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; import com.schooner.MemCached.MemcachedItem; public class MemcachedForJavaExample { // create a static client as most installs only need // a single instance protected static MemCachedClient mcc = new MemCachedClient(); // set up connection pool once at class load static { // server list and weights String[] servers = { "localhost:11211", "localhost:11212", "localhost:11213" }; Integer[] weights = { 3, 3, 2 }; // grab an instance of our connection pool SockIOPool pool = SockIOPool.getInstance(); // set the servers and the weights pool.setServers(servers); pool.setWeights(weights); pool.setHashingAlg(SockIOPool.CONSISTENT_HASH); // set some basic pool settings // 5 initial, 5 min, and 250 max conns // and set the max idle time for a conn // to 6 hours pool.setInitConn(5); pool.setMinConn(5); pool.setMaxConn(250); pool.setMaxIdle(1000 * 60 * 60 * 6); // set the sleep for the maint thread // it will wake up every x seconds and // maintain the pool size pool.setMaintSleep(30); // set some TCP settings // disable nagle // set the read timeout to 3 secs // and don't set a connect timeout pool.setNagle(false); pool.setSocketTO(3000); pool.setSocketConnectTO(0); // initialize the connection pool pool.initialize(); } public static void main(String[] args) { System.out.println("SET: " + mcc.set("key1", "value1")); System.out.println("SET: " + mcc.set("key2", "value2")); System.out.println("SET: " + mcc.set("key3", "value3")); System.out.println("GET: " + mcc.get("key1")); MemcachedItem item = mcc.gets("key1"); System.out.println("GETS: value=" + item.getValue() + ",CasUnique:"+item.getCasUnique()); System.out.println("SET: " + mcc.set("key1", "value1_1")); System.out.println("CAS: " + mcc.cas("key1", "value1_2", item.getCasUnique())); //必须FALSE System.out.println("getMulti:" + mcc.getMulti(new String[]{"key1","key2","key3"})); } }