質問

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?

役に立ちましたか?

解決

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.

他のヒント

In addition to the accepted answer, you can also configure the cache via Java config. This Spring Cloud Sample was helpful to me. This was adapted from ReferenceApplication.java in that project.

In your @Configuration section, you can say this:

@Configuration
@EnableElastiCache({@CacheClusterConfig(name = "customerCache", expiration = 360)})
@Profile("!local")
protected static class ElastiCacheConfiguration {}

It has the added benefit of using Spring Profiles. The cluster is picked up from your aws-config.xml. It is really important to set the region context in the xml config or your cluster won't be picked up.

<aws-context:context-region region="<your-region-here" />

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top