If you're talking about org.springframework.cache.concurrent.ConcurrentMapCache
that comes with Spring, it's not possible. It simply uses java.util.concurrent.ConcurrentHashMap
as a cache store.
However, you can create scheduler to remove the cache manually. If you want to go that route, you have to create the object that holds timestamp information.
class CacheElement {
long timestamp;
Object value;
public CacheElement(Object value){
this.value = value;
touch();
}
public void touch(){
this.timestamp = new Date().getTime();
}
..../omit for breifvity
}
You have to wrap the cache value with CacheElement
as defined above
and you have to override the org.springframework.cache.Cache.ValueWrapper#get
to call touch()
before returning value. Also, you may want to check whether the cache value already expires. Compare timestamp in CacheElement with current timestamp if it's less than specified TTL, touch() it. Otherwise, remove from cache store and return null.
Hope you got the idea :)
My suggestion, use ehcache or other libraries that support TTL.