一、Memcache的客户端类型:
1.官方提供的基于传统阻塞io由Greg Whalin维护的客户端
https://github.com/gwhalin/Memcached-Java-Client/wiki
2.Dustin Sallings实现的基于java nio的Spymemcached
http://code.google.com/p/spymemcached/
3.XMemcached
http://code.google.com/p/xmemcached/,nio
4.各个客户端比较:
XMemcached在并发数增长的情况下有比较稳定的表现,Memcached-Java-Client小并发下性能最好,50并发以后出现问题,几乎不能支持高并发。Spymemchached有不稳定的因素,性能对比中比Xmemcached差一些;
因此选定了XMemcached,一般一个节点一个连接;对于多并发,还是可以使用多连接池来提高性能表现。
二、XMemcached的使用:
1. 常用:MemcachedClient.get(key); set(key, expirationTime, value); deleteWithNoReply(key); cas(key, expirationTime, value, version); GetsResponse<T> response = gets(key);2.对象序列化,可以提升效率,使用Hessian进行序列化。
- <bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
- <property name="servers">
<value>localhost:11211 localhost:12000 localhost:12001</value>
</property>
- <!-- server's weights
-->
- <property name="weights">
- <list>
<value>1</value>
<value>1</value>
<value>2</value>
</list>
</property>
- <!-- AuthInfo map,only valid on 1.2.5 or later version
-->
- <property name="authInfoMap">
<map />
</property>
- <!-- nio connection pool size
-->
<property name="connectionPoolSize" value="1" />
- <!-- when SAL should Use binary protocol,default is TextCommandFactory,otherwise must Text
-->
- <!-- property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean>
</property
-->
- <!-- Distributed strategy
-->
- <property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
</property>
- <!-- Serializing transcoder
-->
- <property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
- <!-- ByteBuffer allocator
-->
- <property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator" />
</property>
- <!-- Failure mode
-->
<property name="failureMode" value="false" />
</bean>
References:
http://xmemcached.googlecode.com/svn/trunk/benchmark/benchmark.html
http://www.splinedancer.com/memcached-win32/
http://wangqiaowqo.iteye.com/blog/1155732
附录:
Memcached vs Redis:
1.数据类型:Memcache可存储任何数据类型:二进制存储即可;Redis只能存储String、List<String>、Set<String>、Hash<String,String>
2.安全:新的Memcache可以认证、高并发有CAS机制;Redis没有CAS;
3.存储:Memcache只有内存,Redis可以存到磁盘;
4.性能上很多说法,说谁快的都有,实际测试数据调研中Redis快的比较多;
5.其他,使用经验上,memcached群体较多。
6.数据大小:1M,内存碎片
另外,还有有名的TT内存缓存。