How to configure eviction (time to live) on Amazon AWS Elasticache Redis + Spring Data

如何配置驱逐(生存时间)对亚马逊的AWS elasticache redis +弹簧数据

问题 (Question)

I'm working on a project where we use Spring Data Cache abstraction along with AWS Elasticache Redis and I would like to know how to configure the eviction time of the objects on the cache.

There's not too much official documentation on how to configure Spring Data Cache Abstraction with Elasticache Redis. We found some good info here: http://blog.joshuawhite.com/java/caching-with-spring-data-redis/

But there's nothing about configuring eviction time or time to live of the objects that are cached. Any help?

我的工作在一个项目中,我们使用Spring的数据缓存抽象随着AWS elasticache redis,我想知道如何在缓存配置对象的清除时间。

没有太多的官方文件如何与elasticache redis配置Spring缓存数据抽象。我们在这里发现了一些好的信息:http://blog.joshuawhite.com/java/caching-with-spring-data-redis/

但没有配置驱逐时间或生存时间被缓存的对象。任何帮助吗?

最佳答案 (Best Answer)

You can configure eviction time by providing expires map in RedisCacheManager. For example you have cacheable method specified like that:

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

in your applicationContext.xml it will look like this:

<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>

This will configure "customerCache" values to be evicted 350 seconds after these were first added to the cache.

您可以配置驱逐时间提供地图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秒后,这些首先被添加到缓存中。

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/21707596