Grails缓存ehcache插件和TTL值
是否有人可以确认是否可以使用带有ehcache扩展的grails缓存插件设置TTL设置,例如timeToLiveSeconds 基本插件的文档明确指出不支持TTL,但ehcache扩展提到了这些值。到目前为止,我尚未成功为缓存设置TTL值:Grails缓存ehcache插件和TTL值,grails,ehcache,Grails,Ehcache,是否有人可以确认是否可以使用带有ehcache扩展的grails缓存插件设置TTL设置,例如timeToLiveSeconds 基本插件的文档明确指出不支持TTL,但ehcache扩展提到了这些值。到目前为止,我尚未成功为缓存设置TTL值: grails.cache.config = { cache { name 'messages' maxElementsInMemory 1000 eternal false timeTo
grails.cache.config = {
cache {
name 'messages'
maxElementsInMemory 1000
eternal false
timeToLiveSeconds 120
overflowToDisk false
memoryStoreEvictionPolicy 'LRU'
}
}
@Cacheable('messages')
def getMessages()
但是,消息将无限期地缓存。我可以使用@cacheexecute注释手动刷新缓存,但我希望在使用ehcache扩展时支持TTL
谢谢ehcache核心插件支持TTL属性。你是如何安装插件的?对于我的项目,我只有:
compile ":cache-ehcache:1.0.0"
在BuildConfig.groovy中的plugins闭包中。因为这个插件依赖于核心grails缓存插件,所以不需要声明它 是的,
cache-ehcache
插件肯定支持TTL和ehcache本机支持的所有缓存配置属性。如文档中所述,基本缓存插件实现了一个不支持TTL的简单内存缓存,但缓存DSL将通过任何未知的配置设置传递给底层缓存提供程序
通过将以下内容添加到Config.groovy
或CacheConfig.groovy
,可以配置EhCache设置:
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
}
}
您可以在运行时验证缓存设置,如下所示:
grailsCacheManager.cacheNames.each {
def config = grailsCacheManager.getCache(it).nativeCache.cacheConfiguration
println "timeToLiveSeconds: ${config.timeToLiveSeconds}"
println "timeToIdleSeconds: ${config.timeToIdleSeconds}"
}
有关其他缓存属性,请参见。您还可以通过记录grails.plugin.cache
和net.sf.ehcache
来启用缓存的详细调试日志记录
请注意,Grails缓存插件实现了自己的缓存管理器,它与本机EhCache缓存管理器不同且独立。如果您直接配置了EhCache(使用EhCache.xml或其他方式),那么这些缓存将与Grails插件管理的缓存分开运行
注意:在旧版本的Cache-EhCache插件中确实存在一个bug,TTL设置没有正确设置,对象将在一年内过期;这已在中修复。我可以通过使用
grails app/conf/BootStrap.groovy
脚本在启动时覆盖配置来解决此问题
例如,这是一个脚本,用于覆盖名为“mycache”的缓存的默认生存时间为60秒:
这个问题很久以前就被问过了,但是现在请看@Ken,链接断了
class BootStrap {
def grailsCacheManager
def init = { servletContext ->
grailsCacheManager.getCache("mycache").nativeCache
.cacheConfiguration.timeToLiveSeconds = 60
}
def destroy = {
}
}