一、在Windows下安装memcached服务端:
1、首先从网上下载memcached server端。文件可以从
http://code.jellycan.com/memcached/
2、解压缩该压缩包到本地,在该文件夹目录下执行memcached.exe -dinstall,安装server端服务。
3、进入到服务管理器中,找到memcachedserveice,将起启动,至此便已经完成在Windows中memcached server端的安装。
二、在项目中使用过程简介:
1、添加相应Java文件到项目中,源码在
memcacheddemo.zip中。
2、添加相应的Spring配置文件,内容如下:
<xmlns:xsi = http: / www.w3.org / 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
default-autowire="byName">
<bean id="sock" class="cn.com.demo.common.cache.memcached.client.SockIOPool"
factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>${mc.poolname}</value>
</constructor-arg>
<property name="servers" value="${mc.servers}" />
<property name="initConn" value="${mc.initconn}" />
<property name="minConn" value="${mc.minconn}" />
<property name="maxConn" value="${mc.maxconn}" />
<property name="maintSleep" value="${mc.maintsleep}" />
<property name="nagle" value="${mc.nagle}" />
<property name="socketTO" value="${mc.sockettimeout}" />
<property name="hashingAlg" value="${mc.hashingAlg}" />
</bean>
<bean id="memCachedClient" class="cn.com.demo.common.cache.memcached.client.MemCachedClient">
<constructor-arg index="0" value="${mc.poolname}" />
<property name="compressThreshold" value="${mc.client.compressthreshold}" />
</bean>
<bean id="memcachedCache" class="cn.com.demo.common.cache.memcached.MemcachedCacheImpl">
<property name="memCachedClient" ref="memCachedClient" />
<property name="cacheName" value="${mc.poolname}" />
</bean>
<bean id="memcachedAdvice" class="cn.com.demo.common.cache.memcached.MemcachedAdvice">
<property name="memcachedCache" ref="memcachedCache" />
</bean>
<bean id="CustomMemcachedAdvice" class="cn.com.demo.cache.CustomMemcachedAdvice">
<property name="memcachedCache" ref="memcachedCache" />
</bean>
<bean id="clearCustomMemcachedAdvice" class="cn.com.demo.cache.ClearCustomMemcachedAdvice">
<property name="memcachedCache" ref="memcachedCache" />
</bean>
<aop:config proxy-target-class="true">
<!--注解定义,所有加了注解的public方法在执行前会先执行拦截器里方法 -->
<aop:advisor pointcut="@annotation(cn.com.demo.cache.CustomMemcachedMethod) and execution(public * *(..))" advice-ref="CustomMemcachedAdvice" />
<aop:advisor pointcut="@annotation(cn.com.demo.cache.ClearCustomMemcachedMethod) and execution(public * *(..))" advice-ref="clearCustomMemcachedAdvice" />
<aop:advisor pointcut="@annotation(cn.com.demo.common.cache.memcached.Memcached) and execution(public * *(..))" advice-ref="memcachedAdvice" />
</aop:config>
</beans>
3、添加config.properties文件,内容如下:
mc.poolname=demo
mc.servers=127.0.0.1:11211
mc.initconn=10
mc.minconn=5
mc.maxconn=100
mc.maintsleep=30000
mc.nagle=false
mc.sockettimeout=3000
mc.client.compress=false
mc.client.compressthreshold=0
mc.hashingAlg=3
4、到目前为止,memcached在项目中的配置已经完毕,当然,这是需要相应的依赖包的,不过都是些基本的包,非常简单,我就不赘述了。
5、在使用的过程中,只需要在需要添加缓存的方法上添加@CustomMemcachedMethod(EntityName=demo.class)注解就可以了。