首页 文章

Eh缓存在Spring启动时的到期时间

提问于
浏览
0

我已经创建了一个spring boot应用程序并在maven项目中实现了EhCache . 我的EhCache的xml配置文件如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" 
updateCheck="true"
monitoring="autodetect" 
dynamicConfig="true">

<diskStore path="java.io.tmpdir" />

<cache name="labCabSourceInfoCache" 
    maxEntriesLocalHeap="10000"
    maxEntriesLocalDisk="1000" 
    eternal="false" 
    diskSpoolBufferSizeMB="20"
    timeToIdleSeconds="300" timeToLiveSeconds="300"
    memoryStoreEvictionPolicy="LFU" 
    transactionalMode="off">
    <persistence strategy="localTempSwap" />
</cache>

我在参数timeToIdleSeconds&timeToLiveSeconds中将到期时间设置为300

但它不适合我 . 我没有使用任何配置bean进行缓存实现 . 我使用@Cacheable注释作为用于缓存的方法 .

@Cacheable(value="labCabSourceInfoCache", key="#labAlias.concat(#Account)") 
public String findLabCabSourceInfo(String labAlias, String Account) { 
    try { 
        //codes return "some string" 
    } catch (Exception e) { } return null; 
}

为什么没有被驱逐或清除?

1 回答

  • 0

    您可能需要启用缓存注释的处理 . 你能尝试在主类上添加@EnableCaching吗?

    来自教程https://spring.io/guides/gs/caching/

    @EnableCaching注释触发一个后处理器,它检查每个Spring bean是否存在公共方法上的缓存注释 . 如果找到这样的注释,则自动创建代理以拦截方法调用并相应地处理缓存行为 .

    您还可以添加以下代码,然后分析彻底的Jconsole,即应用程序中创建的缓存的详细信息 .

    @Bean(initMethod="init")
    @Autowired
    public ManagementService managementService(CacheManager cacheManager,
                  MBeanServer mBeanServer) {
           return new ManagementService(cacheManager, mBeanServer, true, true,true, true);
    }
    

相关问题