Configuration Examples
This section presents possible disk usage configurations for open-source Ehcache 2.6 and higher.
Temporary Disk Storage
The "localTempSwap" persistence strategy allows the cache to use the local disk during cache operation. The disk storage is temporary and is cleared after a restart.
<ehcache>
<diskStore path="/auto/default/path"/>
<cache>
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
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.
In-memory Only Cache
When the persistence strategy is "none", all cache stays in memory (with no overflow to disk nor persistence on disk).
<cache>
<persistence strategy="none"/>
</cache>
Programmatic Configuration Example
The following is an example of how to programmatically configure cache persistence on disk:
Configuration cacheManagerConfig = new Configuration()
.diskStore(new DiskStoreConfiguration()
.path("/path/to/store/data"));
CacheConfiguration cacheConfig = new CacheConfiguration()
.name("my-cache")
.maxBytesLocalHeap(16, MemoryUnit.MEGABYTES)
.persistence(new PersistenceConfiguration()
.strategy(Strategy.LOCALTEMPSWAP));
cacheManagerConfig.addCache(cacheConfig);
CacheManager cacheManager = new CacheManager(cacheManagerConfig);
Ehcache myCache = cacheManager.getEhcache("my-cache");