Element类中比较常用的方法
1、public final long getCreationTime()
Gets the creationTime of the Element
Returns:
The creationTime value
获得元素创建的时间,即添加到缓存时的时间.
2、getLatestOfCreationAndUpdateTime
public final long getLatestOfCreationAndUpdateTime()
Calculates the latest of creation and update time
Returns:
if never updated, creation time is returned, otherwise updated time
返回最近的创建和更新时间,如果从来没有修改,就返回创建时间,否则返回更新的时间
3、getLastAccessTime
public final long getLastAccessTime()
Gets the last access time. Access means a get. So a newly created Element will have a
last access time equal to its create time.
返回最后一次操作时间。操作指的是获取,因此一个新创建的元素的最后获取时间和创建时间相
等。
4、getLastUpdateTime
public long getLastUpdateTime()
If there is an Element in the Cache and it is replaced with a new Element for the
same key, then both the version number and lastUpdateTime should be updated to reflect
that. The creation time will be the creation time of the new Element, not the original
one, so that TTL concepts still work.
Returns:
the time when the last update occured. If this is the original Element, the time
will be null
返回最后一次更新的时间。
如果在缓存后一个元素,并且这个被具有相同Key的元素覆盖,那么版本号和最后更新时间应该更新。创建的时间为新元素创建的时间,不是原来的那个,生存时间照样起作用。
5、isExpired
public boolean isExpired()
An element is expired if the expiration time as given by Element.getExpirationTime()
is in the past.
Returns:
true if the Element is expired, otherwise false. If no lifespan has been set for
the Element it is considered not able to expire.
判断元素是否到期。
6、getExpirationTime
public long getExpirationTime()
Returns the expiration time based on time to live. If this element also has a time to
idle setting, the expiry time will vary depending on whether the element is accessed.
获取终期时间。
Cache类中比较常用的方法
1、isDisabled
public boolean isDisabled()
Whether this cache is disabled. "Disabled" means:
1. bootstrap is disabled
2. puts are discarded
3. putQuiets are discarded
4. gets return null
In all other respects the cache continues as it is.
You can disable and enable a cache programmatically through the Cache.setDisabled
(boolean) method.
By default caches are enabled on creation, unless the net.sf.ehcache.disabled system
property is set.
2、isExpired
public final boolean isExpired(Element element)
throws IllegalStateException,
NullPointerException
Checks whether this cache element has expired.
The element is expired if:
1. the idle time is non-zero and has elapsed, unless the cache is eternal; or
2. the time to live is non-zero and has elapsed, unless the cache is eternal; or
3. the value of the element is null.
判断给定的元素是否已期满,期满有下面三种情况:
1、空闲时间是非零的而且已经流逝,除非cache是永久的
2、生存时间是非零的而且已经流逝,除非cache是永久的
3、这个元素的值为空
这个方法与Element类的方法的不同的地方在于:
Element的isExpired()只根据是否到了生存时间来判断Element是否是到期的
3、isKeyInCache
public boolean isKeyInCache(Object key)
An inexpensive check to see if the key exists in the cache.
This method is not synchronized. It is possible that an element may exist in the
cache and be removed before the check gets to it, or vice versa.
用isKeyInCache()的时候要注意,isKeyInCache这个方法只是检查这个key是否存在,并不检验这个
key是否过期。
4、replace
public Element replace(Element element)
throws NullPointerException
Replace the cached element only if an Element is currently cached for this key
更新的另一种方式
ehcache中的timeToLiveSeconds和timeToIdleSeconds时间
在ehcache中,缓存有2个失效相关的配置即 timeToLiveSeconds和timeToIdleSeconds,分别简称为ttl和tti。 在通常的解释中,前者表示一条缓存自创建时间起多少秒后失效,而后者表示一条缓存自最后读取或更新起多少秒失效。
在2个同时配置时可能时间计算就不那么简单了。 简单说来 任何一方为0,则以另一方时间为准。否则就以最短时间为准。
ehcache是这样计算失效时间的
1 如果ttl不为0并且tti为0, 如果缓存未被读过,失效时间=ttl
2 如果tti不为0,失效时间=tti+读取时间
3 否则 失效时间=min(ttl, tti+读取时间)