You can mix the use of the ehcache framework with DirectMemory. Recent versions of ehcache have some features to store elements in a heap off storage when overflow a number of objects or bytes in heap cache.
First you need to declare dependency to the DirectMemory ehcache integration:
<dependency> <groupId>org.apache.directmemory</groupId> <artifactId>directmemory-ehcache</artifactId> <version>0.2</version> </dependency>
Note the heap off cache class to use is not currently configurable in ehcahce see issue https://jira.terracotta.org/jira/browse/EHC-940. So Apache DirectMemory contains same package/class name as needed by ehcache.
Activate the feature in ehcache:
CacheConfiguration cacheConfiguration = new CacheConfiguration(); cacheConfiguration.setName( "foo" ); cacheConfiguration.setOverflowToOffHeap( true ); cacheConfiguration.setMaxBytesLocalHeap( Long.valueOf( 100 * 1024 * 1024 ) ); cacheConfiguration.setMaxBytesLocalOffHeap( Long.valueOf( 100 * 1024 * 1024 ) ); Cache cache = new Cache( cacheConfiguration ); CacheManager.getInstance().addCache( cache );