今天遇到一个问题,我的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