spring中配置memcache

一. 安装memcache

1.下载memcache

下载地址 http://memcached.org/  最新版本v1.4.15

下载libevent  地址http://libevent.org/  (通信程序,memcache所依赖的异步程序通知库)

linux安装命令:

先安装libevent

# tar zxvf libevent-1.4.9-stable.tar.gz

# cdlibevent-1.4.9-stable

# ./configure--prefix=/usr

# make

# make install

测试libevent是否安装成功:

    #ll /usr/lib| grep libevent

运行memcached

# /usr/local/bin/memcached -p 11211 –d  -m 512 -vv

检查是否正常启动

# telnet localhost 11211

2.windows下安装memcache

安装完成后服务中会出现

启动服务后就可以连接memcache服务器了

一些基本的使用就不在这里介绍了。接下来进入主题在spring中配置memcache。

二、在spring中配置memcache

1、新建配置文件 :spring-memcache.xml的spring配置文件

<?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">


<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>

<property name="servers">
<list>
<value>${memcache.server}</value>
</list>
</property>


<property name="initConn">
<value>${memcache.initConn}</value>
</property>

<property name="minConn">
<value>${memcache.initConn}</value>
</property>


<property name="maxConn">
<value>${memcache.initConn}</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">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
</bean>
</beans>



2、在web.xml中配置新建的文件

<!-- 配置spring的监听器,加载Spring配置文件--> 

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext,
					 classpath*:spring-*.xml,
					 classpath*:applicationContext-*.xml
		</param-value>
	</context-param>

3、修改spring的配置文件applicationContext.xml,并添加一个spring工具类,便于以后调用

<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:memcached.properties</value>
				<value>classpath:mysql.properties</value>
			</list>
		</property>
	</bean>

4、创建memcache的资源文件和spring工具类

memcache.properties
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


spring工具类:

package com.hope.common;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContextHolder implements ApplicationContextAware {

	private static ApplicationContext	applicationContext =new ClassPathXmlApplicationContext("spring-memcache.xml");

	public void setApplicationContext(ApplicationContext applicationContext){
		SpringContextHolder.applicationContext = applicationContext;
	}

	public static ApplicationContext getApplicationContext(){
		checkApplicationContext();
		return applicationContext;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name){
		checkApplicationContext();
		return (T) applicationContext.getBean(name);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<T> clazz){
		checkApplicationContext();
		Map beanMaps = applicationContext.getBeansOfType(clazz);
		if (beanMaps != null && !beanMaps.isEmpty()) {
			return (T) beanMaps.values().iterator().next();
		} else {
			return null;
		}
	}

	private static void checkApplicationContext(){
		if (applicationContext == null) {
			throw new IllegalStateException(
					"applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
		}
	}
}


配置memcache自己的工具类:

import com.danga.MemCached.MemCachedClient;

public class MemcacheUtil {
	public static MemCachedClient getMemCachedClient(){
		return SpringContextHolder.getBean("memCachedClient");
        }
}



最后用junit测试是否成功:

package com.hope;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 import com.danga.MemCached.MemCachedClient;
 
 public class TestMemcache {
     MemCachedClient memCachedClient;
     @Before
     public void beforeTest(){
         
         ApplicationContext atx = new ClassPathXmlApplicationContext("spring-memcache.xml");
         memCachedClient = (MemCachedClient)atx.getBean("memCachedClient");
     }
     
     
     @Test
     public void TestMem(){
         
         memCachedClient.set("name", "liu");
         System.out.println(memCachedClient.get("name"));
     }
     
     
     
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值