Spring集成memcache(一)memcache-client

Windows下的Memcache安装:

1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached

2. 在终端(也即cmd命令界面)下输入 'c:\memcached\memcached.exe -d install' 安装

3. 再输入: 'c:\memcached\memcached.exe -d start' 启动。

NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。

 

memcache-client是官方公布的jar包,源代码内容较少,与Spring集成也比较简单,步骤如下:

1.添加memcache-client.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.server=127.0.0.1\:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
 

 

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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
		factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
		<property name="servers">
			<list>
				<value>${memcache.server}</value>
			</list>
		</property>
		<property name="initConn">
			<value>${memcache.initConn}</value>
		</property>
		<property name="minConn">
			<value>${memcache.minConn}</value>
		</property>
		<property name="maxConn">
			<value>${memcache.maxConn}</value>
		</property>
		<property name="maintSleep">
			<value>${memcache.maintSleep}</value>
		</property>
		<property name="nagle">
			<value>${memcache.nagle}</value>
		</property>
		<property name="socketTO">
			<value>${memcache.socketTO}</value>
		</property>
	</bean>

	<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
	</bean>
</beans>

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

 

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

 

以下是Memcache Win32以及memcache-client.jar包

你可能感兴趣的:(memcache)