java 使用Hibernate的二级缓存时默认缓存时间是多少
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2467466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the default caching time when using Hibernate's 2nd level cache
提问by cometta
When using Hibernate 2nd level cache and query cache and not specifying anything inside ehcache.xml, what is the default caching time?
当使用 Hibernate 2nd level cache 和 query cache 并且没有在里面指定任何东西时ehcache.xml,默认的缓存时间是多少?
回答by Pascal Thivent
Taken from the documentation on Cache Configuration:
取自有关缓存配置的文档:
The following attributes and elements are optional. timeToIdleSeconds: Sets the time to idle for an element before it expires. i.e. The maximum amount of time between accesses before an element expires Is only used if the element is not eternal. Optional attribute. A value of 0 means that an Element can idle for infinity. The default value is 0. timeToLiveSeconds: Sets the time to live for an element before it expires. i.e. The maximum time between creation time and when an element expires. Is only used if the element is not eternal. Optional attribute. A value of 0 means that and Element can live for infinity. The default value is 0.
Note that EHCache uses a timeToLive, not an expire time and the default is 0 if not specified.
请注意,EHCache 使用的是 timeToLive,而不是过期时间,如果未指定,则默认值为 0。
Update:While the above about defaults when configuring a cache is true, it appears that these defaults don't apply if you don't provide any ehcache.xml. So I dug a bit further and I think that EHCache may actually always use a defaultCachein that case - including for the StandardQueryCache- and this defaultCache has a timeToLive of 2 minutes:
更新:虽然上面关于配置缓存时的默认值是正确的,但如果您不提供任何ehcache.xml. 所以我进一步挖掘,我认为 EHCachedefaultCache在这种情况下实际上可能总是使用 a - 包括StandardQueryCache- 并且这个 defaultCache 的 timeToLive 为 2 分钟:
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
I can't confirm this right now but this is what I would do:
我现在无法确认这一点,但这是我会做的:
- first, activate logging on EHCache, EHCache logs a warning when the defaultCache is used:
- 首先,在 EHCache 上激活日志记录,当使用 defaultCache 时,EHCache 会记录警告:
While the defaultCache is a great convenience, it is preferable for each Cache to be configured individually. For this reason a log warning level message is issued each time a cache is created based on the defaultCache values.
虽然 defaultCache 非常方便,但最好单独配置每个 Cache。因此,每次基于 defaultCache 值创建缓存时都会发出日志警告级别消息。
- second, provide a ehcache.xml (and configure a cache for the StandardQueryCache).
- 其次,提供一个 ehcache.xml(并为StandardQueryCache配置一个缓存)。

