Описание тега simple-spring-memcached

Включает кэширование в bean-компонентах, управляемых Spring, с помощью аннотаций Java и Spring/AspectJ AOP поверх клиента spymemcached, xmemcached или aws-elasticache. Может работать как серверная часть кеша для memcached в абстракции Spring Cache (@Cacheable).

Introduction

Distributed caching can be a big, hairy, intricate, and complex proposition when using it extensively.

Simple Spring Memcached (SSM) attempts to simplify implementation for several basic use cases.

(09-06-2015) New version 3.6.0 with Amazon ElastiCache support is available! Since version 3.0.0 it can work as a cache back-end for Spring Cache (@Cacheable). Please check release notes.

This project enables caching in Spring-managed beans, by using Java 5 Annotations and Spring/AspectJ AOP on top of the spymemcached or xmemcached client. Using Simple Spring Memcached requires only a little bit of configuration and the addition of some specific annotations on the methods whose output or input is being cached.

Usage

If you are using maven, you can try it now:

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

and define connection to memcached on localhost:

<beans xmlns="http://www.springframework.org/schema/beans" 
    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">
            <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="127.0.0.1:11211" />
            </bean>
      </property>
      <property name="configuration">
            <bean class="com.google.code.ssm.providers.CacheConfiguration">
                  <property name="consistentHashing" value="true" />
            </bean>
      </property>
 </bean>

Now you can annotate method to cache result:

@ReadThroughSingleCache(namespace = "CplxObj", expiration = 3600)
public ComplexObject getComplexObjectFromDB(@ParameterValueKeyProvider Long complexObjectPk) {
  // ...
  return result;
}

If you already using Spring Cache you may use SSM as an another back-end.

If you are not using maven you can download current SSM distribution from github.

References

Github project