繁体   English   中英

Spring MVC Ehcache问题

[英]Spring mvc Ehcache issue

我正在尝试在Spring Echcache中使用Echcache 所以为此使用类似于以下的java配置:

public net.sf.ehcache.CacheManager ehCacheManager() {
    net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
    CacheConfiguration cacheConfig = new CacheConfiguration();
    cacheConfig.setName("test");
    cacheConfig.setMaxEntriesLocalHeap(0);
    cacheConfig.setMaxEntriesLocalDisk(10);
    cacheConfig.setTimeToLiveSeconds(500);
    config.addCache(cacheConfig);
    DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
    diskStoreConfiguration.setPath("C:/MyCache1");
    config.addDiskStore(diskStoreConfiguration);
    return net.sf.ehcache.CacheManager.newInstance(config);
}

并使用@cachable批注进行缓存。 它正常工作。 但所有缓存的数据都保存在内存中。 并仅创建大小为零的test.data文件。 现在我的问题是如何将缓存的数据写入磁盘?

添加以下设置以存储在磁盘中:

cacheConfig.setDiskPersistent(true);

public net.sf.ehcache.CacheManager ehCacheManager() {
    net.sf.ehcache.config.Configuration config = new 
    net.sf.ehcache.config.Configuration();
    CacheConfiguration cacheConfig = new CacheConfiguration();
    cacheConfig.setName("test");
    cacheConfig.setMaxEntriesLocalHeap(0);
    cacheConfig.setMaxEntriesLocalDisk(10);
    cacheConfig.setTimeToLiveSeconds(500);
    cacheConfig.setDiskPersistent(true);
    config.addCache(cacheConfig);
    DiskStoreConfiguration diskStoreConfiguration = new 
    DiskStoreConfiguration();
    diskStoreConfiguration.setPath("C:/MyCache1");
    config.addDiskStore(diskStoreConfiguration);
    return net.sf.ehcache.CacheManager.newInstance(config);
}
  • 注意:此方法现在@Deprecated,已被persistence(PersistenceConfiguration)方法替换。

如另一个答案所述,您需要使缓存本身使用磁盘层。

如果您使用的是最新版本的Ehcache 2.x,则应通过以下方法完成: cacheConfig.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration‌​.Strategy.LOCALTEMPS‌​WAP)); 正如您在评论中指出的那样。 请注意,此选项是不防崩溃的磁盘层,而选项Strategy.LOCALRESTARTABLE可以处理崩溃,但仅在企业版中提供。

但是以上内容还不够,您还需要在应用程序关闭时正确关闭缓存和缓存管理器。 这将导致刷新所有条目并创建索引文件。 没有这些,数据将在重新启动时被丢弃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM