您的位置:首页 > 其它

关于Ehcache缓存中timeToLiveSeconds和timeToIdleSeconds区别

2017-02-07 14:57 162 查看
其中主要记录的是timeToLiveSeconds和timeToIdleSeconds;因为此俩容易搞混淆:

timeToLiveSeconds=200:缓存自创建日期起至失效时的间隔时间200;

timeToIdleSeconds=200:缓存创建以后,最后一次访问缓存的日期至失效之时的时间间隔200;

如果仅有 timeToLiveSeconds 那么 自创建时间开始 间隔x后缓存失效;

如果没有timeToLiveSeconds 那么自最后一次访问缓存 间隔y后 缓存失效;

如果既有timeToLiveSeconds 也有 timeToIdleSeconds 那么取最小数算作间隔时间;min(x,y);; 

计算原则是:若自创建缓存后一直都没有访问缓存,那么间隔x后失效,

若自创建缓存后有N次访问缓存,那么计算(最后一次访问缓存时间+y ) 

即:按照timeToIdleSeconds计算,但总存活时间不超过 y;举个例子:

timeToIdleSeconds=120;

timeToLiveSeconds=180;

上面的表示此缓存最多可以存活3分钟,如果期间超过2分钟未访问 那么此缓存失效!

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />

<defaultCache maxElementsInMemory="0" eternal="false" overflowToDisk="true" timeToIdleSeconds="1200" timeToLiveSeconds="1200" />
<!--
Cache configuration

The follow attributes are required.
name: sets the name of the cache. This is used to identify the cache. It must be unique.
maxElementsInMemory: sets the maximum number of objects that will be created in memory.
maxElementsOnDisk: sets the maximum number of objects that will be maintained in the DiskStore. The default value is zero, meaning unlimited.
eternal: sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired.
overflowToDisk: sets whether elements can overflow to disk when the memory store has reached the maxInMemory limit.

The following attributes and elements are optional.
timeToIdleSeconds: sets the time to idle for an element before it expires. Is only used if the element is not e
a60d
ternal. The default value is 0. A value of 0 means that an Element can idle for infinity.
timeToLiveSeconds: sets the time to live for an element before it expires. Is only used if the element is not eternal. The default value is 0. A value of 0 means that an Element can live for infinity.
diskPersistent: Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds: The number of seconds between runs of the disk expiry thread. The default value i 120 seconds. Warning: setting this to a low value is not recommended. It can cause excessive DiskStore locking and high cpu utilisation.
diskSpoolBufferSizeMB: This is the size to allocate the DiskStore for a spool buffer. Writes are made to this area and then asynchronously written to disk. The default size is 30MB. If you get OutOfMemory errors consider lowering this value. To improve DiskStore performance consider increasing it.
clearOnFlush: whether the MemoryStore should be cleared when flush() is called on the cache. By default, this is true i.e. the memoryStore is cleared.
memoryStoreEvictionPolicy: Policy would be enforced upon reaching the maxElementsInmemory limit. Default policy is LRU.
-->
<cache name="codeCache" maxElementsInMemory="0"  eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" />
<cache name="thirdPartnerCache" maxElementsInMemory="0"  eternal="false" overflowToDisk="true" timeToIdleSeconds="600" timeToLiveSeconds="1200" />
</ehcache>



对于自定义的处理方法:

private static CacheService cacheManagerUtil = CacheManagerUtil.getInstance("thirdPartnerCache");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: