---
title: "Spring集成memcache（二）xmemcache"
author: "tom_seed"
date: "2014-08-14T22:25:04Z"
tags: []
categories: ["企业架构"]
slug: "spring-memcache-xmemcache"
draft: false
lastmod: "2014-08-14T22:23:13Z"
id: 2103958
url: "https://tom-seed.iteye.com/blog/2103958"
---

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包