memcached是一种分布式缓存工具,有效提升了按主键检索数据的性能问题。

Simple-Spring-Memcached 是memcached与spring 框架整合的一种缓存框架,本质是采用aop的方式实现缓存的调用和管理。其核心组件声明了一些Advice,当遇到相应的切入点时,会执行这些Advice来对memcached 加以管理。

Simple-Spring-Memcached 本身不提供cache机制,需借助第三方组件,比如:spymemcached,xmemcached,aws-elasticache.

本文以Maven项目,采用spymemcached 为例。

首先在.pom文件中添加相关依赖

<dependency>
    <groupId>com.google.code.simple-spring-memcached</groupId>
    <artifactId>spymemcached</artifactId>
    <version>2.8.4</version>
</dependency>
<dependency>
    <groupId>com.google.code.simple-spring-memcached</groupId>
    <artifactId>spymemcached-provider</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>com.google.code.simple-spring-memcached</groupId>
    <artifactId>simple-spring-memcached</artifactId>
    <version>3.1.0</version>
</dependency>

以上是原作者的配置,或者使用下面本人的配置

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

配置文件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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  <import resource="simplesm-context.xml" />

  <aop:aspectj-autoproxy /> 
   <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
        <property name="cacheClientFactory">
            <!--  spymemcached配置方法 -->
            <bean name="cacheClientFactory"
                  class="com.google.code.ssm.providers.spymemcached.MemcacheClientFactoryImpl"/>
        </property>
        <!-- 定义了缓存节点的IP地址和端口号 -->
        <property name="addressProvider">
            <bean class="com.google.code.ssm.config.DefaultAddressProvider">
                <property name="address" value="${aliyun.memcached}:11211"/>
            </bean>
        </property>
        <!-- 定义了缓存节点的查找方法 -->
        <property name="configuration">
            <bean class="com.google.code.ssm.providers.CacheConfiguration">
                <property name="consistentHashing" value="true"/>
            </bean>
        </property>
    </bean>

</beans>
simplesm-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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- mode: ${mode} -->
    <!--<aop:aspectj-autoproxy /> -->

    <context:component-scan base-package="com.google.code.ssm" />

    <bean id="cacheBase" class="com.google.code.ssm.aop.CacheBase" />

    <bean id="readThroughSingleCache" class="com.google.code.ssm.aop.ReadThroughSingleCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="readThroughMultiCache" class="com.google.code.ssm.aop.ReadThroughMultiCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="readThroughAssignCache" class="com.google.code.ssm.aop.ReadThroughAssignCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="updateSingleCache" class="com.google.code.ssm.aop.UpdateSingleCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="updateMultiCache" class="com.google.code.ssm.aop.UpdateMultiCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="updateAssignCache" class="com.google.code.ssm.aop.UpdateAssignCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="invalidateSingleCache" class="com.google.code.ssm.aop.InvalidateSingleCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="invalidateMultiCache" class="com.google.code.ssm.aop.InvalidateMultiCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="invalidateAssignCache" class="com.google.code.ssm.aop.InvalidateAssignCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>

    <bean id="incrementCounterInCache" class="com.google.code.ssm.aop.counter.IncrementCounterInCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="decrementCounterInCache" class="com.google.code.ssm.aop.counter.DecrementCounterInCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="readCounterFromCache" class="com.google.code.ssm.aop.counter.ReadCounterFromCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>
    <bean id="updateCounterInCache" class="com.google.code.ssm.aop.counter.UpdateCounterInCacheAdvice">
        <property name="cacheBase" ref="cacheBase" />
    </bean>

</beans>
现在现在配置基本完成,现在就可以进行实际操作了。下面会介绍一些常见的注解

@CacheKeyMethod

用来注解缓存key值生成的方法。方法无参数,返回值为字符串。如果能找到该注解,则使用该注解的方法生成唯一的key.若找不到则使用Object.toString()

最常用的九大注解:

@ReadThroughSingleCache,@ReadThroughMultiCache,
@ReadThroughAssignCache,@InvalidateSingleCache,
@InvalidateMultiCache,@InvalidateAssignCache,
@UpdateSingleCache,@UpdateMultiCache,@UpdateAssignCache

按照操作类型分为:read,Invalidate,update 读取,失效,更新

按照操作对象分为:Single,Multi,Assign

SingleCache:操作单个POJO的Cache数据,由
ParameterValueKeyProvider和CacheKeyMethod提供标识组装key

MultiCache:读取多个缓存
AssignCache:注解指定key
一些例子
@ReadThroughSingleCache
  @ReadThroughSingleCache(namespace = "test" ,expiration = 3600)
    public ObUsers getUsers(@ParameterValueKeyProvider long id){
        return new ObUsers();
    }

    //多个数据生成key ,需要 order 属性
    @ReadThroughSingleCache(namespace = "test" ,expiration = 3600)
    public ObUsers getUsers2(@ParameterValueKeyProvider long id,
                             @ParameterValueKeyProvider(order = 1) String name){
        return new ObUsers();
    }
@ReadThroughMultiCache
@ReadThroughMultiCache(namespace = "userlist")
    //expiration 不写,默认为0,会使用key值的默认过期时间
    public ArrayList<ObUsers> getUserBaseInfo(@ParameterValueKeyProvider ArrayList<Long> idList) {
        return null;
    }

    @ReadThroughMultiCache(option = @ReadThroughMultiCacheOption(generateKeysFromResult = true),
            namespace = "userlist",expiration = 3600)
    //generateKeysFromResult = TRUE 通过结果生成key
    public ArrayList<ObUsers> getUserBaseInfo2(@ParameterValueKeyProvider ArrayList<Long> idList) {
        return null;
    }
@ReadThroughAssignCache
@ReadThroughAssignCache(assignedKey = "all",namespace = "test",expiration = 3600)
    public ArrayList<ObUsers> getAll() {
        return null;
    }
@UpdateSingleCache
@UpdateSingleCache(namespace = "test", expiration = 3600)
    @ReturnDataUpdateContent
    //更新后的值成为缓存中的返回值
    public ObUsers updateUsers(@ParameterValueKeyProvider  long id) {
        return new ObUsers();
    }


    @UpdateSingleCache(namespace = "test", expiration = 3600)
    public ObUsers updateUsers2(@ParameterValueKeyProvider @ParameterDataUpdateContent ObUsers user) {
        return new ObUsers();
    }
@UpdateMultiCache,@UpdateAssignCache
@UpdateMultiCache(namespace = "userlist")
    @ReturnDataUpdateContent
    public ArrayList<ObUsers> updateUserBaseInfo(@ParameterValueKeyProvider ArrayList<Long> idList) {
        return null;
    }

    @UpdateAssignCache(assignedKey = "all",namespace = "test")
    public ArrayList<ObUsers> updateAll() {
        return null;
    }
@InvalidateSingleCache,@InvalidateMultiCache,@InvalidateAssignCache
@InvalidateSingleCache(namespace = "test")
    @ReturnValueKeyProvider
    public long removeUsers(@ParameterValueKeyProvider  long id) {
        return id;
    }


    @InvalidateMultiCache(namespace = "userlist")
    public ArrayList<Long> removeUserBaseInfo(@ParameterValueKeyProvider ArrayList<Long> idList) {
        return idList;
    }

    @InvalidateAssignCache(assignedKey = "all",namespace = "test")
    public ArrayList<ObUsers> removeAll() {
        return null;
    }
其他的一些注解
@ReadCounterFromCache 读取计数器
@IncrementCounterInCache 计数器加一
@DecrementCOunterIncache 计数器减一
@UpdateCounterIncache 更新计数器