Module methanol

Class DiskStore

java.lang.Object
com.github.mizosoft.methanol.internal.cache.DiskStore
All Implemented Interfaces:
Store, Closeable, Flushable, AutoCloseable

public final class DiskStore extends Object implements Store
A persistent Store implementation that saves entries on disk under a specified directory. A DiskStore instance assumes exclusive ownership of its directory; only a single DiskStore from a single JVM process can safely operate on a given directory. This assumption is cooperatively enforced among DiskStore instances such that attempting to initialize a store with a directory that is in use by another store in the same or a different JVM process will cause an IOException to be thrown.

The store keeps track of entries known to it across sessions by maintaining an on-disk hashtable called the index. As changes are made to the store by adding, accessing or removing entries, the index is transparently updated in a time-limited manner. By default, there's at most one index update every 2 seconds. This rate can be changed by setting the system property: com.github.mizosoft.methanol.internal.cache.DiskStore.indexUpdateDelayMillis. Setting a small delay can result in too often index updates, which extracts a noticeable toll on IO and CPU, especially if there's a relatively large number of entries (updating entails reconstructing then rewriting the whole index). On the other hand, scarcely updating the index affords less durability against crashes as entries that aren't indexed are dropped on initialization. Calling the flush method forces an index update, regardless of the time limit.

To ensure entries are not lost across sessions, a store must be closed after it has been done with. The dispose() method can be called to atomically close the store and clear its directory if persistence isn't needed (e.g. using temp directories for storage). A closed store usually throws an IllegalStateException when used.