Java 如何区分ehcache中的生存时间和空闲时间
eCache上的文档说:Java 如何区分ehcache中的生存时间和空闲时间,java,ehcache,Java,Ehcache,eCache上的文档说: 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 timeToLiveSeconds: Sets the time to live for an element before it expires. i.e. The ma
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
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.
我理解时间到秒
但是,这是否意味着在创建和首次访问缓存项之后,timeToLiveSeconds不再适用?
timeToIdleSeconds
允许缓存对象保留在小于timeToIdleSeconds
的时间段内,只要请求缓存对象timeToLiveSeconds
将使缓存对象在这几秒钟后失效,而不管它被请求了多少次或何时
假设timeToIdleSeconds=3
。如果该对象在4秒钟内未被请求,则该对象将无效
如果timeToLiveSeconds=90
,则该对象将在90秒后从缓存中删除,即使该对象在其短暂生命周期的90秒内已被请求数毫秒 来自(可在Google Cache中找到,它比当前的文档AFAIK更易于浏览和提供更多信息):
timeToIdleSeconds
这是一个可选属性
合法值是介于0和Integer.MAX_值之间的整数
它是一个元素自创建以来应生存的秒数
最后一次使用。“已使用”表示插入或访问
0有一个特殊的含义,即不检查元素的时间
闲置,即它将永远闲置
默认值为0
timeToLiveSeconds
这是一个可选属性
合法值是介于0和Integer.MAX_值之间的整数
它是一个元素自创建以来应生存的秒数
创建。创建意味着使用cache.put插入缓存
方法
0有一个特殊的含义,即不检查元素的时间
活着,也就是说,它将永远活着
默认值为0
如果同时设置这两个参数,则
expirationTime
将为Math.min(ttlExpiry,ttiExpiry)
,其中
ttlExpiry = creationTime + timeToLive
ttiExpiry = mostRecentTime + timeToIdle
完整的源代码。因此,当您说“让我们假设timeToIdleSeconds=3。如果在4秒钟内没有请求对象,则该对象将无效”时,我假定我们总是希望在上面的注释中设置idletime,当您说invalidate时,这是什么意思?它是否将其从堆中移除?如果从缓存中删除了对象,那么我对timeToLive参数的用途感到困惑。当我们进行POC时,我们看到数据是在TimeToIdles之后从源中获取的。虽然timetoLive的值要高得多,但我希望它是从缓存中获取的,因为在我们的例子中,timetoLive的值比timeToIdle的值高得多。@Gayathri如果您有一个经常访问(每两秒一次)但TTL为60秒的数据项。它仍然会每六十秒从源代码中提取一次,即使它被不断地访问(从不空闲)。对于这种情况,TTL和TTI集(即大于零):1)TTI>=TTL:TTI无效。在
creationTime+TTL
2)TTImin((max(lastAccessTime,creationTime)+TTI),(creationTime+TTL))之后被视为过期
“无关”->“不管”现在这种行为对我来说是有意义的。感谢您指出这一点,特别是Math.min
部分。此代码比上面的人类解释更清楚:-)