关于Ehcache缓存中timeToLiveSeconds和timeToIdleSeconds区别

其中主要记录的是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 eternal. 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");


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot整合Ehcache缓存,需要进行以下几个步骤: 1. 添加Ehcache依赖 在pom.xml文件添加Ehcache依赖: ``` <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>${ehcache.version}</version> </dependency> ``` 2. 配置Ehcache缓存 在application.yml(或application.properties)文件配置Ehcache缓存: ``` spring: cache: type: ehcache ehcache: config: classpath:ehcache.xml ``` 其,`spring.cache.type`属性指定使用的缓存类型为Ehcache,`ehcache.config`属性指定Ehcache配置文件的路径(在classpath下)。 3. 编写Ehcache配置文件 在classpath下创建`ehcache.xml`文件,配置Ehcache缓存的相关信息: ``` <ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" transactionalMode="off"> </defaultCache> <cache name="userCache" maxEntriesLocalHeap="1000" maxEntriesLocalDisk="10000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> </cache> </ehcache> ``` 其,`defaultCache`为默认缓存配置,`cache`为自定义缓存配置。 4. 在代码使用缓存 在需要使用缓存的方法上添加`@Cacheable`注解,指定缓存名称和缓存key: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override @Cacheable(value = "userCache", key = "#id") public User getUserById(Integer id) { return userDao.getUserById(id); } // ... } ``` 其,`value`属性指定缓存名称,`key`属性为缓存key表达式,可以使用Spring表达式语言(SpEL)进行动态表达。 以上就是在Spring Boot整合Ehcache缓存的步骤,通过使用Ehcache缓存可以有效地提高应用程序的性能和响应速度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值