标签:memcached xmemcached与spring集成
memcached作为高速运行的分布式缓存服务器,具有以下的特点。
存储方式
为了提高性能, memcached中保存的数据都存储在memcached内置的内存存储空间中。由于数据仅存在于内存中,因此重启memcached、重启操作系统会导致全部数据消失。
此外,内容容量达到指定值之后,就基于LRU(Least Recently Used)算法自动删除不使用的缓存。
memcached本身是为缓存而设计的服务器,因此并没有过多考虑数据的永久性问题。
Memcached
Client简要介绍
Memcached Client目前有3种:
XMemcached简单实现
MemcachedClientBuilder是MemcachedClient核心接口,用来控制Client的构建(build()方法)和关闭(shutdown()方法)。
XMemcachedClientBuilder一般通过构造方法配置地址列表,通常还要配置权重,代码如下:
<span style="font-size: 14px;"> </span><span style="font-size:14px;">public XMemcachedClientBuilder(List<InetSocketAddress> addressList) { if (addressList != null) { for (InetSocketAddress addr : addressList) { this.addressMap.put(addr, null); } } } public XMemcachedClientBuilder(List<InetSocketAddress> addressList, int[] weights) { if (addressList != null) { for (InetSocketAddress addr : addressList) { this.addressMap.put(addr, null); } } this.weights = weights; }</span>
<span style="font-size:14px;">import static junit.framework.Assert.*; import java.io.IOException; import java.util.concurrent.TimeoutException; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import net.rubyeye.xmemcached.command.BinaryCommandFactory; import net.rubyeye.xmemcached.exception.MemcachedException; import net.rubyeye.xmemcached.utils.AddrUtil; import org.junit.Test; public class MemcachedClientTest { @Test public void test() { MemcachedClientBuilder builder = new XMemcachedClientBuilder( AddrUtil.getAddresses("10.11.155.26:11211 10.11.155.41:11211 10.10.76.31:11211 10.10.76.35:11211"), new int[] { 1, 1, 1, 1 }); // 设置连接池大小,即客户端个数 builder.setConnectionPoolSize(50); // 宕机报警 builder.setFailureMode(true); // 使用二进制文件 builder.setCommandFactory(new BinaryCommandFactory()); MemcachedClient memcachedClient = null; try { memcachedClient = builder.build(); try { // 设置/获取 memcachedClient.set("zlex", 36000, "set/get"); assertEquals("set/get", memcachedClient.get("zlex")); // 替换 memcachedClient.replace("zlex", 36000, "replace"); assertEquals("replace", memcachedClient.get("zlex")); // 移除 memcachedClient.delete("zlex"); assertNull(memcachedClient.get("zlex")); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MemcachedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (memcachedClient != null) { try { memcachedClient.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }</span><span style="font-size: 14px;"> </span>
XMemcached与Spring集成可以参考http://code.google.com/p/xmemcached/wiki/Spring_Integration,这里只说最常用的方法。
memcached.properties做基本配置:
Properties代码:
#连接池大小即客户端个数 memcached.connectionPoolSize=50 memcached.failureMode=true #server1 memcached.server1.host=10.11.155.26 memcached.server1.port=11211 memcached.server1.weight=4 #server2 memcached.server2.host=10.11.155.41 memcached.server2.port=11211 memcached.server2.weight=3 #server3 memcached.server3.host=10.10.76.31 memcached.server3.port=11211 memcached.server3.weight=2 #server4 memcached.server4.host=10.10.76.35 memcached.server4.port=11211 memcached.server4.weight=1
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- http://code.google.com/p/xmemcached/wiki/Spring_Integration --> <context:property-placeholder location="memcached.properties" /> <bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder" p:connectionPoolSize="${memcached.connectionPoolSize}" p:failureMode="${memcached.failureMode}"> <!-- XMemcachedClientBuilder have two arguments.First is server list,and second is weights array. --> <constructor-arg> <list> <bean class="java.net.InetSocketAddress"> <constructor-arg> <value>${memcached.server1.host}</value> </constructor-arg> <constructor-arg> <value>${memcached.server1.port}</value> </constructor-arg> </bean> <bean class="java.net.InetSocketAddress"> <constructor-arg> <value>${memcached.server2.host}</value> </constructor-arg> <constructor-arg> <value>${memcached.server2.port}</value> </constructor-arg> </bean> <bean class="java.net.InetSocketAddress"> <constructor-arg> <value>${memcached.server3.host}</value> </constructor-arg> <constructor-arg> <value>${memcached.server3.port}</value> </constructor-arg> </bean> <bean class="java.net.InetSocketAddress"> <constructor-arg> <value>${memcached.server4.host}</value> </constructor-arg> <constructor-arg> <value>${memcached.server4.port}</value> </constructor-arg> </bean> </list> </constructor-arg> <constructor-arg> <list> <value>${memcached.server1.weight}</value> <value>${memcached.server2.weight}</value> <value>${memcached.server3.weight}</value> <value>${memcached.server4.weight}</value> </list> </constructor-arg> <property name="commandFactory"> <bean class="net.rubyeye.xmemcached.command.TextCommandFactory" /> </property> <property name="sessionLocator"> <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" /> </property> <property name="transcoder"> <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" /> </property> </bean> <!-- Use factory bean to build memcached client --> <bean id="memcachedClient" factory-bean="memcachedClientBuilder" factory-method="build" destroy-method="shutdown" /> </beans>
有了Spring容器支持,我们不需要在代码中进行配置,也不需要重复调用build()跟shutdown()方法,这些操作交给Spring来完成。
代码如下:
import static junit.framework.Assert.*; import java.util.concurrent.TimeoutException; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.exception.MemcachedException; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MemcachedSpringTest { private ApplicationContext app; private MemcachedClient memcachedClient; @Before public void init() { app = new ClassPathXmlApplicationContext("applicationContext.xml"); memcachedClient = (MemcachedClient) app.getBean("memcachedClient"); } @Test public void test() { try { // 设置/获取 memcachedClient.set("zlex", 36000, "set/get"); assertEquals("set/get", memcachedClient.get("zlex")); // 替换 memcachedClient.replace("zlex", 36000, "replace"); assertEquals("replace", memcachedClient.get("zlex")); // 移除 memcachedClient.delete("zlex"); assertNull(memcachedClient.get("zlex")); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MemcachedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
标签:memcached xmemcached与spring集成
原文地址:http://blog.csdn.net/u010923921/article/details/45459037