7

我正在开发一个项目,我们将 Spring Data Cache 抽象与 AWS Elasticache Redis 一起使用,我想知道如何配置缓存上对象的逐出时间。

关于如何使用 Elasticache Redis 配置 Spring Data Cache Abstraction 的官方文档并不多。我们在这里找到了一些很好的信息:http: //blog.joshuawhite.com/java/caching-with-spring-data-redis/

但是没有关于配置缓存对象的驱逐时间或生存时间。有什么帮助吗?

4

2 回答 2

13

您可以通过在 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 秒被逐出。

于 2014-02-11T16:49:24.963 回答
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" />

于 2015-09-26T15:07:22.037 回答