Introduction
XMemcached supports a easy way to integrate to spring framework since version 1.1.2
Simple Config
<bean name="memcachedClient"
class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
<property name="servers">
<value>host1:port1 host2:port2</value>
</property>
</bean>
Then,you can use memcachedClient in other beans.
Whole Config
<bean name="memcachedClient"
class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
<property name="servers">
<value>host1:port1 host2:port2 host3:port3</value>
</property>
<!-- server's weights -->
<property name="weights">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<!-- nio connection pool size -->
<property name="connectionPoolSize" value="2"></property>
<!-- Use binary protocol,default is TextCommandFactory -->
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean>
</property>
<!-- Distributed strategy -->
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</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"></bean>
</property>
</bean>Spring 3.0 and Builder config
If you want to use spring 3.0 with xmemcached,there is a exception like "Couldn't find a destroy method named 'shutdown' on bean", it occured beacause spring ioc container has changed the way to process when the destroy method is not found.So you can't use above configuration to intergrating to spring 3.0.
In fact,xmemcached has another way to intergrate to springframework through XMemcachedClientBuilder by factory-method.Here is an example:
<bean name="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<!-- XMemcachedClientBuilder have two arguments.First is server list,and second is weights array. -->
<constructor-arg>
<list>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<value>localhost</value>
</constructor-arg>
<constructor-arg>
<value>12000</value>
</constructor-arg>
</bean>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<value>localhost</value>
</constructor-arg>
<constructor-arg>
<value>12001</value>
</constructor-arg>
</bean>
</list>
</constructor-arg>
<constructor-arg>
<list>
<value>1</value>
<value>2</value>
</list>
</constructor-arg>
<property name="connectionPoolSize" value="2"></property>
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
</property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
</bean>
<!-- Use factory bean to build memcached client -->
<bean name="memcachedClient3" factory-bean="memcachedClientBuilder"
factory-method="build" destroy-method="shutdown"/>
每个服务器节点都用的是相同的配置吗
是的,一个client连多个节点,用的将是相同的配置,除了各自的权重可能不同。
如何设置binary?
将CommandFactory?设置为BinaryCommandFactory?即可。
Can you provide a overloaded constructor for the XMemcachedClientBuilder(String servers) with the string format like "server:port:weight" for example "localhost:11211:1"
With the XMemcachedClientBuilder(List<InetSocketAddress>), we are having hard time to increase or decrease the number of servers and their weightages, whenever we change the number of servers, the xml config needs per environment, where as the string argument will have only one entry in my spring property config file, so my config would be like below
<constructor-arg>${memcached.servers}</constructor-arg>
and in my .properties file it would have an entry like below memcached.servers=server1:11211:2 server2:11211:1
We are having hard time putting AddrUtil?.getAddresses static method called while the builder bean is constructed
@ chinnasamyk
Thanks for your suggestion, it is a good idea,i add it to issues,it would be implemented in 1.3.0
XMemcachedClientFactoryBean中没有定义shutdown方法,为什么要在 spring 的配置中写destroy-method="shutdown",不明白,麻烦解释一下,谢谢啊
@ ttspring@126.com
这是因为FactoryBean?只是生成一个bean供你使用,shutdown是作用在生成的bean上面,而非FactoryBean?。具体请看下spring reference.
<bean id="cacheManager" class="*********" destroy-method="shutdown" depends-on="memcachedClient"> spring context关闭时,想在xmemcached 关闭之前先把数据提交memcached,然后在关闭xmemcached。但是发现在memcachedClient shutdown之前, 关闭钩子已经在关闭session及报错:Xmemcached is stopped。这个问题有怎么好的解决方案吗? 源码中的关闭钩子,都去掉,会有什么样的影响吗?谢谢
如果抛出Xmemcached is stopped这样的错误,证明xmc已经被关闭了,关闭之前是不会抛出这样的错误。也就是说在关闭之后仍然有访问xmc的操作,从安全角度, xmc作为外部资源应当最后关闭吧,在系统逻辑部分关闭之后。
我是想cacheManager shutdown之后,在关闭memcachedClient,这样,我就可以在cacheManager shutdown的时候先把数据提交memcached了。但是在处理cacheManager shutdown时,发现memcachedClient 已经关闭了,报出Xmemcached is stopped。我发现这是因为xmemcached里有三个关闭钩子。我现在是把这三个关闭钩子去掉了,程序关闭的顺序就按照我想要的方式在关闭。我现在担心的是那三个关闭钩子会不会其它方面的影响?
可以在配置里关闭心跳检测吗?
While memcached doesn't have native support for named cache, any plan to support it at the API level in xmemcached?
Is adding Spring 3.1 Caching support in the roadmap?
if connectionPoolSize is more than 1, do I I have no synchronize all get/set calls? Or the client have built in synchronization?