关于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):

Java代码  收藏代码

  1. /** 
  2.  * Returns the expiration time based on time to live. If this element also has a time to idle setting, the expiry 
  3.  * time will vary depending on whether the element is accessed. 
  4.  * 
  5.  * @return the time to expiration 
  6.  */  
  7. public long getExpirationTime() {  
  8.   
  9.     if (!lifespanSet || eternal || (timeToLive == 0 && timeToIdle == 0)) {  
  10.         return Long.MAX_VALUE;  
  11.     }  
  12.   
  13.     long expirationTime = 0;  
  14.     long ttlExpiry = creationTime + timeToLive * ONE_SECOND;  
  15.   
  16.     long mostRecentTime = Math.max(creationTime, nextToLastAccessTime);  
  17.     long ttiExpiry = mostRecentTime + timeToIdle * ONE_SECOND;  
  18.   
  19.     if (timeToLive != 0 && (timeToIdle == 0 || lastAccessTime == 0)) {  
  20.         expirationTime = ttlExpiry;  
  21.     } else if (timeToLive == 0) {  
  22.         expirationTime = ttiExpiry;  
  23.     } else {  
  24.         expirationTime = Math.min(ttlExpiry, ttiExpiry);  
  25.     }  
  26.     return expirationTime;  

http://itstarting.iteye.com/blog/613153


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值