1、添加pom依赖

<dependencies>

<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>spring-cache</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>xmemcached-provider</artifactId>
<version>4.0.0</version>
</dependency>

<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>simple-spring-memcached</artifactId>
<version>4.0.0</version>
</dependency>

</dependencies>

2、创建memcached.properties

#simple memcached config
simple.memcache.server=127.0.0.1:12000
simple.memcache.consistenthashing=true
simple.memcache.connectionpoolsize=1
simple.memcache.optimizeget=false
simple.memcache.optimizemergebuffer=false
simple.memcache.mergefactor=50
simple.memcache.usebinaryprotocol=true
simple.memcache.connectiontimeout=3000
simple.memcache.operationtimeout=2000
simple.memcache.enableheartbeat=true
simple.memcache.failureMode=false

3、创建memcached-simple.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:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
default-autowire="byName">

<!-- =================================== 配置Memcached =============================== -->
<!-- 开启缓存 -->
<import resource="classpath:simplesm-context.xml" />
<aop:aspectj-autoproxy />
<context:annotation-config />
<bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
<property name="cacheClientFactory">
<bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
</property>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="${simple.memcache.server}" />
</bean>
</property>
<property name="configuration">
<bean class="com.google.code.ssm.providers.xmemcached.XMemcachedConfiguration">
<property name="consistentHashing"><!--consistentHashing属性定义了缓存节点的查找方法: 是否使用哈希 -->
<value>${simple.memcache.consistenthashing}</value>
</property>
<property name="connectionPoolSize">
<value>${simple.memcache.connectionpoolsize}</value>
</property>
<property name="optimizeGet">
<value>${simple.memcache.optimizeget}</value>
</property>
<property name="optimizeMergeBuffer">
<value>${simple.memcache.optimizemergebuffer}</value>
</property>
<property name="mergeFactor">
<value>${simple.memcache.mergefactor}</value>
</property>
<property name="useBinaryProtocol">
<value>${simple.memcache.usebinaryprotocol}</value>
</property>
<property name="connectionTimeout">
<value>${simple.memcache.connectiontimeout}</value>
</property>
<property name="operationTimeout">
<value>${simple.memcache.operationtimeout}</value>
</property>
<property name="enableHeartBeat">
<value>${simple.memcache.enableheartbeat}</value>
</property>
<property name="failureMode">
<value>${simple.memcache.failureMode}</value>
</property>
</bean>
</property>
<!-- 该Memcached配置的Cache名称 一个应用中存在多个Memcached时,各个配置的cacheName必须不同。如果该值未设,系统默认为default -->
<property name="cacheName" value="default" />
</bean>

<bean class="com.google.code.ssm.Settings"><!-- 这玩意儿在3.2 后,文档可以指定顺序 以及 拦截器 前后执行 - -!暂时没用过,加上不报错 -->
<property name="order" value="500" />
</bean>
</beans>

4、创建spring-context.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
default-autowire="byName">
<context:property-placeholder location="classpath*:properties/memcached.properties"/>
<context:annotation-config />
<context:component-scan base-package="io.mykit.cache.spring" />
<aop:config proxy-target-class="true"/>

<!-- <import resource="classpath:memcached/memcached.xml"/> -->
<import resource="classpath:memcached/memcached-simple.xml"/>
</beans>

5、编写使用注解的类

/**
* 根据订单号获取订单信息
* @param orderNum
* @return
*/
@ReadThroughSingleCache(namespace = "test", expiration = 30000)
public Order getOrder(@ParameterValueKeyProvider String orderNum) {
System.out.println("缓存没有命中");
return this.orderDao.getOrder(orderNum);
}

注意: @ReadThroughSingleCache(namespace = "test", expiration = 30000) 中的expiration单位是秒!!

就是这么简单,如果大家想了解simple-spring-memcached其他的注解,可参考博文《Memcached之——simple-spring-memcached-3.0.x 注解详解》