`
天空之城
  • 浏览: 398723 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

关于ehcache的timeToLiveSeconds和timeToIdleSeconds

阅读更多

这两个参数很容易误解,看文档根本没用,我仔细分析了ehcache的代码。结论如下:

1、timeToLiveSeconds的定义是:以创建时间为基准开始计算的超时时长;

2、timeToIdleSeconds的定义是:在创建时间和最近访问时间中取出离现在最近的时间作为基准计算的超时时长;

3、如果仅设置了timeToLiveSeconds,则该对象的超时时间=创建时间+timeToLiveSeconds,假设为A;

4、如果没设置timeToLiveSeconds,则该对象的超时时间=min(创建时间,最近访问时间)+timeToIdleSeconds,假设为B;

5、如果两者都设置了,则取出A、B最少的值,即min(A,B),表示只要有一个超时成立即算超时。

为了更好理解,可直接查看代码。摘自:net.sf.ehcache.Element.java(版本1.2.4):

 

/** 
 * 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. 
 * 
 * @return the time to expiration 
 */  
public long getExpirationTime() {  
  
    if (!lifespanSet || eternal || (timeToLive == 0 && timeToIdle == 0)) {  
        return Long.MAX_VALUE;  
    }  
  
    long expirationTime = 0;  
    long ttlExpiry = creationTime + timeToLive * ONE_SECOND;  
  
    long mostRecentTime = Math.max(creationTime, nextToLastAccessTime);  
    long ttiExpiry = mostRecentTime + timeToIdle * ONE_SECOND;  
  
    if (timeToLive != 0 && (timeToIdle == 0 || lastAccessTime == 0)) {  
        expirationTime = ttlExpiry;  
    } else if (timeToLive == 0) {  
        expirationTime = ttiExpiry;  
    } else {  
        expirationTime = Math.min(ttlExpiry, ttiExpiry);  
    }  
    return expirationTime;  
}  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics