Configuring Disk Store
The disk store provides a thread-safe disk-spooling facility that can be used for either additional storage or persisting data through system restarts.
This section describes local disk usage.
You can find additional information about configuring the disk store in Configuring Restartability and Persistence. Serialization
Only data that is Serializable can be placed in the disk store. Writes to and from the disk use ObjectInputStream and the Java serialization mechanism. Any non-serializable data overflowing to the disk store is removed and a NotSerializableException is thrown.
Serialization speed is affected by the size of the objects being serialized and their type. It has been found that:
The serialization time for a Java object consisting of a large Map of String arrays was 126ms, where the serialized size was 349,225 bytes.
The serialization time for a byte[] was 7ms, where the serialized size was 310,232 bytes.
Byte arrays are 20 times faster to serialize, making them a better choice for increasing disk-store performance.
Configuring the Disk Store
Disk stores are configured on a per CacheManager basis. If one or more caches requires a disk store but none is configured, a default directory is used and a warning message is logged to encourage explicit configuration of the disk store path.
Configuring a disk store is optional. If all caches use only memory, then there is no need to configure a disk store. This simplifies configuration, and uses fewer threads. This also makes it unnecessary to configure multiple disk store paths when multiple CacheManagers are being used.
Two disk store options are available:
Temporary store (
localTempSwap)
Persistent store (
localRestartable)
localTempSwap
The localTempSwap persistence strategy allows the memory store to overflow to disk when it becomes full. This option makes the disk a temporary store because overflow data does not survive restarts or failures. When the node is restarted, any existing data on disk is cleared because it is not designed to be reloaded.
If the disk store path is not specified, a default path is used, and the default will be auto-resolved in the case of a conflict with another CacheManager.
The localTempSwap disk store creates a data file for each cache on startup called "<cache_name>.data".
localRestartable
This option implements a restartable store for all in-memory data. After any restart, the data set is automatically reloaded from disk to the in-memory stores.
The path to the directory where any required disk files will be created is configured with the <diskStore> sub-element of the Ehcache configuration. In order to use the restartable store, a unique and explicitly specified path is required.
The diskStore Configuration Element
Files are created in the directory specified by the <diskStore> configuration element. The <diskStore> element has one attribute called path.
<diskStore path="/path/to/store/data"/>
Legal values for the path attribute are legal file system paths. For example, for Unix:
/home/application/cache
The following system properties are also legal, in which case they are translated:
user.home - User's home directory
user.dir- User's current working directory
java.io.tmpdir - Default temp file path
ehcache.disk.store.dir- A system property you would normally specify on the command line—for example,
java -Dehcache.disk.store.dir=/u01/myapp/diskdir.
Subdirectories can be specified below the system property, for example:
user.dir/one
To programmatically set a disk store path:
DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
diskStoreConfiguration.setPath("/my/path/dir");
// Already created a configuration object ...
configuration.addDiskStore(diskStoreConfiguration);
CacheManager mgr = new CacheManager(configuration);
Note: | A CacheManager's disk store path cannot be changed once it is set in configuration. If the disk store path is changed, the CacheManager must be recycled for the new path to take effect. |
Disk Store Expiry and Eviction
Expired elements are eventually evicted to free up disk space. The element is also removed from the in-memory index of elements.
One thread per cache is used to remove expired elements. The optional attribute diskExpiryThreadIntervalSeconds sets the interval between runs of the expiry thread.
Important: | Setting diskExpiryThreadIntervalSeconds to a low value can cause excessive disk-store locking and high CPU utilization. The default value is 120 seconds. |
If a cache's disk store has a limited size, Elements will be evicted from the disk store when it exceeds this limit. The LFU algorithm is used for these evictions. It is not configurable or changeable.
Note: | With the localTempSwap strategy, you can use maxEntriesLocalDisk or maxBytesLocalDisk at either the Cache or CacheManager level to control the size of the disk tier. |
Turning off Disk Stores
To turn off disk store path creation, comment out the diskStore element in ehcache.xml.
The default Ehcache configuration, ehcache-failsafe.xml, uses a disk store. To avoid use of a disk store, specify a custom ehcache.xml with the diskStore element commented out.