【问题标题】: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 Data Cache 抽象和 AWS Elasticache Redis,我想知道如何配置缓存上对象的逐出时间。

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

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

【问题讨论】:

    标签: 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 配置中设置区域上下文非常重要,否则您的集群将不会被拾取。

      &lt;aws-context:context-region region="&lt;your-region-here" /&gt;

      【讨论】:

        猜你喜欢
        • 2020-06-03
        • 2018-07-06
        • 2021-04-17
        • 2015-10-18
        • 2018-03-23
        • 2020-10-02
        • 2015-04-05
        • 2021-02-05
        • 2015-08-14
        相关资源
        最近更新 更多