Grails缓存-ehcache插件和TTL值 [英] Grails cache-ehcache plugin and TTL values

查看:105
本文介绍了Grails缓存-ehcache插件和TTL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  grails.cache.config = {
缓存{
名'消息'
maxElementsInMemory 1000
永恒假
timeToLiveSeconds 120
overflowToDisk false
memoryStoreEvictionPolicy'LRU'
}
}

@Cacheable('messages')
def getMessages()

然而,这些消息仍然无限期地被缓存。我可以使用@CacheEvict注解手动刷新缓存,但我希望在使用ehcache扩展时支持TTL。



谢谢


是的, cache-ehcache 插件绝对支持TTL以及所有本地化的缓存配置属性。

解决方案

EhCache支持。正如文档中所述,基本缓存插件实现了一个不支持TTL的简单内存缓存,但缓存DSL将通过任何未知配置设置传递给底层缓存提供程序。



您可以通过将以下内容添加到 Config.groovy CacheConfig.groovy 来配置EhCache设置:

  grails.cache.config = {
cache {
名称'mycache'
}

//这不是缓存,它是一组应用于其他缓存的默认配置
defaults {
永久false
overflowToDisk true
maxElementsInMemory 10000
maxElementsOnDisk 10000000
timeToLiveSeconds 300
timeToIdleSeconds 0
}
}

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

  grailsCacheManager.cacheNames.each {
def config = grailsCacheMa nager.getCache(it).nativeCache.cacheConfiguration
printlntimeToLiveSeconds:$ {config.timeToLiveSeconds}
printlntimeToIdleSeconds:$ {config.timeToIdleSeconds}
}

请参阅用于CacheConfiguration的EhCache javadoc 以获取其他缓存属性。您还可以通过记录 grails.plugin.cache net.sf.ehcache 来启用缓存的详细调试日志记录。



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

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


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-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.

这篇关于Grails缓存-ehcache插件和TTL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆