Can anyone confirm if TTL settings e.g. timeToLiveSeconds can be set using the grails cache plugin with the ehcache extension?
The documentation for the base plugin explicitly states that TTL is not supported, but the ehcache extension mentions these values. So far I’ve had no success setting TTL values for my cache:
grails.cache.config = {
cache {
name 'messages'
maxElementsInMemory 1000
eternal false
timeToLiveSeconds 120
overflowToDisk false
memoryStoreEvictionPolicy 'LRU'
}
}
@Cacheable('messages')
def getMessages()
However the messages remain cached indefinitely. I can manually flush the cache using the @CacheEvict annotation but I was hoping that TTL would be supported when using the ehcache extension.
Thanks
Yes, the
cache-ehcacheplugin definitely supports TTL and all of the cache configuration properties that are natively supported by EhCache. As stated in the doc, the base cache plugin implements a simple in-memory cache that does not support TTL, but the Cache DSL will pass through any unknown configuration settings to the underlying cache provider.You can configure the EhCache settings by adding the following to
Config.groovyorCacheConfig.groovy:You can verify the cache settings at runtime as follows:
See the EhCache javadoc for CacheConfiguration for the other cache properties. You can also enable detailed debug logging of caching by logging
grails.plugin.cacheandnet.sf.ehcache.Note that the Grails caching plugins implement their own cache manager which is different and separate from the native EhCache cache manager. If you have configured EhCache directly (using ehcache.xml or other means) then these caches will run separately from the caches managed by the Grails plugin.
Note: There was indeed a bug in older versions of the Cache-EhCache plugin where the TTL setting was not being set correctly and objects were expiring in a year; this was fixed in Grails-Cache-Ehcache 1.1.