【问题标题】:How to configure eviction (time to live) on Amazon AWS Elasticache Redis + Spring Data如何在 Amazon AWS Elasticache Redis + Spring Data 上配置驱逐(生存时间)
【发布时间】:2014-03-09 13:52:27
【问题描述】:
【问题讨论】:
标签:
spring
caching
redis
amazon-elasticache
【解决方案1】:
您可以通过在 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>
这会将“customerCache”值配置为在这些值首次添加到缓存后 350 秒被逐出。
【解决方案2】:
除了接受的答案外,您还可以通过 Java config 配置缓存。这个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" />