非正常关闭时Ehcache磁盘存储下次不能使用.

在使用ehcache时,我们会使用

diskPersistent="true"

来保证下次虚拟机重启时,缓存数据依然可以使用.

然而当我们ctrl+C或者直接杀死进程时甚至于断电等非正常关闭时,ehcache的缓存就不能再使用了.原因在于:

在ehcache的源码里,DiskStorageFactory类里,硬盘存储初始化时有这段代码:

 if (getDataFile().lastModified() > (indexFile.lastModified() + TimeUnit.SECONDS.toMillis(1))) {
                LOG.warn("The index for data file {} is out of date, probably due to an unclean shutdown. "
                        + "Deleting index file {}", getDataFile(), indexFile);
                deleteFile(indexFile);
            }

这里会认为索引和数据的时间戳对不上,那么这就属于脏数据,所以使用了deleteFile的方法,把索引给删除了.

deleteFile方法里就是直接干掉.这是个protected方法,当然我们可以重写,可惜那太麻烦了,因为有几个方法都引用了deleteFile

比如shutdown方法:当diskPersistent="false"时,在关闭时,会删除磁盘数据.

if (!diskPersistent) {
            deleteFile(file);
            deleteFile(indexFile);
        }

目前没有什么可以解决的,钩子当然没用,ehcache2版的话,企业版倒是或许可以解决吧,里面有这样一个属性strategy,有一个可选项localRestartable  

    strategy:
    配置由配置的缓存提供的持久性类型。这必须是
    以下值之一:

    * localRestartable  - 启用RestartStore并复制所有缓存条目(堆上和/或堆外)
    到磁盘。此选项提供快速可重新启动性,磁盘上具有容错缓存持久性。
    它仅适用于企业 Ehcache用户。

也不知道Ehcache3是什么样,有机会去看看.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EhCache3.X可以在Java代码中使用编程方式配置,而不需要使用XML文件。以下是使用Java代码配置EhCache3.X的示例: ```java import org.ehcache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; import org.ehcache.config.Configuration; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ConfigurationBuilder; import org.ehcache.config.units.EntryUnit; import org.ehcache.expiry.Duration; import org.ehcache.expiry.Expirations; import java.util.concurrent.TimeUnit; public class EhCacheExample { public static void main(String[] args) { // Create a cache configuration CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder( Long.class, String.class, // Set the maximum number of entries in the cache to 100 CacheConfigurationBuilder.newResourcePoolsBuilder().heap(100, EntryUnit.ENTRIES)) // Set the duration of the entries to be 5 minutes .withExpiry(Expirations.timeToLiveExpiration(Duration.of(5, TimeUnit.MINUTES))) .build(); // Create a cache manager configuration Configuration configuration = ConfigurationBuilder.newConfigurationBuilder().build(); // Create a cache manager CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", cacheConfiguration) .withConfiguration(configuration) .build(true); // Get the cache Cache<Long, String> cache = cacheManager.getCache("myCache", Long.class, String.class); // Put an entry in the cache cache.put(1L, "Hello, world!"); // Get an entry from the cache String value = cache.get(1L); // Print the value System.out.println(value); // Close the cache manager cacheManager.close(); } } ``` 在上面的示例中,我们使用`CacheConfigurationBuilder`和`ConfigurationBuilder`创建了缓存和缓存管理器的配置,并使用`CacheManagerBuilder`创建了缓存管理器。我们还设置了缓存的最大条目数和条目的过期间。最后,我们将一个条目放入缓存中并从缓存中获取它。最后,我们关闭了缓存管理器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值