Grails cache-ehcache插件和TTL值

5 grails ehcache

任何人都可以确认是否可以使用带有ehcache扩展名的grails缓存插件设置TTL设置,例如timeToLiveSeconds?

基本插件的文档明确声明不支持TTL,但ehcache扩展提到了这些值.到目前为止,我没有成功为我的缓存设置TTL值:

grails.cache.config = {
    cache {
        name 'messages'
        maxElementsInMemory 1000
        eternal false
        timeToLiveSeconds 120
        overflowToDisk false
        memoryStoreEvictionPolicy 'LRU'
    }
}

@Cacheable('messages')
def getMessages()
Run Code Online (Sandbox Code Playgroud)

但是,消息仍然无限期地缓存.我可以使用@CacheEvict注释手动刷新缓存,但我希望在使用ehcache扩展时支持TTL.

谢谢

Ken*_*Liu 5

是的,该cache-ehcache插件肯定支持TTL和EhCache本机支持的所有缓存配置属性.如文档中所述,基本缓存插件实现了一个不支持TTL的简单内存缓存,但Cache DSL将通过任何未知配置设置传递给底层缓存提供程序.

您可以通过将以下内容添加到Config.groovy或配置EhCache设置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
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在运行时验证缓存设置,如下所示:

grailsCacheManager.cacheNames.each { 
   def config = grailsCacheManager.getCache(it).nativeCache.cacheConfiguration
   println "timeToLiveSeconds: ${config.timeToLiveSeconds}"
   println "timeToIdleSeconds: ${config.timeToIdleSeconds}"
}
Run Code Online (Sandbox Code Playgroud)

有关其他缓存属性,请参阅EhCache javadoc for CacheConfiguration.您还可以通过记录grails.plugin.cache和启用缓存的详细调试日志记录net.sf.ehcache.

请注意,Grails缓存插件实现了自己的缓存管理器,该管理器与本机EhCache缓存管理器不同且独立.如果您已直接配置EhCache(使用ehcache.xml或其他方法),则这些缓存将与Grails插件管理的缓存分开运行.

注意:在旧版本的Cache-EhCache插件中确实存在一个错误,其中TTL设置未正确设置且对象在一年内到期; 这已在Grails-Cache-Ehcache 1.1中修复.