2014-02-11 49 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" />

相关问题