问题
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
回答1:
Yes, the cache-ehcache plugin 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.groovy or CacheConfig.groovy:
grails.cache.config = {
cache {
name 'mycache'
}
//this is not a cache, it's a set of default configs to apply to other caches
defaults {
eternal false
overflowToDisk true
maxElementsInMemory 10000
maxElementsOnDisk 10000000
timeToLiveSeconds 300
timeToIdleSeconds 0
}
}
You can verify the cache settings at runtime as follows:
grailsCacheManager.cacheNames.each {
def config = grailsCacheManager.getCache(it).nativeCache.cacheConfiguration
println "timeToLiveSeconds: ${config.timeToLiveSeconds}"
println "timeToIdleSeconds: ${config.timeToIdleSeconds}"
}
See the EhCache javadoc for CacheConfiguration for the other cache properties. You can also enable detailed debug logging of caching by logging grails.plugin.cache and net.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.
回答2:
The TTL property is supported with the ehcache core plugin. How are you installing the plugin? For my project, I only have:
compile ":cache-ehcache:1.0.0"
in BuildConfig.groovy in the plugins closure. Since this plugin has a dependency on the core grails cache plugin, you don't need to declare it.
回答3:
I could resolve this issue overriding the configuration at startup time with the grails-app/conf/BootStrap.groovy script.
For example, this is the script to override the default time to live to 60 seconds of a cache named "mycache":
class BootStrap {
def grailsCacheManager
def init = { servletContext ->
grailsCacheManager.getCache("mycache").nativeCache
.cacheConfiguration.timeToLiveSeconds = 60
}
def destroy = {
}
}
来源:https://stackoverflow.com/questions/12415029/grails-cache-ehcache-plugin-and-ttl-values