2014-02-11 89 views
6

我正在開發一個項目,我們使用Spring Data Cache抽象以及AWS Elasticache Redis,我想知道如何配置驅逐緩存中對象的時間。如何在Amazon AWS上配置驅逐(生存時間)Elasticache Redis + Spring數據

關於如何使用Elasticache Redis配置Spring Data Cache抽象的官方文檔沒有太多。我們在這裏找到了一些很好的信息:http://blog.joshuawhite.com/java/caching-with-spring-data-redis/

但是,配置驅逐時間或緩存對象的生存時間沒有任何關係。任何幫助?

回答

11

您可以通過在RedisCacheManager中提供過期地圖來配置驅逐時間。例如 您指定這樣的高速緩存方法:

@Cacheable(value = "customerCache", key = "#id") 
public Customer findOne(Integer id) { 
    return customerRepository.findOne(id); 
} 

在你的applicationContext.xml它看起來就像這樣:

<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate" p:usePrefix="true"> 
    <property name="expires"> 
     <map> 
      <entry key="customerCache" value="350"/>      
     </map> 
    </property> 
</bean> 

這將配置後,這些被拆遷戶350秒「customerCache」值首先被添加到緩存中。

1

除了接受的答案,您還可以通過Java配置配置緩存。這Spring Cloud Sample對我很有幫助。該項目從ReferenceApplication.java改編而來。

在你@Configuration部分,你可以這樣說:

@Configuration 
@EnableElastiCache({@CacheClusterConfig(name = "customerCache", expiration = 360)}) 
@Profile("!local") 
protected static class ElastiCacheConfiguration {} 

它使用Spring Profiles的好處。羣集從您的aws-config.xml中挑選出來。在xml配置中設置區域上下文非常重要,否則您的羣集將不會被拾取。

<aws-context:context-region region="<your-region-here" />

相關問題