Java 如何区分ehcache中的生存时间和空闲时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2583429/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:29:27  来源:igfitidea点击:

How to differentiate between time to live and time to idle in ehcache

javaehcache

提问by Jacques René Mesrine

The docs on ehache says:

ehache 上的文档说:

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.

I understand timeToIdleSeconds

我理解timeToIdleSeconds

But does it means that after the creation & first access of a cache item, the timeToLiveSecondsis not applicable anymore ?

但这是否意味着在创建和首次访问缓存项之后,timeToLiveSeconds不再适用?

采纳答案by Boris Pavlovi?

timeToIdleSecondsenables cached object to be kept in as long as it is requested in periods shorter than timeToIdleSeconds. timeToLiveSecondswill make the cached object be invalidated after that many seconds regardless of how many times or when it was requested.

timeToIdleSeconds只要在短于 的时间段内请求缓存的对象,就可以将其保留timeToIdleSecondstimeToLiveSeconds无论请求多少次或何时请求,都将使缓存的对象在几秒钟后失效。

Let's say that timeToIdleSeconds = 3. Then the object will be invalidated if it hasn't been requested for 4 seconds.

这么说吧timeToIdleSeconds = 3。如果 4 秒内没有请求该对象,则该对象将失效。

If timeToLiveSeconds = 90, then the object will be removed from cache after 90 seconds, even if it has been requested few milliseconds in the 90th second of its short life.

如果timeToLiveSeconds = 90,则该对象将在 90 秒后从缓存中删除,即使它在其短暂生命的第 90 秒内被请求了几毫秒。

回答by Damo

From the old 1.1 documentation(available in Google Cache, which is easier to browse and more informative than the current docs AFAIK):

来自旧的 1.1 文档(在 Google Cache 中可用,它比当前的文档 AFAIK 更易于浏览且信息更多):

timeToIdleSeconds

This is an optional attribute.

Legal values are integers between 0 and Integer.MAX_VALUE.

It is the number of seconds that an Element should live since it was last used. Used means inserted or accessed.

0 has a special meaning, which is not to check the Element for time to idle, i.e. it will idle forever.

The default value is 0.

timeToLiveSeconds

This is an optional attribute.

Legal values are integers between 0 and Integer.MAX_VALUE.

It is the number of seconds that an Element should live since it was created. Created means inserted into a cache using the Cache.put method.

0 has a special meaning, which is not to check the Element for time to live, i.e. it will live forever.

The default value is 0.

空闲时间

这是一个可选属性。

合法值为 0 到 Integer.MAX_VALUE 之间的整数。

它是元素自上次使用以来应该存活的秒数。使用意味着插入或访问。

0有一个特殊的含义,就是不检查Element是否有空闲时间,即永远空闲。

默认值为 0。

生存时间

这是一个可选属性。

合法值为 0 到 Integer.MAX_VALUE 之间的整数。

它是元素自创建以来应该存活的秒数。Created 意味着使用 Cache.put 方法插入到缓存中。

0 有一个特殊的含义,就是不检查元素的生存时间,即永远存在。

默认值为 0。

回答by Lee Chee Kiam

If you set both, the expirationTimewill be Math.min(ttlExpiry, ttiExpiry), where

如果您同时设置两者,expirationTime则将是Math.min(ttlExpiry, ttiExpiry),其中

ttlExpiry = creationTime + timeToLive
ttiExpiry = mostRecentTime + timeToIdle

Full source code here.

完整的源代码在这里