如何在Amazon AWS Elasticache Redis + Spring Data上配置驱逐(生存时间)

rug*_*bal 6 spring caching redis amazon-elasticache

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

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

但是,没有任何关于配置被驱逐对象的驱逐时间或时间.有帮助吗?

小智 13

您可以通过在RedisCacheManager中提供过期映射来配置驱逐时间.例如,您具有如下指定的可缓存方法:

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

在您的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>
Run Code Online (Sandbox Code Playgroud)

这将配置"customerCache"值在首次添加到缓存后350秒被驱逐.