当使用Hibernate 2级缓存和查询缓存且未在ehcache.xml 内部指定任何内容时,默认的缓存时间是多少?

最佳答案

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


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 defaultCache in that case - including for the StandardQueryCache - and this defaultCache has a timeToLive of 2 minutes:

<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
        />

我目前无法确认,但这就是我要做的:
  • 首先,在EHCache上激活日志记录,当使用defaultCache时,EHCache记录警告:


  • 其次,提供一个ehcache.xml(并为StandardQueryCache配置一个缓存)。
  • 10-05 21:09
    查看更多