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

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?

How To Backup AWS Elasticache Redis Data?

I’m testing a new Redis cluster (AWS Large primary/slave nodes) in AWS Elasticache Redis. How can I backup or export my data from the Redis cluster? Other hosted redis services automatically create a



Can you connect to Amazon Elasticache Redis outside of Amazon

I’m able to connect to an Elasticache redis instance in a VPC from EC2 instances, but I would like to know if there is a way to connect to an Elasticache Redis node outside of Amazon EC2 instances, su

AWS Elasticache SDK doesn’t work for Redis ElastiCache

I want to dynamically configure my API servers depending on the name of the cluster. So I’m using AmazonElastiCacheClient to discover the clusters name and need to extract the endpoint of the one th

Node Redis with AWS

AWS has Redis support via the ElastiCache Service. My question is, can I connect to Redis on AWS Elasticache from node, using the following: var client = require(‘redis’).createClient(6379, ‘elasticha

SSL support in AWS ElastiCache

We would like to use AWS ElastiCache for our application, and we have a strict requirement that all data should be encrypted in transit. I was trying to find out whether ElastiCache access is done usi

Php Zend with AWS ElastiCache

Hi i have an app with php zend framework, i setup an AWS ElastiCache as follow on my zend app Bootstrap.php: protected function _initMemcache() { if (extension_loaded(‘memcache’)) { // Configure cachi

Configuring Laravel 4 with AWS Elasticache Memcached

I have Amazon Elasticache Memcached node (just one) I have a webserver in the same region. Cache subnet group VPC ID is the same as EC2 instance’s, the permissions are set properly from AWS perspectiv

How to use enyim memcached client with amazon elasticache in c#

I have created a cache cluster on Amazon ElastiCache. It has given me an endpoint address. Now I want to store the data in Amazon ElastiCache. For that I am making use of enyim memcached client. The s

Amazon ElasticCache Redis Public Access

I have a Amazon AWS account.I want to use ElasticCache Redis.I’ve installed ElasticCache Redis.How i connect ElasticCache Redis from public address? I’ve installed Amazon Linux AMI.I’am accessing Elas

AWS Elasticache + set()

I can get .set() to work when interacting with Elasticache if I use the compression & time settings (third and forth settings) $memcached = new Memcached(); $memcached->setOption(Memcached::OP

Answers

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