【maven项目】Spring MVC 中配置Memcache

今天项目上要求使用Memcache对数据进行缓存操作,因为之前的项目中用过Memcache,所以整理下来。

第一步:引入jar;在pom文件中添加以下内容

<!-- memcache -->
      		<dependency>
			<groupId>com.whalin</groupId>
			<artifactId>Memcached-Java-Client</artifactId>
			<version>3.0.0</version>
		</dependency>

 第二步:配置Memcache相关属性(init.properties)

#######################设置Memcached服务器参数#######################
#设置服务器地址
memcached.server=目标服务器ip:11211  #该端口号默认为11211
#容错
memcached.failOver=true
#设置初始连接数
memcached.initConn=20
#设置最小连接数
memcached.minConn=10
#设置最大连接数
memcached.maxConn=250
#设置连接池维护线程的睡眠时间
memcached.maintSleep=3000
#设置是否使用Nagle算法(Socket的参数),如果是true在写数据时不缓冲,立即发送出去
memcached.nagle=false
#设置socket的读取等待超时时间
memcached.socketTO=3000
#设置连接心跳监测开关
memcached.aliveCheck=true
#######################设置Memcached服务器参数#######################

 第三步:加载Memcache属性(memcached-content.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://code.alibabatech.com/schema/dubbo    
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/task  
	http://www.springframework.org/schema/task/spring-task-3.1.xsd">
	
	<!-- Memcached配置 -->  
    <bean id="memcachedPool" class="com.whalin.MemCached.SockIOPool"  
        factory-method="getInstance" init-method="initialize" destroy-method="shutDown">  
        <property name="servers">  
            <list>  
                <value>${memcached.server}</value>  
            </list>  
        </property>  
        <property name="initConn">  
            <value>${memcached.initConn}</value>  
        </property>  
        <property name="minConn">  
            <value>${memcached.minConn}</value>  
        </property>  
        <property name="maxConn">  
            <value>${memcached.maxConn}</value>  
        </property>  
        <property name="maintSleep">  
            <value>${memcached.maintSleep}</value>  
        </property>  
        <property name="nagle">  
            <value>${memcached.nagle}</value>  
        </property>  
        <property name="socketTO">  
            <value>${memcached.socketTO}</value>  
        </property>  
    </bean>
    
</beans>

 第四步:引入到spring配置文件中(applicationContext.xml)

<!-- 只需要加入这一句就可以 -->
<import resource="memcached-content.xml"/>

 

ok,大功告成!至于Memcache应用, 可以网上搜以下,就是一个单纯的exe文件, 用的时候打开就行,如图, 打开后就是这个样子

 要想看是否连接成功的话,可以在cmd中telnet 服务器地址 11211 (服务器地址与端口号之间是一个空格, 不要输入冒号)

猜你喜欢

转载自843977358.iteye.com/blog/2220963