Spring集成memcache(二)xmemcache

xmemcache是国内开发的jar包,,与Spring集成也比较简单,步骤如下:

1.添加xmemcache.jar包至工程中;

2.在web.xml文件中添加配置

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/conf/spring/*-beans.xml
		</param-value>
	</context-param>

 

 

3.在属性文件中添加如下配置:

#memcache配置
memcache.ip=127.0.0.1
memcache.port=11211
memcache.pool.size=5

 

 

4.在/WEB-INF/conf/spring/目录下添加配置文件memcache-beans.xml

<?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:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
        http://www.springframework.org/schema/cache  
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"
	default-autowire="byName">

	<bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
		<constructor-arg>
			<list>
				<bean class="java.net.InetSocketAddress">
					<constructor-arg value="${memcache.ip}" />
					<constructor-arg value="${memcache.port}" />
				</bean>
			</list>
		</constructor-arg>
		<property name="connectionPoolSize" value="${memcache.pool.size}" />
		<property name="commandFactory">
			<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory" />
		</property>
		<property name="transcoder">
			<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
		</property>
	</bean>

	<bean id="memcachedClient" factory-bean="memcachedClientBuilder"
		factory-method="build" destroy-method="shutdown" />
</beans>

 

 

5.在Action中测试代码如下:

@RequestMapping(value = "/index")
	public void index(HttpServletRequest request, ModelMap model) throws TimeoutException, InterruptedException, MemcachedException{
		List<FmSupplier> list = supplierManager.getEnableSuppliers();
		memcachedClient.add("list", 0, list);
	}
	
	@RequestMapping(value = "/show")
	public void show(HttpServletRequest request, ModelMap model) throws TimeoutException, InterruptedException, MemcachedException{
		List<FmSupplier> listTemp = (List<FmSupplier>) memcachedClient.get("list");
		for(FmSupplier temp :listTemp)
			System.out.println(temp.getLinkman());
	}

 

以下是xmemcache.jar包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值