springboot ehcache 设置过期时间不生效的解决办法

本文详细解析了SpringBoot项目中集成Ehcache时,配置的过期时间timeToIdleSeconds不生效的原因及解决办法。通过调整application.properties文件,明确指定缓存类型为ehcache,并正确引用ehcache.xml配置文件,从而有效控制缓存过期时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天遇到一个问题,我的springboot集成了ehcache,配置了过期时间timeToIdleSeconds,发现就是不生效,默认永久不过期

ehcache.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
	updateCheck="false">
	<defaultCache eternal="false" maxElementsInMemory="1000"
		overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="2"
		timeToLiveSeconds="2" memoryStoreEvictionPolicy="LRU" />

	<cache name="menus" eternal="false" maxElementsInMemory="100"
		overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="5"
		 memoryStoreEvictionPolicy="LRU" />

</ehcache>

上面的配置是没有问题的,其实关键的问题在于没有指定缓存类型为ehcache,ehcache.xml文件压根就没有生效,springboot使用默认的SimpleCacheConfiguration,不是用的ehcache,解决方法如下:

application.properties添加如下

spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:ehcache.xml

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值