Memcached Java客户端2.6.1发布
Memcached是被广泛使用的分布式缓存技术。不同的语言有不同的Memcached客户端程序,对于Java客户端来说,首推Memcached Java Client(http://github.com/gwhalin/Memcached-Java-Client)。
这次,Memcached Java Client推出的2.6.1发布版是基于全新的performance分支,具有如下重大改进:
- 较之老版本,在性能上有300%左右的提升;兼容老版本,用户无须修改自己的源代码;支持多个memcached协议,包括text,udp和binary协议;支持SASL认证机制;重新实现的连接池,修复了之前的连接数太多所导致的OutOfMemory异常;加入了slf4j logger支持,使得开发人员可以方便的记录日志;支持自定义的对象序列化方法。
这个分支由Schooner Information Technology贡献,并由Schooner中国团队完成开发,开发者是:
- 王新根,new.root@gmail.com;李蒙,jowett.lee@gmail.com,IT eye博客地址是:http://menglee.iteye.com
可以从这里下载二进制包:https://github.com/gwhalin/Memcached-Java-Client/downloads
源代码在github上,http://github.com/gwhalin/Memcached-Java-Client,然后选择performance分支。
下面是一些性能测试的数据,包括了当前流行的Memcached Java Client。其中,schooner指的是这个分支的text protocol, schooner_bin指的是binary protocol。




?
com.danga.MemCached.MemCachedClient Tue Aug 16 11:17:56 CST 2011 - ++++ exception thrown while trying to get object from cache for key: test117
com.danga.MemCached.MemCachedClient Tue Aug 16 11:17:56 CST 2011 - invalid stream header: 76616C75
java.io.StreamCorruptedException: invalid stream header: 76616C75
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at com.schooner.MemCached.ObjectTransCoder.decode(ObjectTransCoder.java:57)
at com.schooner.MemCached.AscIIClient.get(AscIIClient.java:761)
at com.schooner.MemCached.AscIIClient.get(AscIIClient.java:612)
at com.schooner.MemCached.AscIIClient.get(AscIIClient.java:608)
at com.danga.MemCached.MemCachedClient.get(MemCachedClient.java:1047)
at TestM.main(TestM.java:56)
操作时通过设置可分布式环境的要求变量 client。setPrimitiveAsString(true),和 pool.setHashingAlg(2);还请楼主帮忙看一下是怎么回事?
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);
// 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);
pool.setBufferSize(30000000);
// initialize the connection pool
pool.initialize();
// lets set some compression on for the client
// compress anything larger than 64k
//mcc.setCompressEnable(true);
//mcc.setCompressThreshold(64 * 1024);
mcc = new MemCachedClient(true);
}
public static void main(String args[]){
int k=1;
String[] values= new String[8000];
int j = 0;
for (int i = 0; i < keys.length; i++) {
keys[j] = "3";
j++;
}
BlockingQueue<Runnable> startStopQueue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor taskExecutor = new ThreadPoolExecutor(10,
10, 10, TimeUnit.SECONDS,
startStopQueue);
taskExecutor.allowCoreThreadTimeOut(true);
final String[] vs=values;
final MemCachedClient mc = mcc;
while(k>0){
//mcc.delete("foo");
taskExecutor.submit(new Runnable(){
@Override
public void run() {
//mcc = new MemCachedClient(true);
String key = "foo"+UUID.randomUUID();
mcc.set(key, vs);
Object obj = mcc.get(key);
System.out.println(obj);
}
});
try {
TimeUnit.MICROSECONDS.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
k--;
}
taskExecutor.shutdown();
while(!taskExecutor.isTerminated()){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.exit(1);
}
}
服务端日志:
<30 new auto-negotiating client connection
30: Client using the binary protocol
<30 Read binary protocol data:
<30 0x80 0x01 0x00 0x27
<30 0x08 0x00 0x00 0x00
<30 0x00 0x00 0x62 0x02
<30 0x00 0x00 0x00 0x00
<30 0x00 0x00 0x00 0x00
<30 0x00 0x00 0x00 0x00
<30 SET foo5eb1b171-68e6-4d82-af89-ba9fed2f368e Value len is 25043
Failed to read, and not due to blocking:
errno: 0 Error 0
rcurr=57ccac4a ritem=5a4272d5 rbuf=57cca2a0 rlbytes=22640 rsize=4096
<30 connection closed. 10 楼 sankby 2012-04-13 为什么我用binary比ascii的慢一倍,需要怎么设置?有测试过的吗 11 楼 287854442 2012-08-06 请问有没有2.6.3版本的api文档?? 12 楼 bukebuhao 2012-09-05 Memcached Java Client2.5.2,线上出现java.lang.OutOfMemoryError
at sun.misc.Unsafe.allocateMemory(Native Method)
at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:126)
at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:306)
at com.schooner.MemCached.SchoonerSockIOPool$TCPSockIO.<init>(Unknown Source)。
最新的2.6.1已修复这个bug,不过,我本地模拟大并发量,为何2.5.2也不出现内存溢出。
能不能给我一段测试代码,重现bug,好确认是不是这个问题。谢谢!!配置参数最好给个参考实例