如何在AWS Auto Discovery中使用simple-spring-memcached

时间:2014-04-01 07:02:29

标签: amazon-elasticache spymemcached spring-cache simple-spring-memcached

如何在AWS Elasti Cache Auto Discovery功能中使用simple-spring-memcached库(SSM)?我们使用spymemcached作为客户端。

1 个答案:

答案 0 :(得分:0)

所以目前你正在使用spymemcached并希望使用简单的spring memcached(SSM)添加缓存层,对吧?如果是,那么请提供spymemcached的当前配置。使用SSM应该很容易使用相同的配置。

<强>更新

我在SSM中添加了使用AWS ElastiCache Cluster Client的新专用memcached提供程序。它在master branch上可用,尚未发布。如果您从主服务器构建SSM或使用此repository中提供的快照,则可以使用自动发现功能。

删除 spymemcached-provider spymemcached 的依赖项,而不是添加新的依赖项:

<dependency>
  <groupId>com.google.code.simple-spring-memcached</groupId>
  <artifactId>aws-elasticache-provider</artifactId>
  <version>3.4.1-SNAPSHOT</version>
</dependency>

使用以下配置:

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

  <cache:annotation-driven />

  <bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
    <property name="caches">
      <set>
        <bean class="com.google.code.ssm.spring.SSMCache">
          <constructor-arg name="cache" index="0" ref="defaultCache" />
          <!-- 5 minutes -->
          <constructor-arg name="expiration" index="1" value="300" />
          <!-- @CacheEvict(..., "allEntries" = true) won't work because allowClear is false, 
           so we won't flush accidentally all entries from memcached instance -->
          <constructor-arg name="allowClear" index="2" value="false" />
        </bean>
      </set>
    </property>
  </bean>

  <bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
    <property name="cacheName" value="defaultCache" />
    <property name="cacheClientFactory">
      <bean name="cacheClientFactory" class="com.google.code.ssm.providers.elasticache.MemcacheClientFactoryImpl" />
    </property>
    <property name="addressProvider">
      <bean class="com.google.code.ssm.config.DefaultAddressProvider">
      <!-- set only single address to configuration endpoint -->    
        <property name="address" value="mycluster.fnjyzo.cfg.use1.cache.amazonaws.com:11211" />
      </bean>
    </property>
    <property name="configuration">
      <bean class="com.google.code.ssm.providers.elasticache.ElastiCacheConfiguration">
        <!-- set client mode to dynamic to enable Auto Discovery feature -->
        <property name="clientMode" value="#{T(net.spy.memcached.ClientMode).Dynamic}" />
      </bean>
    </property>
  </bean>
</beans>

让我知道它是否适合你。

更新2

具有AWS Auto Discovery功能的新简单Spring Memcached版本3.5.0可在github和中央maven存储库中使用。