How to set up eviction (lifetime) on Amazon AWS Elasticache Redis + Spring Data

I am working on a project in which we use the Data Cache Spring abstraction with AWS Elasticache Redis, and I would like to know how to set the time to turn off objects in the cache.

There's not too much official documentation on setting up Spring. Abstraction of data caching with Elasticache Redis. We found useful information here: http://blog.joshuawhite.com/java/caching-with-spring-data-redis/

But there is nothing about how to set up eviction time or life time for cached objects. Any help?

+6
source share
2 answers

You can customize your eviction time by providing an expiration card in RedisCacheManager. For example, you have a caching method specified as follows:

@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 allow you to configure the "customerCache" values, which will be evicted 350 seconds after they are first added to the cache.

+11
source

In addition to the accepted answer, you can also configure the cache through the Java configuration. This Spring Cloud Sample has been helpful to me. This has been adapted from ReferenceApplication.java in this project.

In the @Configuration section @Configuration you can say the following:

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

This has the added benefit of using Spring Profiles . The cluster is selected from aws-config.xml . It is very important to set the region context in the xml configuration, or your cluster will not be raised.

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

+1
source

All Articles